From dfb46a47b22620ab50b3065fc210e4a3f39a0117 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Sat, 7 Jan 2023 13:07:33 +0800 Subject: [PATCH 1/5] feat(core): add markdownEnv to Page --- packages/core/src/page/createPage.ts | 2 ++ packages/core/src/page/renderPageContent.ts | 15 +++++++++++++++ packages/core/src/types/page.ts | 5 +++++ .../core/tests/page/renderPageContent.spec.ts | 1 + 4 files changed, 23 insertions(+) diff --git a/packages/core/src/page/createPage.ts b/packages/core/src/page/createPage.ts index 7f40f66db8..51b296073f 100644 --- a/packages/core/src/page/createPage.ts +++ b/packages/core/src/page/createPage.ts @@ -37,6 +37,7 @@ export const createPage = async ( frontmatter, headers, links, + markdownEnv, sfcBlocks, title, } = renderPageContent({ @@ -123,6 +124,7 @@ export const createPage = async ( date, deps, links, + markdownEnv, pathInferred, pathLocale, permalink, diff --git a/packages/core/src/page/renderPageContent.ts b/packages/core/src/page/renderPageContent.ts index b3d0e966c3..e213249284 100644 --- a/packages/core/src/page/renderPageContent.ts +++ b/packages/core/src/page/renderPageContent.ts @@ -24,6 +24,7 @@ export const renderPageContent = ({ }): { contentRendered: string deps: string[] + markdownEnv: Record frontmatter: PageFrontmatter headers: MarkdownHeader[] links: MarkdownLink[] @@ -54,6 +55,19 @@ export const renderPageContent = ({ customBlocks: [], }, title = '', + + // values dropped from env + + // eslint-disable-next-line @typescript-eslint/naming-convention + base: _base, + // eslint-disable-next-line @typescript-eslint/naming-convention + content: _content, + // eslint-disable-next-line @typescript-eslint/naming-convention + filePath: _filePath, + // eslint-disable-next-line @typescript-eslint/naming-convention + filePathRelative: _filePathRelative, + + ...otherEnv } = markdownEnv return { @@ -62,6 +76,7 @@ export const renderPageContent = ({ frontmatter, headers, links, + markdownEnv: otherEnv, sfcBlocks, title: frontmatter.title ?? title, } diff --git a/packages/core/src/types/page.ts b/packages/core/src/types/page.ts index 532ba237b1..5b04a924af 100644 --- a/packages/core/src/types/page.ts +++ b/packages/core/src/types/page.ts @@ -74,6 +74,11 @@ export type Page< */ routeMeta: Record + /** + * Markdown Environment of the page + */ + markdownEnv: Record + /** * Extracted sfc blocks of the page */ diff --git a/packages/core/tests/page/renderPageContent.spec.ts b/packages/core/tests/page/renderPageContent.spec.ts index 0ecc671b71..055beaf0a1 100644 --- a/packages/core/tests/page/renderPageContent.spec.ts +++ b/packages/core/tests/page/renderPageContent.spec.ts @@ -31,6 +31,7 @@ const msg = 'msg' frontmatter: {}, headers: [], links: [], + markdownEnv: { excerpt: '' }, sfcBlocks: { template: { type: 'template', From 0c379034397e3d07ef9bb71d9d378514c43e7a3a Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 27 Feb 2023 19:25:56 +0800 Subject: [PATCH 2/5] refactor: use omit --- packages/core/package.json | 4 ++++ packages/core/src/page/renderPageContent.ts | 24 +++++++++------------ packages/core/src/types/page.ts | 10 ++++----- pnpm-lock.yaml | 19 ++++++++++++++++ 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 819e1b8582..00bd8af469 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -35,8 +35,12 @@ "@vuepress/markdown": "workspace:*", "@vuepress/shared": "workspace:*", "@vuepress/utils": "workspace:*", + "lodash.omit": "^4.5.0", "vue": "^3.2.47" }, + "devDependencies": { + "@types/lodash.omit": "^4.5.7" + }, "publishConfig": { "access": "public" }, diff --git a/packages/core/src/page/renderPageContent.ts b/packages/core/src/page/renderPageContent.ts index e213249284..d99a8c6b00 100644 --- a/packages/core/src/page/renderPageContent.ts +++ b/packages/core/src/page/renderPageContent.ts @@ -4,6 +4,7 @@ import type { MarkdownLink, MarkdownSfcBlocks, } from '@vuepress/markdown' +import omit from 'lodash.omit' import type { App, PageFrontmatter, PageOptions } from '../types/index.js' /** @@ -55,19 +56,7 @@ export const renderPageContent = ({ customBlocks: [], }, title = '', - - // values dropped from env - - // eslint-disable-next-line @typescript-eslint/naming-convention - base: _base, - // eslint-disable-next-line @typescript-eslint/naming-convention - content: _content, - // eslint-disable-next-line @typescript-eslint/naming-convention - filePath: _filePath, - // eslint-disable-next-line @typescript-eslint/naming-convention - filePathRelative: _filePathRelative, - - ...otherEnv + ...extraMarkdownEnv } = markdownEnv return { @@ -76,7 +65,14 @@ export const renderPageContent = ({ frontmatter, headers, links, - markdownEnv: otherEnv, + markdownEnv: omit( + extraMarkdownEnv, + 'base', + 'content', + 'filePath', + 'filePathRelative', + 'frontmatter' + ), sfcBlocks, title: frontmatter.title ?? title, } diff --git a/packages/core/src/types/page.ts b/packages/core/src/types/page.ts index 5b04a924af..fa01216ace 100644 --- a/packages/core/src/types/page.ts +++ b/packages/core/src/types/page.ts @@ -42,6 +42,11 @@ export type Page< */ links: MarkdownLink[] + /** + * Markdown env object of the page + */ + markdownEnv: Record + /** * Path of the page that inferred from file path * @@ -74,11 +79,6 @@ export type Page< */ routeMeta: Record - /** - * Markdown Environment of the page - */ - markdownEnv: Record - /** * Extracted sfc blocks of the page */ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ed8be93996..cd0fa3e050 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -533,17 +533,22 @@ importers: packages/core: specifiers: + '@types/lodash.omit': ^4.5.7 '@vuepress/client': workspace:* '@vuepress/markdown': workspace:* '@vuepress/shared': workspace:* '@vuepress/utils': workspace:* + lodash.omit: ^4.5.0 vue: ^3.2.47 dependencies: '@vuepress/client': link:../client '@vuepress/markdown': link:../markdown '@vuepress/shared': link:../shared '@vuepress/utils': link:../utils + lodash.omit: 4.5.0 vue: 3.2.47 + devDependencies: + '@types/lodash.omit': 4.5.7 packages/markdown: specifiers: @@ -2892,6 +2897,16 @@ packages: resolution: {integrity: sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==} dev: false + /@types/lodash.omit/4.5.7: + resolution: {integrity: sha512-6q6cNg0tQ6oTWjSM+BcYMBhan54P/gLqBldG4AuXd3nKr0oeVekWNS4VrNEu3BhCSDXtGapi7zjhnna0s03KpA==} + dependencies: + '@types/lodash': 4.14.191 + dev: true + + /@types/lodash/4.14.191: + resolution: {integrity: sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==} + dev: true + /@types/markdown-it-emoji/2.0.2: resolution: {integrity: sha512-2ln8Wjbcj/0oRi/6VnuMeWEHHuK8uapFttvcLmDIe1GKCsFBLOLBX+D+xhDa9oWOQV0IpvxwrSfKKssAqqroog==} dependencies: @@ -7137,6 +7152,10 @@ packages: resolution: {integrity: sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==} dev: true + /lodash.omit/4.5.0: + resolution: {integrity: sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg==} + dev: false + /lodash.snakecase/4.1.1: resolution: {integrity: sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==} dev: true From d6bf4b736159e9ddcc8e63b87df37e9290a503af Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 27 Feb 2023 19:33:24 +0800 Subject: [PATCH 3/5] feat: add omit util --- packages/core/package.json | 4 ---- packages/core/src/page/renderPageContent.ts | 2 +- packages/shared/src/utils/index.ts | 1 + packages/shared/src/utils/omit.ts | 13 +++++++++++++ pnpm-lock.yaml | 19 ------------------- 5 files changed, 15 insertions(+), 24 deletions(-) create mode 100644 packages/shared/src/utils/omit.ts diff --git a/packages/core/package.json b/packages/core/package.json index 00bd8af469..819e1b8582 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -35,12 +35,8 @@ "@vuepress/markdown": "workspace:*", "@vuepress/shared": "workspace:*", "@vuepress/utils": "workspace:*", - "lodash.omit": "^4.5.0", "vue": "^3.2.47" }, - "devDependencies": { - "@types/lodash.omit": "^4.5.7" - }, "publishConfig": { "access": "public" }, diff --git a/packages/core/src/page/renderPageContent.ts b/packages/core/src/page/renderPageContent.ts index d99a8c6b00..b0de9c67ba 100644 --- a/packages/core/src/page/renderPageContent.ts +++ b/packages/core/src/page/renderPageContent.ts @@ -4,7 +4,7 @@ import type { MarkdownLink, MarkdownSfcBlocks, } from '@vuepress/markdown' -import omit from 'lodash.omit' +import { omit } from '@vuepress/shared' import type { App, PageFrontmatter, PageOptions } from '../types/index.js' /** diff --git a/packages/shared/src/utils/index.ts b/packages/shared/src/utils/index.ts index 28ce5ae511..a3967da277 100644 --- a/packages/shared/src/utils/index.ts +++ b/packages/shared/src/utils/index.ts @@ -11,6 +11,7 @@ export * from './isLinkHttp.js' export * from './isLinkMailto.js' export * from './isLinkTel.js' export * from './isPlainObject.js' +export * from './omit.js' export * from './removeEndingSlash.js' export * from './removeLeadingSlash.js' export * from './resolveHeadIdentifier.js' diff --git a/packages/shared/src/utils/omit.ts b/packages/shared/src/utils/omit.ts new file mode 100644 index 0000000000..1c8af7e951 --- /dev/null +++ b/packages/shared/src/utils/omit.ts @@ -0,0 +1,13 @@ +/** + * Omit properties from an object + */ +export const omit = >( + obj: T, + ...keys: string[] +): Omit => { + const result = { ...obj } + for (const key of keys) { + delete result[key] + } + return result +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd0fa3e050..ed8be93996 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -533,22 +533,17 @@ importers: packages/core: specifiers: - '@types/lodash.omit': ^4.5.7 '@vuepress/client': workspace:* '@vuepress/markdown': workspace:* '@vuepress/shared': workspace:* '@vuepress/utils': workspace:* - lodash.omit: ^4.5.0 vue: ^3.2.47 dependencies: '@vuepress/client': link:../client '@vuepress/markdown': link:../markdown '@vuepress/shared': link:../shared '@vuepress/utils': link:../utils - lodash.omit: 4.5.0 vue: 3.2.47 - devDependencies: - '@types/lodash.omit': 4.5.7 packages/markdown: specifiers: @@ -2897,16 +2892,6 @@ packages: resolution: {integrity: sha512-HZQYqbiFVWufzCwexrvh694SOim8z2d+xJl5UNamcvQFejLY/2YUtzXHYi3cHdI7PMlS8ejH2slRAOJQ32aNbA==} dev: false - /@types/lodash.omit/4.5.7: - resolution: {integrity: sha512-6q6cNg0tQ6oTWjSM+BcYMBhan54P/gLqBldG4AuXd3nKr0oeVekWNS4VrNEu3BhCSDXtGapi7zjhnna0s03KpA==} - dependencies: - '@types/lodash': 4.14.191 - dev: true - - /@types/lodash/4.14.191: - resolution: {integrity: sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ==} - dev: true - /@types/markdown-it-emoji/2.0.2: resolution: {integrity: sha512-2ln8Wjbcj/0oRi/6VnuMeWEHHuK8uapFttvcLmDIe1GKCsFBLOLBX+D+xhDa9oWOQV0IpvxwrSfKKssAqqroog==} dependencies: @@ -7152,10 +7137,6 @@ packages: resolution: {integrity: sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==} dev: true - /lodash.omit/4.5.0: - resolution: {integrity: sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg==} - dev: false - /lodash.snakecase/4.1.1: resolution: {integrity: sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==} dev: true From 42dc48f9ffcc9c29238568f288af5e195e047e86 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 27 Feb 2023 19:47:43 +0800 Subject: [PATCH 4/5] docs: add docs for markdownEnv --- docs/reference/node-api.md | 17 ++++++++++++++++- docs/zh/reference/node-api.md | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/reference/node-api.md b/docs/reference/node-api.md index f93cb1074c..5fb978a9d5 100644 --- a/docs/reference/node-api.md +++ b/docs/reference/node-api.md @@ -536,7 +536,22 @@ interface MarkdownLink { - Details: - Links of the page. + Links included in the page content. + +### markdownEnv + +- Type: `Record` + +- Details: + + The `env` object when parsing markdown content with markdown-it. + + Some markdown-it plugins may store extra information inside this object, and you can make use of them for advanced customization. + + Notice that some other page properties are also extracted from the original `env` object. Those properties have already been removed from `page.markdownEnv`. + +- Also see: + - [markdown-it > API Documentation > MarkdownIt > parse](https://markdown-it.github.io/markdown-it/#MarkdownIt.parse) ### pathInferred diff --git a/docs/zh/reference/node-api.md b/docs/zh/reference/node-api.md index 083ef742b1..d877f187d6 100644 --- a/docs/zh/reference/node-api.md +++ b/docs/zh/reference/node-api.md @@ -531,7 +531,23 @@ interface MarkdownLink { - 详情: - 该 Page 中的链接。 + 该 Page 内容中包含的链接。 + + +### markdownEnv + +- 类型: `Record` + +- 详情: + + 在使用 markdown-it 解析 Markdown 内容时的 `env` 对象。 + + 一些 markdown-it 插件可能会在这个对象中存储一些额外的信息,你可以使用它们来进行高级定制化。 + + 需要注意的是,其他的一些 Page 属性其实也是从 `env` 对象中获取到的,但是我们已经把这些属性从 `page.markdownEnv` 中移除掉了。 + +- 参考: + - [markdown-it > API Documentation > MarkdownIt > parse](https://markdown-it.github.io/markdown-it/#MarkdownIt.parse) ### pathInferred From f7c3f39cefc7fc6387657d262b4eeb5d3a6bb842 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 27 Feb 2023 19:55:40 +0800 Subject: [PATCH 5/5] chore: tweaks --- packages/shared/src/utils/omit.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/shared/src/utils/omit.ts b/packages/shared/src/utils/omit.ts index 1c8af7e951..ce9febaba6 100644 --- a/packages/shared/src/utils/omit.ts +++ b/packages/shared/src/utils/omit.ts @@ -1,10 +1,10 @@ /** * Omit properties from an object */ -export const omit = >( +export const omit = , U extends string[]>( obj: T, - ...keys: string[] -): Omit => { + ...keys: U +): Omit => { const result = { ...obj } for (const key of keys) { delete result[key]