Skip to content

Commit 8a63e19

Browse files
feat(core): attach markdownEnv to page object (#1228)
Co-authored-by: meteorlxy <meteor.lxy@foxmail.com>
1 parent f5d5b11 commit 8a63e19

File tree

8 files changed

+66
-2
lines changed

8 files changed

+66
-2
lines changed

docs/reference/node-api.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,22 @@ interface MarkdownLink {
536536

537537
- Details:
538538

539-
Links of the page.
539+
Links included in the page content.
540+
541+
### markdownEnv
542+
543+
- Type: `Record<string, unknown>`
544+
545+
- Details:
546+
547+
The `env` object when parsing markdown content with markdown-it.
548+
549+
Some markdown-it plugins may store extra information inside this object, and you can make use of them for advanced customization.
550+
551+
Notice that some other page properties are also extracted from the original `env` object. Those properties have already been removed from `page.markdownEnv`.
552+
553+
- Also see:
554+
- [markdown-it > API Documentation > MarkdownIt > parse](https://markdown-it.github.io/markdown-it/#MarkdownIt.parse)
540555

541556
### pathInferred
542557

docs/zh/reference/node-api.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,23 @@ interface MarkdownLink {
531531

532532
- 详情:
533533

534-
该 Page 中的链接。
534+
该 Page 内容中包含的链接。
535+
536+
537+
### markdownEnv
538+
539+
- 类型: `Record<string, unknown>`
540+
541+
- 详情:
542+
543+
在使用 markdown-it 解析 Markdown 内容时的 `env` 对象。
544+
545+
一些 markdown-it 插件可能会在这个对象中存储一些额外的信息,你可以使用它们来进行高级定制化。
546+
547+
需要注意的是,其他的一些 Page 属性其实也是从 `env` 对象中获取到的,但是我们已经把这些属性从 `page.markdownEnv` 中移除掉了。
548+
549+
- 参考:
550+
- [markdown-it > API Documentation > MarkdownIt > parse](https://markdown-it.github.io/markdown-it/#MarkdownIt.parse)
535551

536552
### pathInferred
537553

packages/core/src/page/createPage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export const createPage = async (
3737
frontmatter,
3838
headers,
3939
links,
40+
markdownEnv,
4041
sfcBlocks,
4142
title,
4243
} = renderPageContent({
@@ -123,6 +124,7 @@ export const createPage = async (
123124
date,
124125
deps,
125126
links,
127+
markdownEnv,
126128
pathInferred,
127129
pathLocale,
128130
permalink,

packages/core/src/page/renderPageContent.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
MarkdownLink,
55
MarkdownSfcBlocks,
66
} from '@vuepress/markdown'
7+
import { omit } from '@vuepress/shared'
78
import type { App, PageFrontmatter, PageOptions } from '../types/index.js'
89

910
/**
@@ -24,6 +25,7 @@ export const renderPageContent = ({
2425
}): {
2526
contentRendered: string
2627
deps: string[]
28+
markdownEnv: Record<string, unknown>
2729
frontmatter: PageFrontmatter
2830
headers: MarkdownHeader[]
2931
links: MarkdownLink[]
@@ -54,6 +56,7 @@ export const renderPageContent = ({
5456
customBlocks: [],
5557
},
5658
title = '',
59+
...extraMarkdownEnv
5760
} = markdownEnv
5861

5962
return {
@@ -62,6 +65,14 @@ export const renderPageContent = ({
6265
frontmatter,
6366
headers,
6467
links,
68+
markdownEnv: omit(
69+
extraMarkdownEnv,
70+
'base',
71+
'content',
72+
'filePath',
73+
'filePathRelative',
74+
'frontmatter'
75+
),
6576
sfcBlocks,
6677
title: frontmatter.title ?? title,
6778
}

packages/core/src/types/page.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export type Page<
4242
*/
4343
links: MarkdownLink[]
4444

45+
/**
46+
* Markdown env object of the page
47+
*/
48+
markdownEnv: Record<string, unknown>
49+
4550
/**
4651
* Path of the page that inferred from file path
4752
*

packages/core/tests/page/renderPageContent.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const msg = 'msg'
3131
frontmatter: {},
3232
headers: [],
3333
links: [],
34+
markdownEnv: { excerpt: '' },
3435
sfcBlocks: {
3536
template: {
3637
type: 'template',

packages/shared/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './isLinkHttp.js'
1111
export * from './isLinkMailto.js'
1212
export * from './isLinkTel.js'
1313
export * from './isPlainObject.js'
14+
export * from './omit.js'
1415
export * from './removeEndingSlash.js'
1516
export * from './removeLeadingSlash.js'
1617
export * from './resolveHeadIdentifier.js'

packages/shared/src/utils/omit.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Omit properties from an object
3+
*/
4+
export const omit = <T extends Record<string, unknown>, U extends string[]>(
5+
obj: T,
6+
...keys: U
7+
): Omit<T, U[number]> => {
8+
const result = { ...obj }
9+
for (const key of keys) {
10+
delete result[key]
11+
}
12+
return result
13+
}

0 commit comments

Comments
 (0)