@mdit-vue/plugin-toc 0.7.0 → 0.9.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/dist/index.cjs +15 -8
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +15 -8
- package/package.json +11 -7
- package/CHANGELOG.md +0 -34
- package/build.config.ts +0 -11
- package/src/create-render-headers.ts +0 -43
- package/src/create-toc-block-rule.ts +0 -62
- package/src/index.ts +0 -4
- package/src/toc-plugin.ts +0 -79
- package/src/types.ts +0 -83
- package/tests/__snapshots__/toc-plugin.spec.ts.snap +0 -161
- package/tests/toc-plugin.spec.ts +0 -193
package/dist/index.cjs
CHANGED
|
@@ -18,7 +18,9 @@ const createRenderHeaders = ({
|
|
|
18
18
|
const linkTagString = shared.htmlEscape(linkTag);
|
|
19
19
|
const linkClassString = linkClass ? ` class="${shared.htmlEscape(linkClass)}"` : "";
|
|
20
20
|
const linkTo = (slug) => linkTag === "router-link" ? ` to="#${slug}"` : ` href="#${slug}"`;
|
|
21
|
-
const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map(
|
|
21
|
+
const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map(
|
|
22
|
+
(header) => `<${itemTagString}${itemClassString}${itemClassString}><${linkTagString}${linkClassString}${linkTo(header.slug)}>${header.title}</${linkTagString}>${header.children.length > 0 ? renderHeaders(header.children) : ""}</${itemTagString}>`
|
|
23
|
+
).join("")}</${listTagString}>`;
|
|
22
24
|
return renderHeaders;
|
|
23
25
|
};
|
|
24
26
|
|
|
@@ -80,13 +82,18 @@ const tocPlugin = (md, {
|
|
|
80
82
|
});
|
|
81
83
|
return true;
|
|
82
84
|
});
|
|
83
|
-
md.block.ruler.before(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
md.block.ruler.before(
|
|
86
|
+
"heading",
|
|
87
|
+
"toc",
|
|
88
|
+
createTocBlockRule({
|
|
89
|
+
pattern,
|
|
90
|
+
containerTag,
|
|
91
|
+
containerClass
|
|
92
|
+
}),
|
|
93
|
+
{
|
|
94
|
+
alt: ["paragraph", "reference", "blockquote"]
|
|
95
|
+
}
|
|
96
|
+
);
|
|
90
97
|
const renderHeaders = createRenderHeaders({
|
|
91
98
|
listTag,
|
|
92
99
|
listClass,
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -14,7 +14,9 @@ const createRenderHeaders = ({
|
|
|
14
14
|
const linkTagString = htmlEscape(linkTag);
|
|
15
15
|
const linkClassString = linkClass ? ` class="${htmlEscape(linkClass)}"` : "";
|
|
16
16
|
const linkTo = (slug) => linkTag === "router-link" ? ` to="#${slug}"` : ` href="#${slug}"`;
|
|
17
|
-
const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map(
|
|
17
|
+
const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map(
|
|
18
|
+
(header) => `<${itemTagString}${itemClassString}${itemClassString}><${linkTagString}${linkClassString}${linkTo(header.slug)}>${header.title}</${linkTagString}>${header.children.length > 0 ? renderHeaders(header.children) : ""}</${itemTagString}>`
|
|
19
|
+
).join("")}</${listTagString}>`;
|
|
18
20
|
return renderHeaders;
|
|
19
21
|
};
|
|
20
22
|
|
|
@@ -76,13 +78,18 @@ const tocPlugin = (md, {
|
|
|
76
78
|
});
|
|
77
79
|
return true;
|
|
78
80
|
});
|
|
79
|
-
md.block.ruler.before(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
md.block.ruler.before(
|
|
82
|
+
"heading",
|
|
83
|
+
"toc",
|
|
84
|
+
createTocBlockRule({
|
|
85
|
+
pattern,
|
|
86
|
+
containerTag,
|
|
87
|
+
containerClass
|
|
88
|
+
}),
|
|
89
|
+
{
|
|
90
|
+
alt: ["paragraph", "reference", "blockquote"]
|
|
91
|
+
}
|
|
92
|
+
);
|
|
86
93
|
const renderHeaders = createRenderHeaders({
|
|
87
94
|
listTag,
|
|
88
95
|
listClass,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdit-vue/plugin-toc",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "A markdown-it plugin to generate table-of-contents that compatible with Vue.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"markdown-it",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"author": "meteorlxy <meteor.lxy@foxmail.com>",
|
|
22
|
+
"type": "module",
|
|
22
23
|
"exports": {
|
|
23
24
|
".": {
|
|
24
25
|
"types": "./dist/index.d.ts",
|
|
@@ -29,24 +30,27 @@
|
|
|
29
30
|
"main": "./dist/index.cjs",
|
|
30
31
|
"module": "./dist/index.mjs",
|
|
31
32
|
"types": "./dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"./dist"
|
|
35
|
+
],
|
|
32
36
|
"scripts": {
|
|
33
37
|
"build": "unbuild",
|
|
34
38
|
"test": "vitest"
|
|
35
39
|
},
|
|
36
40
|
"dependencies": {
|
|
37
|
-
"@mdit-vue/shared": "0.
|
|
38
|
-
"@mdit-vue/types": "0.
|
|
41
|
+
"@mdit-vue/shared": "0.9.0",
|
|
42
|
+
"@mdit-vue/types": "0.9.0",
|
|
39
43
|
"@types/markdown-it": "^12.2.3",
|
|
40
44
|
"markdown-it": "^13.0.1"
|
|
41
45
|
},
|
|
42
46
|
"devDependencies": {
|
|
43
|
-
"c8": "^7.
|
|
47
|
+
"c8": "^7.12.0",
|
|
44
48
|
"markdown-it-anchor": "^8.6.4",
|
|
45
|
-
"unbuild": "^0.7.
|
|
46
|
-
"vitest": "^0.
|
|
49
|
+
"unbuild": "^0.7.6",
|
|
50
|
+
"vitest": "^0.19.1"
|
|
47
51
|
},
|
|
48
52
|
"publishConfig": {
|
|
49
53
|
"access": "public"
|
|
50
54
|
},
|
|
51
|
-
"gitHead": "
|
|
55
|
+
"gitHead": "4279fc7ac1db0b585ffb395f55a07038af2a7263"
|
|
52
56
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
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.7.0](https://github.com/mdit-vue/mdit-vue/compare/v0.6.0...v0.7.0) (2022-07-18)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @mdit-vue/plugin-toc
|
|
9
|
-
|
|
10
|
-
# [0.6.0](https://github.com/mdit-vue/mdit-vue/compare/v0.5.0...v0.6.0) (2022-07-11)
|
|
11
|
-
|
|
12
|
-
### Features
|
|
13
|
-
|
|
14
|
-
- **types:** extract types from shared package ([5fc56ce](https://github.com/mdit-vue/mdit-vue/commit/5fc56ce6439159584e09e13ca9a6e87abe8f4389))
|
|
15
|
-
|
|
16
|
-
# [0.5.0](https://github.com/mdit-vue/mdit-vue/compare/v0.4.0...v0.5.0) (2022-07-06)
|
|
17
|
-
|
|
18
|
-
**Note:** Version bump only for package @mdit-vue/plugin-toc
|
|
19
|
-
|
|
20
|
-
# [0.4.0](https://github.com/mdit-vue/mdit-vue/compare/v0.3.1...v0.4.0) (2022-07-02)
|
|
21
|
-
|
|
22
|
-
**Note:** Version bump only for package @mdit-vue/plugin-toc
|
|
23
|
-
|
|
24
|
-
## [0.3.1](https://github.com/mdit-vue/mdit-vue/compare/v0.3.0...v0.3.1) (2022-06-25)
|
|
25
|
-
|
|
26
|
-
### Bug Fixes
|
|
27
|
-
|
|
28
|
-
- **plugin-toc:** export types ([310320e](https://github.com/mdit-vue/mdit-vue/commit/310320eb12087b0296ed8840b5d581887568b15f))
|
|
29
|
-
|
|
30
|
-
# [0.3.0](https://github.com/mdit-vue/mdit-vue/compare/v0.2.0...v0.3.0) (2022-06-25)
|
|
31
|
-
|
|
32
|
-
### Features
|
|
33
|
-
|
|
34
|
-
- **plugin-toc:** add toc plugin ([e64cc2c](https://github.com/mdit-vue/mdit-vue/commit/e64cc2ce3bbc89999d373f8a00fb3a049d6af58f))
|
package/build.config.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { htmlEscape } from '@mdit-vue/shared';
|
|
2
|
-
import type { MarkdownItHeader } from '@mdit-vue/types';
|
|
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
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
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
DELETED
package/src/toc-plugin.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
slugify as defaultSlugify,
|
|
3
|
-
resolveHeadersFromTokens,
|
|
4
|
-
} from '@mdit-vue/shared';
|
|
5
|
-
import type { MarkdownItHeader } from '@mdit-vue/types';
|
|
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
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options of @mdit-vue/plugin-toc
|
|
3
|
-
*/
|
|
4
|
-
export interface TocPluginOptions {
|
|
5
|
-
/**
|
|
6
|
-
* The pattern serving as the TOC placeholder in your markdown
|
|
7
|
-
*
|
|
8
|
-
* @default /^\[\[toc\]\]$/i
|
|
9
|
-
*/
|
|
10
|
-
pattern?: RegExp;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* A custom slugification function
|
|
14
|
-
*
|
|
15
|
-
* Should use the same slugify function with markdown-it-anchor
|
|
16
|
-
* to ensure the link is matched
|
|
17
|
-
*/
|
|
18
|
-
slugify?: (str: string) => string;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A function for formatting headings
|
|
22
|
-
*/
|
|
23
|
-
format?: (str: string) => string;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Heading level that going to be included in the TOC
|
|
27
|
-
*
|
|
28
|
-
* Should be a subset of markdown-it-anchor's `level` option
|
|
29
|
-
* to ensure the link is existed
|
|
30
|
-
*
|
|
31
|
-
* @default [2,3]
|
|
32
|
-
*/
|
|
33
|
-
level?: number[];
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* HTML tag of the TOC container
|
|
37
|
-
*
|
|
38
|
-
* @default 'nav'
|
|
39
|
-
*/
|
|
40
|
-
containerTag?: string;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The class for the TOC container
|
|
44
|
-
*
|
|
45
|
-
* @default 'table-of-contents'
|
|
46
|
-
*/
|
|
47
|
-
containerClass?: string;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* HTML tag of the TOC list
|
|
51
|
-
*
|
|
52
|
-
* @default 'ul'
|
|
53
|
-
*/
|
|
54
|
-
listTag?: 'ul' | 'ol';
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* The class for the TOC list
|
|
58
|
-
*
|
|
59
|
-
* @default ''
|
|
60
|
-
*/
|
|
61
|
-
listClass?: string;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* The class for the `<li>` tag
|
|
65
|
-
*
|
|
66
|
-
* @default ''
|
|
67
|
-
*/
|
|
68
|
-
itemClass?: string;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The tag of the link inside `<li>` tag
|
|
72
|
-
*
|
|
73
|
-
* @default 'a'
|
|
74
|
-
*/
|
|
75
|
-
linkTag?: 'a' | 'router-link';
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The class for the link inside the `<li>` tag
|
|
79
|
-
*
|
|
80
|
-
* @default ''
|
|
81
|
-
*/
|
|
82
|
-
linkClass?: string;
|
|
83
|
-
}
|
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
// Vitest Snapshot v1
|
|
2
|
-
|
|
3
|
-
exports[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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[`@mdit-vue/plugin-toc > toc-plugin > 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
|
-
`;
|
package/tests/toc-plugin.spec.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
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('@mdit-vue/plugin-toc > toc-plugin', () => {
|
|
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
|
-
});
|