Skip to content

Commit 8441569

Browse files
committedJul 2, 2022
refactor(markdown): externalize headers and title plugin
BREAKING CHANGE: `markdown.extractHeaders` has been renamed to `markdown.headers` BREAKING CHANGE: `markdown.extractTitle` has been renamed to `markdown.title`
1 parent 20f8a3f commit 8441569

File tree

24 files changed

+102
-704
lines changed

24 files changed

+102
-704
lines changed
 

‎docs/guide/migration.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ Use [extendsMarkdown](../reference/plugin-api.md#extendsmarkdown) hook.
118118

119119
#### markdown.extractHeaders
120120

121-
Changed.
122-
123-
See [Config > markdown.extractHeaders](../reference/config.md#markdown-extractheaders)
121+
Moved to [markdown.headers](../reference/config.md#markdown-headers).
124122

125123
#### Webpack Related Configs
126124

‎docs/reference/config.md

+17-59
Original file line numberDiff line numberDiff line change
@@ -488,75 +488,19 @@ You should not configure it unless you understand what it is for.
488488
- Also see:
489489
- [Guide > Markdown > Syntax Extensions > Emoji](../guide/markdown.md#emoji)
490490

491-
### markdown.extractHeaders
491+
### markdown.headers
492492

493-
- Type: `ExtractHeadersPluginOptions | false`
493+
- Type: `HeadersPluginOptions | false`
494494

495495
- Details:
496496

497-
Options for VuePress built-in markdown-it extract-headers plugin.
498-
499-
It will extract page headers to page data, which would be used for generating sidebar, table of contents, etc. For example, the sidebar of current page is auto generated from the headers that extracted by this plugin.
497+
Options for [@mdit-vue/plugin-headers](https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-headers).
500498

501499
Set to `false` to disable this plugin.
502500

503501
- Also see:
504502
- [Node API > Page Properties > headers](./node-api.md#headers)
505503

506-
#### markdown.extractHeaders.level
507-
508-
- Type: `number[]`
509-
510-
- Default: `[2, 3]`
511-
512-
- Details:
513-
514-
Header levels that going to be extracted.
515-
516-
For example, if you set this option to `[2]`, only `##` headers will be extracted.
517-
518-
Should be a subset of [markdown.anchor.level](#markdown-anchor) option to ensure the extracted links are existed.
519-
520-
#### markdown.extractHeaders.slugify
521-
522-
- Type: `(str: string) => string`
523-
524-
- Details:
525-
526-
A function to get the extracted slug of header from the raw header title.
527-
528-
Should use the same slugify function with [markdown.anchor.slugify](#markdown-anchor) to ensure the links are matched.
529-
530-
::: tip
531-
If you really want to modify the slugify function, you'd better change the [markdown.slugify](#markdown-slugify) option instead of this one. Otherwise you have to set all the options about slugify function to ensure they are consistent.
532-
:::
533-
534-
#### markdown.extractHeaders.format
535-
536-
- Type: `((str: string) => string) | undefined`
537-
538-
- Default: `undefined`
539-
540-
- Details:
541-
542-
A function to format the extracted title of header from the raw header title.
543-
544-
### markdown.extractTitle
545-
546-
- Type: `undefined | false`
547-
548-
- Details:
549-
550-
Options for VuePress built-in markdown-it extract-title plugin.
551-
552-
It will extract title to page data, which will be used as the page title.
553-
554-
Set to `false` to disable this plugin.
555-
556-
::: danger
557-
You should not configure it unless you understand what it is for.
558-
:::
559-
560504
### markdown.importCode
561505

562506
- Type: `ImportCodePluginOptions | false`
@@ -639,6 +583,20 @@ You should not configure it unless you understand what it is for.
639583

640584
The default slugify function.
641585

586+
### markdown.title
587+
588+
- Type: `undefined | false`
589+
590+
- Details:
591+
592+
Options for [@mdit-vue/plugin-title](https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-title).
593+
594+
Set to `false` to disable this plugin.
595+
596+
::: danger
597+
You should not configure it unless you understand what it is for.
598+
:::
599+
642600
### markdown.toc
643601

644602
- Type: `TocPluginOptions | false`

‎docs/reference/default-theme/config.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ module.exports = {
415415
- Set to `2` to include `<h2>` and `<h3>` headers.
416416
- ...
417417

418-
The max value depends on which levels of headers you have extracted via [markdown.extractHeaders.level](../config.md#markdown-extractheaders).
418+
The max value depends on which levels of headers you have extracted via [markdown.headers.level](../config.md#markdown-headers).
419419

420-
The default value of `markdown.extractHeaders.level` is `[2, 3]`, so the default max value of `sidebarDepth` is `2`.
420+
The default value of `markdown.headers.level` is `[2, 3]`, so the default max value of `sidebarDepth` is `2`.
421421

422422
You can override this global option via [sidebarDepth](./frontmatter.md#sidebardepth) frontmatter in your pages.
423423

‎docs/reference/node-api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ interface PageHeader {
482482
Headers of the page.
483483

484484
- Also see:
485-
- [Config > markdown.extractHeaders](./config.md#markdown-extractheaders)
485+
- [Config > markdown.headers](./config.md#markdown-headers)
486486

487487
### data
488488

‎docs/reference/plugin-api.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ Modifying the default header levels that going to be extracted:
214214
```js
215215
export default {
216216
extendsMarkdownOptions: (markdownOptions, app) => {
217-
if (markdownOptions.extractHeaders === false) return
218-
markdownOptions.extractHeaders ??= {}
219-
if (markdownOptions.extractHeaders.level) return
220-
markdownOptions.extractHeaders.level = [2, 3, 4, 5, 6]
217+
if (markdownOptions.headers === false) return
218+
markdownOptions.headers ??= {}
219+
if (markdownOptions.headers.level) return
220+
markdownOptions.headers.level = [2, 3, 4, 5, 6]
221221
},
222222
}
223223
```

‎docs/zh/guide/migration.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ VuePress v2 的核心思想和流程是和 v1 一致的,但 v2 API 经过了
118118

119119
#### markdown.extractHeaders
120120

121-
有修改。
122-
123-
参考 [配置 > markdown.extractHeaders](../reference/config.md#markdown-extractheaders)
121+
移动至 [markdown.headers](../reference/config.md#markdown-headers)
124122

125123
#### Webpack 相关配置
126124

‎docs/zh/reference/config.md

+5-47
Original file line numberDiff line numberDiff line change
@@ -487,68 +487,26 @@ VuePress 在开发和构建时会加载临时文件,因此临时文件目录
487487
- 参考:
488488
- [指南 > Markdown > 语法扩展 > Emoji](../guide/markdown.md#emoji)
489489

490-
### markdown.extractHeaders
490+
### markdown.headers
491491

492-
- 类型: `ExtractHeadersPluginOptions | false`
492+
- 类型: `HeadersPluginOptions | false`
493493

494494
- 详情:
495495

496-
VuePress 内置的 markdown-it extract-headers 插件的配置项。
497-
498-
它将会把页面的子标题提取到 Page Data 中,可以用于生成侧边栏、目录等。比如当前页面的侧边栏,就是由这个插件提取出的标题来自动生成的。
496+
[@mdit-vue/plugin-headers](https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-headers) 插件的配置项。
499497

500498
设置为 `false` 可以禁用该插件。
501499

502500
- 参考:
503501
- [Node API > Page 属性 > headers](./node-api.md#headers)
504502

505-
#### markdown.extractHeaders.level
506-
507-
- 类型: `number[]`
508-
509-
- 默认值: `[2, 3]`
510-
511-
- 详情:
512-
513-
需要提取的子标题层级。
514-
515-
举例来说,如果你把该选项设为 `[2]` ,那么只会提取 `##` 子标题。
516-
517-
它应该是 [markdown.anchor.level](#markdown-anchor) 选项的一个子集,以便确保提取出来的链接是存在的。
518-
519-
#### markdown.extractHeaders.slugify
520-
521-
- 类型: `(str: string) => string`
522-
523-
- 详情:
524-
525-
一个函数,根据原始的子标题来获取提取出的子标题 slug 。
526-
527-
它应该使用和 [markdown.anchor.slugify](#markdown-anchor) 选项相同的 slugify 函数,来确保链接是匹配的。
528-
529-
::: tip
530-
如果你确实想要修改 slugify 函数,建议你修改 [markdown.slugify](#markdown-slugify) 配置项,否则你必须同时修改其他的 slugify 函数配置来确保它们是一致的。
531-
:::
532-
533-
#### markdown.extractHeaders.format
534-
535-
- 类型: `((str: string) => string) | undefined`
536-
537-
- 默认值: `undefined`
538-
539-
- 详情:
540-
541-
一个函数,将原始的子标题格式化为提取出的标题。
542-
543-
### markdown.extractTitle
503+
### markdown.title
544504

545505
- 类型: `undefined | false`
546506

547507
- 详情:
548508

549-
VuePress 内置的 markdown-it extract-title 插件的配置项。
550-
551-
它将会把大标题提取到 Page Data 中,将会被用作页面标题。
509+
[@mdit-vue/plugin-title](https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-title) 插件的配置项。
552510

553511
设置为 `false` 可以禁用该插件。
554512

‎docs/zh/reference/default-theme/config.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ module.exports = {
415415
- 设为 `2` 来包含 `<h2>``<h3>` 标题。
416416
- ...
417417

418-
最大值取决于你通过 [markdown.extractHeaders.level](../config.md#markdown-extractheaders) 提取了哪些级别的标题。
418+
最大值取决于你通过 [markdown.headers.level](../config.md#markdown-headers) 提取了哪些级别的标题。
419419

420-
由于 `markdown.extractHeaders.level` 的默认值是 `[2, 3]` ,因此 `sidebarDepth` 的默认最大值是 `2`
420+
由于 `markdown.headers.level` 的默认值是 `[2, 3]` ,因此 `sidebarDepth` 的默认最大值是 `2`
421421

422422
你可以通过页面的 [sidebarDepth](./frontmatter.md#sidebardepth) frontmatter 来覆盖这个全局配置。
423423

‎docs/zh/reference/node-api.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ interface PageHeader {
480480
该 Page 的小标题。
481481

482482
- 参考:
483-
- [配置 > markdown.extractHeaders](./config.md#markdown-extractheaders)
483+
- [配置 > markdown.headers](./config.md#markdown-headers)
484484

485485
### data
486486

‎docs/zh/reference/plugin-api.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ export default {
214214
```js
215215
export default {
216216
extendsMarkdownOptions: (markdownOptions, app) => {
217-
if (markdownOptions.extractHeaders === false) return
218-
markdownOptions.extractHeaders ??= {}
219-
if (markdownOptions.extractHeaders.level) return
220-
markdownOptions.extractHeaders.level = [2, 3, 4, 5, 6]
217+
if (markdownOptions.headers === false) return
218+
markdownOptions.headers ??= {}
219+
if (markdownOptions.headers.level) return
220+
markdownOptions.headers.level = [2, 3, 4, 5, 6]
221221
},
222222
}
223223
```

‎packages/@vuepress/core/__tests__/pluginApi/createHookQueue.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('core > pluginApi > createHookQueue', () => {
107107
markdownOptions.emoji = false
108108
})
109109
const func2 = jest.fn((markdownOptions) => {
110-
markdownOptions.extractHeaders = false
110+
markdownOptions.headers = false
111111
})
112112
hook.add({
113113
pluginName: 'test1',
@@ -124,7 +124,7 @@ describe('core > pluginApi > createHookQueue', () => {
124124
expect(func1).toHaveBeenCalledWith(markdownOptions, app)
125125
expect(func2).toHaveBeenCalledTimes(1)
126126
expect(func2).toHaveBeenCalledWith(markdownOptions, app)
127-
expect(markdownOptions).toEqual({ emoji: false, extractHeaders: false })
127+
expect(markdownOptions).toEqual({ emoji: false, headers: false })
128128
})
129129

130130
it(`extendsMarkdown`, async () => {

‎packages/@vuepress/core/src/page/renderPageContent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ export const renderPageContent = async ({
5252
headers,
5353
links,
5454
sfcBlocks,
55-
title,
55+
title: frontmatter.title ?? title,
5656
}
5757
}

‎packages/@vuepress/markdown/__tests__/markdown.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ describe('@vuepress/markdown > markdown', () => {
66

77
it.todo('emoji')
88

9-
it.todo('extractHeaders')
10-
119
it.todo('links')
1210
})
1311

‎packages/@vuepress/markdown/__tests__/plugins/__snapshots__/extractHeadersPlugin.spec.ts.snap

-223
This file was deleted.

‎packages/@vuepress/markdown/__tests__/plugins/extractHeadersPlugin.spec.ts

-147
This file was deleted.

‎packages/@vuepress/markdown/__tests__/plugins/extractTitlePlugin.spec.ts

-49
This file was deleted.

‎packages/@vuepress/markdown/package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
"clean": "rimraf lib *.tsbuildinfo"
2727
},
2828
"dependencies": {
29-
"@mdit-vue/plugin-component": "^0.3.0",
30-
"@mdit-vue/plugin-sfc": "^0.3.0",
31-
"@mdit-vue/plugin-toc": "^0.3.1",
32-
"@mdit-vue/shared": "^0.3.0",
29+
"@mdit-vue/plugin-component": "^0.4.0",
30+
"@mdit-vue/plugin-headers": "^0.4.0",
31+
"@mdit-vue/plugin-sfc": "^0.4.0",
32+
"@mdit-vue/plugin-title": "^0.4.0",
33+
"@mdit-vue/plugin-toc": "^0.4.0",
34+
"@mdit-vue/shared": "^0.4.0",
3335
"@types/markdown-it": "^12.2.3",
3436
"@types/markdown-it-emoji": "^2.0.2",
3537
"@vuepress/shared": "workspace:*",

‎packages/@vuepress/markdown/src/markdown.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import {
66
codePlugin,
77
componentPlugin,
88
emojiPlugin,
9-
extractHeadersPlugin,
10-
extractTitlePlugin,
9+
headersPlugin,
1110
importCodePlugin,
1211
linksPlugin,
1312
sfcPlugin,
13+
titlePlugin,
1414
tocPlugin,
1515
} from './plugins'
1616
import type {
1717
AnchorPluginOptions,
1818
AssetsPluginOptions,
1919
CodePluginOptions,
2020
EmojiPluginOptions,
21-
ExtractHeadersPluginOptions,
21+
HeadersPluginOptions,
2222
ImportCodePluginOptions,
2323
LinksPluginOptions,
2424
SfcPluginOptions,
@@ -35,8 +35,8 @@ export const createMarkdown = ({
3535
code,
3636
component,
3737
emoji,
38-
extractHeaders,
39-
extractTitle,
38+
headers,
39+
title,
4040
importCode,
4141
links,
4242
sfc,
@@ -86,17 +86,17 @@ export const createMarkdown = ({
8686
}
8787

8888
// extract headers into env
89-
if (extractHeaders !== false) {
90-
md.use<ExtractHeadersPluginOptions>(extractHeadersPlugin, {
89+
if (headers !== false) {
90+
md.use<HeadersPluginOptions>(headersPlugin, {
9191
level: [2, 3],
9292
slugify,
93-
...extractHeaders,
93+
...headers,
9494
})
9595
}
9696

9797
// extract title into env
98-
if (extractTitle !== false) {
99-
md.use(extractTitlePlugin)
98+
if (title !== false) {
99+
md.use(titlePlugin)
100100
}
101101

102102
// =====================================================

‎packages/@vuepress/markdown/src/plugins/extractHeadersPlugin.ts

-72
This file was deleted.

‎packages/@vuepress/markdown/src/plugins/extractTitlePlugin.ts

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
export * from '@mdit-vue/plugin-component'
2+
export * from '@mdit-vue/plugin-headers'
23
export * from '@mdit-vue/plugin-sfc'
4+
export * from '@mdit-vue/plugin-title'
35
export * from '@mdit-vue/plugin-toc'
46

57
export * from './anchorPlugin'
68
export * from './assetsPlugin'
79
export * from './codePlugin'
810
export * from './emojiPlugin'
9-
export * from './extractHeadersPlugin'
10-
export * from './extractTitlePlugin'
1111
export * from './importCodePlugin'
1212
export * from './linksPlugin'

‎packages/@vuepress/markdown/src/types.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
AssetsPluginOptions,
77
CodePluginOptions,
88
EmojiPluginOptions,
9-
ExtractHeadersPluginOptions,
9+
HeadersPluginOptions,
1010
ImportCodePluginOptions,
1111
LinksPluginOptions,
1212
SfcPluginOptions,
@@ -21,8 +21,8 @@ export interface MarkdownOptions extends MarkdownIt.Options {
2121
code?: false | CodePluginOptions
2222
component?: false
2323
emoji?: false | EmojiPluginOptions
24-
extractHeaders?: false | ExtractHeadersPluginOptions
25-
extractTitle?: false
24+
headers?: false | HeadersPluginOptions
25+
title?: false
2626
importCode?: false | ImportCodePluginOptions
2727
links?: false | LinksPluginOptions
2828
sfc?: false | SfcPluginOptions
@@ -78,11 +78,6 @@ export interface MarkdownEnv extends MarkdownItEnv {
7878

7979
// Output
8080

81-
/**
82-
* Headers that extracted by extractHeadersPlugin
83-
*/
84-
headers?: MarkdownHeader[]
85-
8681
/**
8782
* Imported file that extracted by importCodePlugin
8883
*/
@@ -92,11 +87,6 @@ export interface MarkdownEnv extends MarkdownItEnv {
9287
* Links that extracted by linksPlugin
9388
*/
9489
links?: MarkdownLink[]
95-
96-
/**
97-
* Title that extracted by extractTitlePlugin
98-
*/
99-
title?: string
10090
}
10191

10292
/**

‎packages/@vuepress/theme-default/src/shared/options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ export interface DefaultThemeLocaleData extends LocaleData {
151151
* - ...
152152
*
153153
* The max value depends on which headers you have extracted
154-
* via `markdown.extractHeaders.level`.
154+
* via `markdown.headers.level`.
155155
*
156-
* The default value of `markdown.extractHeaders.level` is `[2, 3]`,
156+
* The default value of `markdown.headers.level` is `[2, 3]`,
157157
* so the default max value of `sidebarDepth` is `2`
158158
*/
159159
sidebarDepth?: number

‎pnpm-lock.yaml

+38-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.