@mdit-vue/plugin-toc 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/LICENSE +21 -0
- package/README.md +147 -0
- package/build.config.ts +11 -0
- package/dist/index.cjs +107 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.mjs +101 -0
- package/package.json +51 -0
- package/src/create-render-headers.ts +43 -0
- package/src/create-toc-block-rule.ts +62 -0
- package/src/index.ts +3 -0
- package/src/toc-plugin.ts +79 -0
- package/src/types.ts +66 -0
- package/tests/__snapshots__/toc-plugin.spec.ts.snap +161 -0
- package/tests/toc-plugin.spec.ts +193 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
# [0.3.0](https://github.com/mdit-vue/mdit-vue/compare/v0.2.0...v0.3.0) (2022-06-25)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **plugin-toc:** add toc plugin ([e64cc2c](https://github.com/mdit-vue/mdit-vue/commit/e64cc2ce3bbc89999d373f8a00fb3a049d6af58f))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present, mdit-vue Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @mdit-vue/plugin-toc
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@mdit-vue/plugin-toc)
|
|
4
|
+
|
|
5
|
+
A [markdown-it](https://github.com/markdown-it/markdown-it) plugin to generate table-of-contents (TOC) that compatible with [Vue.js](https://vuejs.org).
|
|
6
|
+
|
|
7
|
+
This plugin is basically a fork of [markdown-it-toc-done-right](https://github.com/nagaozen/markdown-it-toc-done-right), with following enhancement:
|
|
8
|
+
|
|
9
|
+
- Allows `html_inline` tags in headings to support vue components.
|
|
10
|
+
- Supports [containerTag](#containertag), [listTag](#listtag) and [linkTag](#linktag).
|
|
11
|
+
- Only allows array in [level](#level) option.
|
|
12
|
+
- Code refactor and optimizations.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
npm i @mdit-vue/plugin-toc
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import MarkdownIt from 'markdown-it';
|
|
24
|
+
import { tocPlugin } from '@mdit-vue/plugin-toc';
|
|
25
|
+
|
|
26
|
+
const md = MarkdownIt({ html: true }).use(tocPlugin, {
|
|
27
|
+
// options
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Options
|
|
32
|
+
|
|
33
|
+
### pattern
|
|
34
|
+
|
|
35
|
+
- Type: `RegExp`
|
|
36
|
+
|
|
37
|
+
- Default: `/^\[\[toc\]\]$/i`
|
|
38
|
+
|
|
39
|
+
- Details:
|
|
40
|
+
|
|
41
|
+
The pattern serving as the TOC placeholder in your markdown.
|
|
42
|
+
|
|
43
|
+
### slugify
|
|
44
|
+
|
|
45
|
+
- Type: `(str: string) => string`
|
|
46
|
+
|
|
47
|
+
- Default: `slugify` from `@mdit-vue/shared`
|
|
48
|
+
|
|
49
|
+
- Details:
|
|
50
|
+
|
|
51
|
+
A custom slugification function.
|
|
52
|
+
|
|
53
|
+
The default slugify function comes from [@mdit-vue/shared](https://github.com/mdit-vue/mdit-vue/tree/main/packages/shared) package.
|
|
54
|
+
|
|
55
|
+
When using this plugin with [markdown-it-anchor](https://github.com/valeriangalliat/markdown-it-anchor) (recommended), this option will be ignored because the `id` of the headings have already been determined by markdown-it-anchor's `slugify` option.
|
|
56
|
+
|
|
57
|
+
### format
|
|
58
|
+
|
|
59
|
+
- Type: `(str: string) => string`
|
|
60
|
+
|
|
61
|
+
- Details:
|
|
62
|
+
|
|
63
|
+
A function for formatting headings.
|
|
64
|
+
|
|
65
|
+
### level
|
|
66
|
+
|
|
67
|
+
- Type: `number[]`
|
|
68
|
+
|
|
69
|
+
- Default: `[2, 3]`
|
|
70
|
+
|
|
71
|
+
- Details:
|
|
72
|
+
|
|
73
|
+
Heading level that going to be included in the TOC.
|
|
74
|
+
|
|
75
|
+
When using this plugin with [markdown-it-anchor](https://github.com/valeriangalliat/markdown-it-anchor) (recommended), this option should be a subset of markdown-it-anchor's `level` option to ensure the target links are existed.
|
|
76
|
+
|
|
77
|
+
### containerTag
|
|
78
|
+
|
|
79
|
+
- Type: `string`
|
|
80
|
+
|
|
81
|
+
- Default: `'nav'`
|
|
82
|
+
|
|
83
|
+
- Details:
|
|
84
|
+
|
|
85
|
+
HTML tag of the TOC container.
|
|
86
|
+
|
|
87
|
+
### containerClass
|
|
88
|
+
|
|
89
|
+
- Type: `string`
|
|
90
|
+
|
|
91
|
+
- Default: `'table-of-contents'`
|
|
92
|
+
|
|
93
|
+
- Details:
|
|
94
|
+
|
|
95
|
+
The class for the TOC container.
|
|
96
|
+
|
|
97
|
+
### listTag
|
|
98
|
+
|
|
99
|
+
- Type: `'ul' | 'ol'`
|
|
100
|
+
|
|
101
|
+
- Default: `'ul'`
|
|
102
|
+
|
|
103
|
+
- Details:
|
|
104
|
+
|
|
105
|
+
HTML tag of the TOC list.
|
|
106
|
+
|
|
107
|
+
### listClass
|
|
108
|
+
|
|
109
|
+
- Type: `string`
|
|
110
|
+
|
|
111
|
+
- Default: `''`
|
|
112
|
+
|
|
113
|
+
- Details:
|
|
114
|
+
|
|
115
|
+
The class for the TOC list.
|
|
116
|
+
|
|
117
|
+
### itemClass
|
|
118
|
+
|
|
119
|
+
- Type: `string`
|
|
120
|
+
|
|
121
|
+
- Default: `''`
|
|
122
|
+
|
|
123
|
+
- Details:
|
|
124
|
+
|
|
125
|
+
The class for the `<li>` tag.
|
|
126
|
+
|
|
127
|
+
### linkTag
|
|
128
|
+
|
|
129
|
+
- Type: `'a' | 'router-link'`
|
|
130
|
+
|
|
131
|
+
- Default: `'a'`
|
|
132
|
+
|
|
133
|
+
- Details:
|
|
134
|
+
|
|
135
|
+
The tag of the link inside the `<li>` tag.
|
|
136
|
+
|
|
137
|
+
You can set this option to render to links as [vue-router's router-link](https://router.vuejs.org/guide/#html).
|
|
138
|
+
|
|
139
|
+
### linkClass
|
|
140
|
+
|
|
141
|
+
- Type: `string`
|
|
142
|
+
|
|
143
|
+
- Default: `''`
|
|
144
|
+
|
|
145
|
+
- Details:
|
|
146
|
+
|
|
147
|
+
The class for the link inside the `<li>` tag.
|
package/build.config.ts
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const shared = require('@mdit-vue/shared');
|
|
6
|
+
|
|
7
|
+
const createRenderHeaders = ({
|
|
8
|
+
listTag,
|
|
9
|
+
listClass,
|
|
10
|
+
itemClass,
|
|
11
|
+
linkTag,
|
|
12
|
+
linkClass
|
|
13
|
+
}) => {
|
|
14
|
+
const listTagString = shared.htmlEscape(listTag);
|
|
15
|
+
const listClassString = listClass ? ` class="${shared.htmlEscape(listClass)}"` : "";
|
|
16
|
+
const itemTagString = "li";
|
|
17
|
+
const itemClassString = itemClass ? ` class="${shared.htmlEscape(itemClass)}"` : "";
|
|
18
|
+
const linkTagString = shared.htmlEscape(linkTag);
|
|
19
|
+
const linkClassString = linkClass ? ` class="${shared.htmlEscape(linkClass)}"` : "";
|
|
20
|
+
const linkTo = (slug) => linkTag === "router-link" ? ` to="#${slug}"` : ` href="#${slug}"`;
|
|
21
|
+
const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map((header) => `<${itemTagString}${itemClassString}${itemClassString}><${linkTagString}${linkClassString}${linkTo(header.slug)}>${header.title}</${linkTagString}>${header.children.length > 0 ? renderHeaders(header.children) : ""}</${itemTagString}>`).join("")}</${listTagString}>`;
|
|
22
|
+
return renderHeaders;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const createTocBlockRule = ({
|
|
26
|
+
pattern,
|
|
27
|
+
containerTag,
|
|
28
|
+
containerClass
|
|
29
|
+
}) => {
|
|
30
|
+
return (state, startLine, endLine, silent) => {
|
|
31
|
+
if (state.sCount[startLine] - state.blkIndent >= 4) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const pos = state.bMarks[startLine] + state.tShift[startLine];
|
|
35
|
+
const max = state.eMarks[startLine];
|
|
36
|
+
const lineFirstToken = state.src.slice(pos, max).split(" ")[0];
|
|
37
|
+
if (!pattern.test(lineFirstToken))
|
|
38
|
+
return false;
|
|
39
|
+
if (silent)
|
|
40
|
+
return true;
|
|
41
|
+
state.line = startLine + 1;
|
|
42
|
+
const tokenOpen = state.push("toc_open", containerTag, 1);
|
|
43
|
+
tokenOpen.markup = "";
|
|
44
|
+
tokenOpen.map = [startLine, state.line];
|
|
45
|
+
if (containerClass) {
|
|
46
|
+
tokenOpen.attrSet("class", containerClass);
|
|
47
|
+
}
|
|
48
|
+
const tokenBody = state.push("toc_body", "", 0);
|
|
49
|
+
tokenBody.markup = lineFirstToken;
|
|
50
|
+
tokenBody.map = [startLine, state.line];
|
|
51
|
+
tokenBody.hidden = true;
|
|
52
|
+
const tokenClose = state.push("toc_close", containerTag, -1);
|
|
53
|
+
tokenClose.markup = "";
|
|
54
|
+
tokenBody.map = [startLine, state.line];
|
|
55
|
+
return true;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const tocPlugin = (md, {
|
|
60
|
+
pattern = /^\[\[toc\]\]$/i,
|
|
61
|
+
slugify = shared.slugify,
|
|
62
|
+
format,
|
|
63
|
+
level = [2, 3],
|
|
64
|
+
containerTag = "nav",
|
|
65
|
+
containerClass = "table-of-contents",
|
|
66
|
+
listTag = "ul",
|
|
67
|
+
listClass = "",
|
|
68
|
+
itemClass = "",
|
|
69
|
+
linkTag = "a",
|
|
70
|
+
linkClass = ""
|
|
71
|
+
} = {}) => {
|
|
72
|
+
let headers;
|
|
73
|
+
md.core.ruler.push("resolveTocHeaders", (state) => {
|
|
74
|
+
headers = shared.resolveHeadersFromTokens(state.tokens, {
|
|
75
|
+
level,
|
|
76
|
+
shouldAllowHtml: true,
|
|
77
|
+
shouldEscapeText: true,
|
|
78
|
+
slugify,
|
|
79
|
+
format
|
|
80
|
+
});
|
|
81
|
+
return true;
|
|
82
|
+
});
|
|
83
|
+
md.block.ruler.before("heading", "toc", createTocBlockRule({
|
|
84
|
+
pattern,
|
|
85
|
+
containerTag,
|
|
86
|
+
containerClass
|
|
87
|
+
}), {
|
|
88
|
+
alt: ["paragraph", "reference", "blockquote"]
|
|
89
|
+
});
|
|
90
|
+
const renderHeaders = createRenderHeaders({
|
|
91
|
+
listTag,
|
|
92
|
+
listClass,
|
|
93
|
+
itemClass,
|
|
94
|
+
linkTag,
|
|
95
|
+
linkClass
|
|
96
|
+
});
|
|
97
|
+
md.renderer.rules.toc_body = () => {
|
|
98
|
+
if (!headers) {
|
|
99
|
+
return "";
|
|
100
|
+
}
|
|
101
|
+
return renderHeaders(headers);
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
exports.createRenderHeaders = createRenderHeaders;
|
|
106
|
+
exports.createTocBlockRule = createTocBlockRule;
|
|
107
|
+
exports.tocPlugin = tocPlugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { MarkdownItHeader } from '@mdit-vue/shared';
|
|
2
|
+
import { RuleBlock } from 'markdown-it/lib/parser_block';
|
|
3
|
+
import { PluginWithOptions } from 'markdown-it';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options of @mdit-vue/plugin-toc
|
|
7
|
+
*/
|
|
8
|
+
interface TocPluginOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The pattern serving as the TOC placeholder in your markdown
|
|
11
|
+
*/
|
|
12
|
+
pattern?: RegExp;
|
|
13
|
+
/**
|
|
14
|
+
* A custom slugification function
|
|
15
|
+
*
|
|
16
|
+
* Should use the same slugify function with markdown-it-anchor
|
|
17
|
+
* to ensure the link is matched
|
|
18
|
+
*/
|
|
19
|
+
slugify?: (str: string) => string;
|
|
20
|
+
/**
|
|
21
|
+
* A function for formatting headings
|
|
22
|
+
*/
|
|
23
|
+
format?: (str: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Heading level that going to be included in the TOC
|
|
26
|
+
*
|
|
27
|
+
* Should be a subset of markdown-it-anchor's `level` option
|
|
28
|
+
* to ensure the link is existed
|
|
29
|
+
*/
|
|
30
|
+
level?: number[];
|
|
31
|
+
/**
|
|
32
|
+
* HTML tag of the TOC container
|
|
33
|
+
*/
|
|
34
|
+
containerTag?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The class for the TOC container
|
|
37
|
+
*/
|
|
38
|
+
containerClass?: string;
|
|
39
|
+
/**
|
|
40
|
+
* HTML tag of the TOC list
|
|
41
|
+
*/
|
|
42
|
+
listTag?: 'ul' | 'ol';
|
|
43
|
+
/**
|
|
44
|
+
* The class for the TOC list
|
|
45
|
+
*/
|
|
46
|
+
listClass?: string;
|
|
47
|
+
/**
|
|
48
|
+
* The class for the `<li>` tag
|
|
49
|
+
*/
|
|
50
|
+
itemClass?: string;
|
|
51
|
+
/**
|
|
52
|
+
* The tag of the link inside `<li>` tag
|
|
53
|
+
*/
|
|
54
|
+
linkTag?: 'a' | 'router-link';
|
|
55
|
+
/**
|
|
56
|
+
* The class for the link inside the `<li>` tag
|
|
57
|
+
*/
|
|
58
|
+
linkClass?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare type RenderHeadersFn = (headers: MarkdownItHeader[]) => string;
|
|
62
|
+
declare const createRenderHeaders: ({ listTag, listClass, itemClass, linkTag, linkClass, }: Pick<Required<TocPluginOptions>, 'listTag' | 'listClass' | 'itemClass' | 'linkTag' | 'linkClass'>) => RenderHeadersFn;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Forked and modified from markdown-it-toc-done-right
|
|
66
|
+
*
|
|
67
|
+
* - remove the `inlineOptions` support
|
|
68
|
+
* - use markdown-it default renderer to render token whenever possible
|
|
69
|
+
*
|
|
70
|
+
* @see https://github.com/nagaozen/markdown-it-toc-done-right
|
|
71
|
+
*/
|
|
72
|
+
declare const createTocBlockRule: ({ pattern, containerTag, containerClass, }: Pick<Required<TocPluginOptions>, 'pattern' | 'containerTag' | 'containerClass'>) => RuleBlock;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Generate table of contents
|
|
76
|
+
*
|
|
77
|
+
* Forked and modified from markdown-it-toc-done-right:
|
|
78
|
+
*
|
|
79
|
+
* @see https://github.com/nagaozen/markdown-it-toc-done-right
|
|
80
|
+
*/
|
|
81
|
+
declare const tocPlugin: PluginWithOptions<TocPluginOptions>;
|
|
82
|
+
|
|
83
|
+
export { createRenderHeaders, createTocBlockRule, tocPlugin };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { htmlEscape, resolveHeadersFromTokens, slugify } from '@mdit-vue/shared';
|
|
2
|
+
|
|
3
|
+
const createRenderHeaders = ({
|
|
4
|
+
listTag,
|
|
5
|
+
listClass,
|
|
6
|
+
itemClass,
|
|
7
|
+
linkTag,
|
|
8
|
+
linkClass
|
|
9
|
+
}) => {
|
|
10
|
+
const listTagString = htmlEscape(listTag);
|
|
11
|
+
const listClassString = listClass ? ` class="${htmlEscape(listClass)}"` : "";
|
|
12
|
+
const itemTagString = "li";
|
|
13
|
+
const itemClassString = itemClass ? ` class="${htmlEscape(itemClass)}"` : "";
|
|
14
|
+
const linkTagString = htmlEscape(linkTag);
|
|
15
|
+
const linkClassString = linkClass ? ` class="${htmlEscape(linkClass)}"` : "";
|
|
16
|
+
const linkTo = (slug) => linkTag === "router-link" ? ` to="#${slug}"` : ` href="#${slug}"`;
|
|
17
|
+
const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map((header) => `<${itemTagString}${itemClassString}${itemClassString}><${linkTagString}${linkClassString}${linkTo(header.slug)}>${header.title}</${linkTagString}>${header.children.length > 0 ? renderHeaders(header.children) : ""}</${itemTagString}>`).join("")}</${listTagString}>`;
|
|
18
|
+
return renderHeaders;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const createTocBlockRule = ({
|
|
22
|
+
pattern,
|
|
23
|
+
containerTag,
|
|
24
|
+
containerClass
|
|
25
|
+
}) => {
|
|
26
|
+
return (state, startLine, endLine, silent) => {
|
|
27
|
+
if (state.sCount[startLine] - state.blkIndent >= 4) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
const pos = state.bMarks[startLine] + state.tShift[startLine];
|
|
31
|
+
const max = state.eMarks[startLine];
|
|
32
|
+
const lineFirstToken = state.src.slice(pos, max).split(" ")[0];
|
|
33
|
+
if (!pattern.test(lineFirstToken))
|
|
34
|
+
return false;
|
|
35
|
+
if (silent)
|
|
36
|
+
return true;
|
|
37
|
+
state.line = startLine + 1;
|
|
38
|
+
const tokenOpen = state.push("toc_open", containerTag, 1);
|
|
39
|
+
tokenOpen.markup = "";
|
|
40
|
+
tokenOpen.map = [startLine, state.line];
|
|
41
|
+
if (containerClass) {
|
|
42
|
+
tokenOpen.attrSet("class", containerClass);
|
|
43
|
+
}
|
|
44
|
+
const tokenBody = state.push("toc_body", "", 0);
|
|
45
|
+
tokenBody.markup = lineFirstToken;
|
|
46
|
+
tokenBody.map = [startLine, state.line];
|
|
47
|
+
tokenBody.hidden = true;
|
|
48
|
+
const tokenClose = state.push("toc_close", containerTag, -1);
|
|
49
|
+
tokenClose.markup = "";
|
|
50
|
+
tokenBody.map = [startLine, state.line];
|
|
51
|
+
return true;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const tocPlugin = (md, {
|
|
56
|
+
pattern = /^\[\[toc\]\]$/i,
|
|
57
|
+
slugify: slugify$1 = slugify,
|
|
58
|
+
format,
|
|
59
|
+
level = [2, 3],
|
|
60
|
+
containerTag = "nav",
|
|
61
|
+
containerClass = "table-of-contents",
|
|
62
|
+
listTag = "ul",
|
|
63
|
+
listClass = "",
|
|
64
|
+
itemClass = "",
|
|
65
|
+
linkTag = "a",
|
|
66
|
+
linkClass = ""
|
|
67
|
+
} = {}) => {
|
|
68
|
+
let headers;
|
|
69
|
+
md.core.ruler.push("resolveTocHeaders", (state) => {
|
|
70
|
+
headers = resolveHeadersFromTokens(state.tokens, {
|
|
71
|
+
level,
|
|
72
|
+
shouldAllowHtml: true,
|
|
73
|
+
shouldEscapeText: true,
|
|
74
|
+
slugify: slugify$1,
|
|
75
|
+
format
|
|
76
|
+
});
|
|
77
|
+
return true;
|
|
78
|
+
});
|
|
79
|
+
md.block.ruler.before("heading", "toc", createTocBlockRule({
|
|
80
|
+
pattern,
|
|
81
|
+
containerTag,
|
|
82
|
+
containerClass
|
|
83
|
+
}), {
|
|
84
|
+
alt: ["paragraph", "reference", "blockquote"]
|
|
85
|
+
});
|
|
86
|
+
const renderHeaders = createRenderHeaders({
|
|
87
|
+
listTag,
|
|
88
|
+
listClass,
|
|
89
|
+
itemClass,
|
|
90
|
+
linkTag,
|
|
91
|
+
linkClass
|
|
92
|
+
});
|
|
93
|
+
md.renderer.rules.toc_body = () => {
|
|
94
|
+
if (!headers) {
|
|
95
|
+
return "";
|
|
96
|
+
}
|
|
97
|
+
return renderHeaders(headers);
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { createRenderHeaders, createTocBlockRule, tocPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdit-vue/plugin-toc",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "A markdown-it plugin to generate table-of-contents that compatible with Vue.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"markdown-it",
|
|
7
|
+
"markdown-it-plugin",
|
|
8
|
+
"vue",
|
|
9
|
+
"toc",
|
|
10
|
+
"table-of-contents"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/mdit-vue",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/mdit-vue/mdit-vue/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/mdit-vue/mdit-vue.git"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": "meteorlxy <meteor.lxy@foxmail.com>",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.mjs",
|
|
26
|
+
"require": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/index.cjs",
|
|
30
|
+
"module": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "unbuild",
|
|
34
|
+
"test": "vitest"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@mdit-vue/shared": "0.3.0",
|
|
38
|
+
"@types/markdown-it": "^12.2.3",
|
|
39
|
+
"markdown-it": "^13.0.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"c8": "^7.11.3",
|
|
43
|
+
"markdown-it-anchor": "^8.6.4",
|
|
44
|
+
"unbuild": "^0.7.4",
|
|
45
|
+
"vitest": "^0.15.2"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "bcbfd4705d747ee71d6c7c32f8be6155261e9c90"
|
|
51
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { htmlEscape } from '@mdit-vue/shared';
|
|
2
|
+
import type { MarkdownItHeader } from '@mdit-vue/shared';
|
|
3
|
+
import type { TocPluginOptions } from './types';
|
|
4
|
+
|
|
5
|
+
type RenderHeadersFn = (headers: MarkdownItHeader[]) => string;
|
|
6
|
+
|
|
7
|
+
export const createRenderHeaders = ({
|
|
8
|
+
listTag,
|
|
9
|
+
listClass,
|
|
10
|
+
itemClass,
|
|
11
|
+
linkTag,
|
|
12
|
+
linkClass,
|
|
13
|
+
}: Pick<
|
|
14
|
+
Required<TocPluginOptions>,
|
|
15
|
+
'listTag' | 'listClass' | 'itemClass' | 'linkTag' | 'linkClass'
|
|
16
|
+
>): RenderHeadersFn => {
|
|
17
|
+
const listTagString = htmlEscape(listTag);
|
|
18
|
+
const listClassString = listClass ? ` class="${htmlEscape(listClass)}"` : '';
|
|
19
|
+
const itemTagString = 'li';
|
|
20
|
+
const itemClassString = itemClass ? ` class="${htmlEscape(itemClass)}"` : '';
|
|
21
|
+
const linkTagString = htmlEscape(linkTag);
|
|
22
|
+
const linkClassString = linkClass ? ` class="${htmlEscape(linkClass)}"` : '';
|
|
23
|
+
const linkTo = (slug: string): string =>
|
|
24
|
+
linkTag === 'router-link' ? ` to="#${slug}"` : ` href="#${slug}"`;
|
|
25
|
+
|
|
26
|
+
const renderHeaders: RenderHeadersFn = (headers) => `\
|
|
27
|
+
<${listTagString}${listClassString}>\
|
|
28
|
+
${headers
|
|
29
|
+
.map(
|
|
30
|
+
(header) => `\
|
|
31
|
+
<${itemTagString}${itemClassString}${itemClassString}>\
|
|
32
|
+
<${linkTagString}${linkClassString}${linkTo(header.slug)}>\
|
|
33
|
+
${header.title}\
|
|
34
|
+
</${linkTagString}>\
|
|
35
|
+
${header.children.length > 0 ? renderHeaders(header.children) : ''}\
|
|
36
|
+
</${itemTagString}>\
|
|
37
|
+
`,
|
|
38
|
+
)
|
|
39
|
+
.join('')}\
|
|
40
|
+
</${listTagString}>`;
|
|
41
|
+
|
|
42
|
+
return renderHeaders;
|
|
43
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { RuleBlock } from 'markdown-it/lib/parser_block';
|
|
2
|
+
import type { TocPluginOptions } from './types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Forked and modified from markdown-it-toc-done-right
|
|
6
|
+
*
|
|
7
|
+
* - remove the `inlineOptions` support
|
|
8
|
+
* - use markdown-it default renderer to render token whenever possible
|
|
9
|
+
*
|
|
10
|
+
* @see https://github.com/nagaozen/markdown-it-toc-done-right
|
|
11
|
+
*/
|
|
12
|
+
export const createTocBlockRule = ({
|
|
13
|
+
pattern,
|
|
14
|
+
containerTag,
|
|
15
|
+
containerClass,
|
|
16
|
+
}: Pick<
|
|
17
|
+
Required<TocPluginOptions>,
|
|
18
|
+
'pattern' | 'containerTag' | 'containerClass'
|
|
19
|
+
>): RuleBlock => {
|
|
20
|
+
return (state, startLine, endLine, silent): boolean => {
|
|
21
|
+
// if it's indented more than 3 spaces, it should be a code block
|
|
22
|
+
if (state.sCount[startLine] - state.blkIndent >= 4) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const pos = state.bMarks[startLine] + state.tShift[startLine];
|
|
27
|
+
const max = state.eMarks[startLine];
|
|
28
|
+
|
|
29
|
+
// use whitespace as a line tokenizer and extract the first token
|
|
30
|
+
// to test against the placeholder anchored pattern, rejecting if false
|
|
31
|
+
const lineFirstToken = state.src.slice(pos, max).split(' ')[0];
|
|
32
|
+
if (!pattern.test(lineFirstToken)) return false;
|
|
33
|
+
|
|
34
|
+
if (silent) return true;
|
|
35
|
+
|
|
36
|
+
state.line = startLine + 1;
|
|
37
|
+
|
|
38
|
+
// generate toc_open token
|
|
39
|
+
// will be rendered by markdown-it default renderer
|
|
40
|
+
const tokenOpen = state.push('toc_open', containerTag, 1);
|
|
41
|
+
tokenOpen.markup = '';
|
|
42
|
+
tokenOpen.map = [startLine, state.line];
|
|
43
|
+
if (containerClass) {
|
|
44
|
+
tokenOpen.attrSet('class', containerClass);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// generate toc_body token
|
|
48
|
+
// will be rendered by our custom renderer
|
|
49
|
+
const tokenBody = state.push('toc_body', '', 0);
|
|
50
|
+
tokenBody.markup = lineFirstToken;
|
|
51
|
+
tokenBody.map = [startLine, state.line];
|
|
52
|
+
tokenBody.hidden = true;
|
|
53
|
+
|
|
54
|
+
// generate toc_close token
|
|
55
|
+
// will be rendered by markdown-it default renderer
|
|
56
|
+
const tokenClose = state.push('toc_close', containerTag, -1);
|
|
57
|
+
tokenClose.markup = '';
|
|
58
|
+
tokenBody.map = [startLine, state.line];
|
|
59
|
+
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import {
|
|
2
|
+
slugify as defaultSlugify,
|
|
3
|
+
resolveHeadersFromTokens,
|
|
4
|
+
} from '@mdit-vue/shared';
|
|
5
|
+
import type { MarkdownItHeader } from '@mdit-vue/shared';
|
|
6
|
+
import type { PluginWithOptions } from 'markdown-it';
|
|
7
|
+
import { createRenderHeaders } from './create-render-headers';
|
|
8
|
+
import { createTocBlockRule } from './create-toc-block-rule';
|
|
9
|
+
import type { TocPluginOptions } from './types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Generate table of contents
|
|
13
|
+
*
|
|
14
|
+
* Forked and modified from markdown-it-toc-done-right:
|
|
15
|
+
*
|
|
16
|
+
* @see https://github.com/nagaozen/markdown-it-toc-done-right
|
|
17
|
+
*/
|
|
18
|
+
export const tocPlugin: PluginWithOptions<TocPluginOptions> = (
|
|
19
|
+
md,
|
|
20
|
+
{
|
|
21
|
+
pattern = /^\[\[toc\]\]$/i,
|
|
22
|
+
slugify = defaultSlugify,
|
|
23
|
+
format,
|
|
24
|
+
level = [2, 3],
|
|
25
|
+
containerTag = 'nav',
|
|
26
|
+
containerClass = 'table-of-contents',
|
|
27
|
+
listTag = 'ul',
|
|
28
|
+
listClass = '',
|
|
29
|
+
itemClass = '',
|
|
30
|
+
linkTag = 'a',
|
|
31
|
+
linkClass = '',
|
|
32
|
+
}: TocPluginOptions = {},
|
|
33
|
+
): void => {
|
|
34
|
+
let headers: MarkdownItHeader[];
|
|
35
|
+
|
|
36
|
+
// push the rule to the end of the chain
|
|
37
|
+
// resolve headers from the parsed tokens
|
|
38
|
+
md.core.ruler.push('resolveTocHeaders', (state) => {
|
|
39
|
+
headers = resolveHeadersFromTokens(state.tokens, {
|
|
40
|
+
level,
|
|
41
|
+
shouldAllowHtml: true,
|
|
42
|
+
shouldEscapeText: true,
|
|
43
|
+
slugify,
|
|
44
|
+
format,
|
|
45
|
+
});
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// add toc syntax as a block rule
|
|
50
|
+
md.block.ruler.before(
|
|
51
|
+
'heading',
|
|
52
|
+
'toc',
|
|
53
|
+
createTocBlockRule({
|
|
54
|
+
pattern,
|
|
55
|
+
containerTag,
|
|
56
|
+
containerClass,
|
|
57
|
+
}),
|
|
58
|
+
{
|
|
59
|
+
alt: ['paragraph', 'reference', 'blockquote'],
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
const renderHeaders = createRenderHeaders({
|
|
64
|
+
listTag,
|
|
65
|
+
listClass,
|
|
66
|
+
itemClass,
|
|
67
|
+
linkTag,
|
|
68
|
+
linkClass,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// custom toc_body render rule
|
|
72
|
+
md.renderer.rules.toc_body = () => {
|
|
73
|
+
/* istanbul ignore if */
|
|
74
|
+
if (!headers) {
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
return renderHeaders(headers);
|
|
78
|
+
};
|
|
79
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options of @mdit-vue/plugin-toc
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface TocPluginOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The pattern serving as the TOC placeholder in your markdown
|
|
8
|
+
*/
|
|
9
|
+
pattern?: RegExp;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A custom slugification function
|
|
13
|
+
*
|
|
14
|
+
* Should use the same slugify function with markdown-it-anchor
|
|
15
|
+
* to ensure the link is matched
|
|
16
|
+
*/
|
|
17
|
+
slugify?: (str: string) => string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A function for formatting headings
|
|
21
|
+
*/
|
|
22
|
+
format?: (str: string) => string;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Heading level that going to be included in the TOC
|
|
26
|
+
*
|
|
27
|
+
* Should be a subset of markdown-it-anchor's `level` option
|
|
28
|
+
* to ensure the link is existed
|
|
29
|
+
*/
|
|
30
|
+
level?: number[];
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* HTML tag of the TOC container
|
|
34
|
+
*/
|
|
35
|
+
containerTag?: string;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The class for the TOC container
|
|
39
|
+
*/
|
|
40
|
+
containerClass?: string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* HTML tag of the TOC list
|
|
44
|
+
*/
|
|
45
|
+
listTag?: 'ul' | 'ol';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The class for the TOC list
|
|
49
|
+
*/
|
|
50
|
+
listClass?: string;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The class for the `<li>` tag
|
|
54
|
+
*/
|
|
55
|
+
itemClass?: string;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The tag of the link inside `<li>` tag
|
|
59
|
+
*/
|
|
60
|
+
linkTag?: 'a' | 'router-link';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The class for the link inside the `<li>` tag
|
|
64
|
+
*/
|
|
65
|
+
linkClass?: string;
|
|
66
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Vitest Snapshot v1
|
|
2
|
+
|
|
3
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render class name correctly > complexTree 1`] = `
|
|
4
|
+
"<nav><ul class=\\"toc-list\\"><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-1\\">s1-1</a><ul class=\\"toc-list\\"><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-1-1\\">s1-1-1</a></li><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-1-2\\">s1-1-2</a></li><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-1-3\\">s1-1-3</a></li></ul></li><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-2\\">s1-2</a></li><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-3\\">s1-3</a><ul class=\\"toc-list\\"><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#s1-3-2\\">s1-3-2</a></li></ul></li></ul></nav>
|
|
5
|
+
<h1>s1</h1>
|
|
6
|
+
<h2>s1-1</h2>
|
|
7
|
+
<h3>s1-1-1</h3>
|
|
8
|
+
<h4>s1-1-1-1</h4>
|
|
9
|
+
<h3>s1-1-2</h3>
|
|
10
|
+
<h3>s1-1-3</h3>
|
|
11
|
+
<h4>s1-1-3-1</h4>
|
|
12
|
+
<h2>s1-2</h2>
|
|
13
|
+
<h2>s1-3</h2>
|
|
14
|
+
<h3>s1-3-2</h3>
|
|
15
|
+
<h4>s1-3-2-1</h4>
|
|
16
|
+
<h5>s1-3-2-1-1</h5>
|
|
17
|
+
<h5>s1-3-2-1-2</h5>
|
|
18
|
+
"
|
|
19
|
+
`;
|
|
20
|
+
|
|
21
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render class name correctly > reversedTree 1`] = `
|
|
22
|
+
"<nav><ul class=\\"toc-list\\"><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#h3\\">h3</a></li><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#h2\\">h2</a></li></ul></nav>
|
|
23
|
+
<h6>h6</h6>
|
|
24
|
+
<h5>h5</h5>
|
|
25
|
+
<h4>h4</h4>
|
|
26
|
+
<h3>h3</h3>
|
|
27
|
+
<h2>h2</h2>
|
|
28
|
+
<h1>h1</h1>
|
|
29
|
+
"
|
|
30
|
+
`;
|
|
31
|
+
|
|
32
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render class name correctly > simpleTree 1`] = `
|
|
33
|
+
"<nav><ul class=\\"toc-list\\"><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#h2\\">h2</a><ul class=\\"toc-list\\"><li class=\\"toc-item\\" class=\\"toc-item\\"><a class=\\"toc-link\\" href=\\"#h3\\">h3</a></li></ul></li></ul></nav>
|
|
34
|
+
<h1>h1</h1>
|
|
35
|
+
<h2>h2</h2>
|
|
36
|
+
<h3>h3</h3>
|
|
37
|
+
<h4>h4</h4>
|
|
38
|
+
<h5>h5</h5>
|
|
39
|
+
<h6>h6</h6>
|
|
40
|
+
"
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc (h1, h2, h3, h4) > complexTree 1`] = `
|
|
44
|
+
"<nav class=\\"table-of-contents\\"><ul><li><a href=\\"#s1\\">s1</a><ul><li><a href=\\"#s1-1\\">s1-1</a><ul><li><a href=\\"#s1-1-1\\">s1-1-1</a><ul><li><a href=\\"#s1-1-1-1\\">s1-1-1-1</a></li></ul></li><li><a href=\\"#s1-1-2\\">s1-1-2</a></li><li><a href=\\"#s1-1-3\\">s1-1-3</a><ul><li><a href=\\"#s1-1-3-1\\">s1-1-3-1</a></li></ul></li></ul></li><li><a href=\\"#s1-2\\">s1-2</a></li><li><a href=\\"#s1-3\\">s1-3</a><ul><li><a href=\\"#s1-3-2\\">s1-3-2</a><ul><li><a href=\\"#s1-3-2-1\\">s1-3-2-1</a></li></ul></li></ul></li></ul></li></ul></nav>
|
|
45
|
+
<h1>s1</h1>
|
|
46
|
+
<h2>s1-1</h2>
|
|
47
|
+
<h3>s1-1-1</h3>
|
|
48
|
+
<h4>s1-1-1-1</h4>
|
|
49
|
+
<h3>s1-1-2</h3>
|
|
50
|
+
<h3>s1-1-3</h3>
|
|
51
|
+
<h4>s1-1-3-1</h4>
|
|
52
|
+
<h2>s1-2</h2>
|
|
53
|
+
<h2>s1-3</h2>
|
|
54
|
+
<h3>s1-3-2</h3>
|
|
55
|
+
<h4>s1-3-2-1</h4>
|
|
56
|
+
<h5>s1-3-2-1-1</h5>
|
|
57
|
+
<h5>s1-3-2-1-2</h5>
|
|
58
|
+
"
|
|
59
|
+
`;
|
|
60
|
+
|
|
61
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc (h1, h2, h3, h4) > reversedTree 1`] = `
|
|
62
|
+
"<nav class=\\"table-of-contents\\"><ul><li><a href=\\"#h4\\">h4</a></li><li><a href=\\"#h3\\">h3</a></li><li><a href=\\"#h2\\">h2</a></li><li><a href=\\"#h1\\">h1</a></li></ul></nav>
|
|
63
|
+
<h6>h6</h6>
|
|
64
|
+
<h5>h5</h5>
|
|
65
|
+
<h4>h4</h4>
|
|
66
|
+
<h3>h3</h3>
|
|
67
|
+
<h2>h2</h2>
|
|
68
|
+
<h1>h1</h1>
|
|
69
|
+
"
|
|
70
|
+
`;
|
|
71
|
+
|
|
72
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc (h1, h2, h3, h4) > simpleTree 1`] = `
|
|
73
|
+
"<nav class=\\"table-of-contents\\"><ul><li><a href=\\"#h1\\">h1</a><ul><li><a href=\\"#h2\\">h2</a><ul><li><a href=\\"#h3\\">h3</a><ul><li><a href=\\"#h4\\">h4</a></li></ul></li></ul></li></ul></li></ul></nav>
|
|
74
|
+
<h1>h1</h1>
|
|
75
|
+
<h2>h2</h2>
|
|
76
|
+
<h3>h3</h3>
|
|
77
|
+
<h4>h4</h4>
|
|
78
|
+
<h5>h5</h5>
|
|
79
|
+
<h6>h6</h6>
|
|
80
|
+
"
|
|
81
|
+
`;
|
|
82
|
+
|
|
83
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc with RouterLink > complexTree 1`] = `
|
|
84
|
+
"<nav class=\\"table-of-contents\\"><ul><li><RouterLink href=\\"#s1-1\\">s1-1</RouterLink><ul><li><RouterLink href=\\"#s1-1-1\\">s1-1-1</RouterLink></li><li><RouterLink href=\\"#s1-1-2\\">s1-1-2</RouterLink></li><li><RouterLink href=\\"#s1-1-3\\">s1-1-3</RouterLink></li></ul></li><li><RouterLink href=\\"#s1-2\\">s1-2</RouterLink></li><li><RouterLink href=\\"#s1-3\\">s1-3</RouterLink><ul><li><RouterLink href=\\"#s1-3-2\\">s1-3-2</RouterLink></li></ul></li></ul></nav>
|
|
85
|
+
<h1>s1</h1>
|
|
86
|
+
<h2>s1-1</h2>
|
|
87
|
+
<h3>s1-1-1</h3>
|
|
88
|
+
<h4>s1-1-1-1</h4>
|
|
89
|
+
<h3>s1-1-2</h3>
|
|
90
|
+
<h3>s1-1-3</h3>
|
|
91
|
+
<h4>s1-1-3-1</h4>
|
|
92
|
+
<h2>s1-2</h2>
|
|
93
|
+
<h2>s1-3</h2>
|
|
94
|
+
<h3>s1-3-2</h3>
|
|
95
|
+
<h4>s1-3-2-1</h4>
|
|
96
|
+
<h5>s1-3-2-1-1</h5>
|
|
97
|
+
<h5>s1-3-2-1-2</h5>
|
|
98
|
+
"
|
|
99
|
+
`;
|
|
100
|
+
|
|
101
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc with RouterLink > reversedTree 1`] = `
|
|
102
|
+
"<nav class=\\"table-of-contents\\"><ul><li><RouterLink href=\\"#h3\\">h3</RouterLink></li><li><RouterLink href=\\"#h2\\">h2</RouterLink></li></ul></nav>
|
|
103
|
+
<h6>h6</h6>
|
|
104
|
+
<h5>h5</h5>
|
|
105
|
+
<h4>h4</h4>
|
|
106
|
+
<h3>h3</h3>
|
|
107
|
+
<h2>h2</h2>
|
|
108
|
+
<h1>h1</h1>
|
|
109
|
+
"
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc with RouterLink > simpleTree 1`] = `
|
|
113
|
+
"<nav class=\\"table-of-contents\\"><ul><li><RouterLink href=\\"#h2\\">h2</RouterLink><ul><li><RouterLink href=\\"#h3\\">h3</RouterLink></li></ul></li></ul></nav>
|
|
114
|
+
<h1>h1</h1>
|
|
115
|
+
<h2>h2</h2>
|
|
116
|
+
<h3>h3</h3>
|
|
117
|
+
<h4>h4</h4>
|
|
118
|
+
<h5>h5</h5>
|
|
119
|
+
<h6>h6</h6>
|
|
120
|
+
"
|
|
121
|
+
`;
|
|
122
|
+
|
|
123
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc with default option (h2, h3) > complexTree 1`] = `
|
|
124
|
+
"<nav class=\\"table-of-contents\\"><ul><li><a href=\\"#s1-1\\">s1-1</a><ul><li><a href=\\"#s1-1-1\\">s1-1-1</a></li><li><a href=\\"#s1-1-2\\">s1-1-2</a></li><li><a href=\\"#s1-1-3\\">s1-1-3</a></li></ul></li><li><a href=\\"#s1-2\\">s1-2</a></li><li><a href=\\"#s1-3\\">s1-3</a><ul><li><a href=\\"#s1-3-2\\">s1-3-2</a></li></ul></li></ul></nav>
|
|
125
|
+
<h1>s1</h1>
|
|
126
|
+
<h2>s1-1</h2>
|
|
127
|
+
<h3>s1-1-1</h3>
|
|
128
|
+
<h4>s1-1-1-1</h4>
|
|
129
|
+
<h3>s1-1-2</h3>
|
|
130
|
+
<h3>s1-1-3</h3>
|
|
131
|
+
<h4>s1-1-3-1</h4>
|
|
132
|
+
<h2>s1-2</h2>
|
|
133
|
+
<h2>s1-3</h2>
|
|
134
|
+
<h3>s1-3-2</h3>
|
|
135
|
+
<h4>s1-3-2-1</h4>
|
|
136
|
+
<h5>s1-3-2-1-1</h5>
|
|
137
|
+
<h5>s1-3-2-1-2</h5>
|
|
138
|
+
"
|
|
139
|
+
`;
|
|
140
|
+
|
|
141
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc with default option (h2, h3) > reversedTree 1`] = `
|
|
142
|
+
"<nav class=\\"table-of-contents\\"><ul><li><a href=\\"#h3\\">h3</a></li><li><a href=\\"#h2\\">h2</a></li></ul></nav>
|
|
143
|
+
<h6>h6</h6>
|
|
144
|
+
<h5>h5</h5>
|
|
145
|
+
<h4>h4</h4>
|
|
146
|
+
<h3>h3</h3>
|
|
147
|
+
<h2>h2</h2>
|
|
148
|
+
<h1>h1</h1>
|
|
149
|
+
"
|
|
150
|
+
`;
|
|
151
|
+
|
|
152
|
+
exports[`@vuepress/markdown > plugins > tocPlugin > should render toc with default option (h2, h3) > simpleTree 1`] = `
|
|
153
|
+
"<nav class=\\"table-of-contents\\"><ul><li><a href=\\"#h2\\">h2</a><ul><li><a href=\\"#h3\\">h3</a></li></ul></li></ul></nav>
|
|
154
|
+
<h1>h1</h1>
|
|
155
|
+
<h2>h2</h2>
|
|
156
|
+
<h3>h3</h3>
|
|
157
|
+
<h4>h4</h4>
|
|
158
|
+
<h5>h5</h5>
|
|
159
|
+
<h6>h6</h6>
|
|
160
|
+
"
|
|
161
|
+
`;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { slugify } from '@mdit-vue/shared';
|
|
2
|
+
import MarkdownIt from 'markdown-it';
|
|
3
|
+
import anchorPlugin from 'markdown-it-anchor';
|
|
4
|
+
import { describe, expect, it } from 'vitest';
|
|
5
|
+
import { tocPlugin } from '../src';
|
|
6
|
+
|
|
7
|
+
const fixtures = {
|
|
8
|
+
simpleTree: `\
|
|
9
|
+
[[toc]]
|
|
10
|
+
# h1
|
|
11
|
+
## h2
|
|
12
|
+
### h3
|
|
13
|
+
#### h4
|
|
14
|
+
##### h5
|
|
15
|
+
###### h6
|
|
16
|
+
`,
|
|
17
|
+
complexTree: `\
|
|
18
|
+
[[toc]]
|
|
19
|
+
# s1
|
|
20
|
+
## s1-1
|
|
21
|
+
### s1-1-1
|
|
22
|
+
#### s1-1-1-1
|
|
23
|
+
### s1-1-2
|
|
24
|
+
### s1-1-3
|
|
25
|
+
#### s1-1-3-1
|
|
26
|
+
## s1-2
|
|
27
|
+
## s1-3
|
|
28
|
+
### s1-3-2
|
|
29
|
+
#### s1-3-2-1
|
|
30
|
+
##### s1-3-2-1-1
|
|
31
|
+
##### s1-3-2-1-2
|
|
32
|
+
`,
|
|
33
|
+
reversedTree: `\
|
|
34
|
+
[[toc]]
|
|
35
|
+
###### h6
|
|
36
|
+
##### h5
|
|
37
|
+
#### h4
|
|
38
|
+
### h3
|
|
39
|
+
## h2
|
|
40
|
+
# h1
|
|
41
|
+
`,
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
describe('@vuepress/markdown > plugins > tocPlugin', () => {
|
|
45
|
+
describe('should render toc with default option (h2, h3)', () => {
|
|
46
|
+
const md = MarkdownIt().use(tocPlugin);
|
|
47
|
+
|
|
48
|
+
Object.entries(fixtures).forEach(([name, source]) => {
|
|
49
|
+
it(name, () => {
|
|
50
|
+
const result = md.render(source);
|
|
51
|
+
expect(result).toMatchSnapshot();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('should render toc (h1, h2, h3, h4)', () => {
|
|
57
|
+
const md = MarkdownIt().use(tocPlugin, {
|
|
58
|
+
level: [1, 2, 3, 4],
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
Object.entries(fixtures).forEach(([name, source]) => {
|
|
62
|
+
it(name, () => {
|
|
63
|
+
const result = md.render(source);
|
|
64
|
+
expect(result).toMatchSnapshot();
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('should render toc with RouterLink', () => {
|
|
70
|
+
const md = MarkdownIt().use(tocPlugin, {
|
|
71
|
+
linkTag: 'RouterLink',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
Object.entries(fixtures).forEach(([name, source]) => {
|
|
75
|
+
it(name, () => {
|
|
76
|
+
const result = md.render(source);
|
|
77
|
+
expect(result).toMatchSnapshot();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe('should render class name correctly', () => {
|
|
83
|
+
const md = MarkdownIt().use(tocPlugin, {
|
|
84
|
+
// remove default container class
|
|
85
|
+
containerClass: '',
|
|
86
|
+
// add custom list / item / link class
|
|
87
|
+
listClass: 'toc-list',
|
|
88
|
+
itemClass: 'toc-item',
|
|
89
|
+
linkClass: 'toc-link',
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
Object.entries(fixtures).forEach(([name, source]) => {
|
|
93
|
+
it(name, () => {
|
|
94
|
+
const result = md.render(source);
|
|
95
|
+
expect(result).toMatchSnapshot();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('should include html elements and should escape texts', () => {
|
|
101
|
+
const md = MarkdownIt({
|
|
102
|
+
html: true,
|
|
103
|
+
})
|
|
104
|
+
.use(anchorPlugin, {
|
|
105
|
+
level: [1, 2, 3, 4, 5, 6],
|
|
106
|
+
slugify,
|
|
107
|
+
permalink: anchorPlugin.permalink.ariaHidden({
|
|
108
|
+
class: 'header-anchor',
|
|
109
|
+
symbol: '#',
|
|
110
|
+
space: true,
|
|
111
|
+
placement: 'before',
|
|
112
|
+
}),
|
|
113
|
+
})
|
|
114
|
+
.use(tocPlugin, { slugify });
|
|
115
|
+
|
|
116
|
+
const testCases: [string, { slug: string; title: string; h2: string }][] = [
|
|
117
|
+
// html element should be kept as is
|
|
118
|
+
[
|
|
119
|
+
`\
|
|
120
|
+
[[toc]]
|
|
121
|
+
## foo <bar />
|
|
122
|
+
`,
|
|
123
|
+
{
|
|
124
|
+
slug: 'foo',
|
|
125
|
+
title: 'foo <bar />',
|
|
126
|
+
h2: 'foo <bar />',
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
// inline code should be escaped
|
|
130
|
+
[
|
|
131
|
+
`\
|
|
132
|
+
[[toc]]
|
|
133
|
+
## foo <bar /> \`<code />\`
|
|
134
|
+
`,
|
|
135
|
+
{
|
|
136
|
+
slug: 'foo-code',
|
|
137
|
+
title: 'foo <bar /> <code />',
|
|
138
|
+
h2: 'foo <bar /> <code><code /></code>',
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
// text should be escaped
|
|
142
|
+
[
|
|
143
|
+
`\
|
|
144
|
+
[[toc]]
|
|
145
|
+
## foo <bar/> "baz"
|
|
146
|
+
`,
|
|
147
|
+
{
|
|
148
|
+
slug: 'foo-baz',
|
|
149
|
+
title: 'foo <bar/> "baz"',
|
|
150
|
+
h2: 'foo <bar/> "baz"',
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
// text should be escaped
|
|
154
|
+
[
|
|
155
|
+
`\
|
|
156
|
+
[[toc]]
|
|
157
|
+
## < test >
|
|
158
|
+
`,
|
|
159
|
+
{
|
|
160
|
+
slug: 'test',
|
|
161
|
+
title: '< test >',
|
|
162
|
+
h2: '< test >',
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
testCases.forEach(([source, expected], i) =>
|
|
168
|
+
it(`case ${i}`, () => {
|
|
169
|
+
expect(md.render(source)).toEqual(`\
|
|
170
|
+
<nav class="table-of-contents"><ul><li><a href="#${expected.slug}">${expected.title}</a></li></ul></nav>
|
|
171
|
+
<h2 id="${expected.slug}" tabindex="-1"><a class="header-anchor" href="#${expected.slug}" aria-hidden="true">#</a> ${expected.h2}</h2>
|
|
172
|
+
`);
|
|
173
|
+
}),
|
|
174
|
+
);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
describe('edge cases', () => {
|
|
178
|
+
it('should not terminate the blockquote', () => {
|
|
179
|
+
const md = MarkdownIt().use(tocPlugin);
|
|
180
|
+
|
|
181
|
+
const source = `\
|
|
182
|
+
> foo
|
|
183
|
+
[[toc]]
|
|
184
|
+
`;
|
|
185
|
+
expect(md.render(source)).toEqual(`\
|
|
186
|
+
<blockquote>
|
|
187
|
+
<p>foo
|
|
188
|
+
[[toc]]</p>
|
|
189
|
+
</blockquote>
|
|
190
|
+
`);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
});
|