@mdit-vue/plugin-toc 2.1.4 → 3.0.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 CHANGED
@@ -1,102 +1,120 @@
1
- 'use strict';
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
2
22
 
3
- const shared = require('@mdit-vue/shared');
23
+ //#endregion
24
+ const __mdit_vue_shared = __toESM(require("@mdit-vue/shared"));
4
25
 
5
- const createRenderHeaders = ({
6
- listTag,
7
- listClass,
8
- itemClass,
9
- linkTag,
10
- linkClass
11
- }) => {
12
- const listTagString = shared.htmlEscape(listTag);
13
- const listClassString = listClass ? ` class="${shared.htmlEscape(listClass)}"` : "";
14
- const itemTagString = "li";
15
- const itemClassString = itemClass ? ` class="${shared.htmlEscape(itemClass)}"` : "";
16
- const linkTagString = shared.htmlEscape(linkTag);
17
- const linkClassString = linkClass ? ` class="${shared.htmlEscape(linkClass)}"` : "";
18
- const linkTo = (link) => linkTag === "router-link" ? ` to="${link}"` : ` href="${link}"`;
19
- const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map(
20
- (header) => `<${itemTagString}${itemClassString}><${linkTagString}${linkClassString}${linkTo(header.link)}>${header.title}</${linkTagString}>${header.children.length > 0 ? renderHeaders(header.children) : ""}</${itemTagString}>`
21
- ).join("")}</${listTagString}>`;
22
- return renderHeaders;
26
+ //#region src/create-render-headers.ts
27
+ const createRenderHeaders = ({ listTag, listClass, itemClass, linkTag, linkClass }) => {
28
+ const listTagString = (0, __mdit_vue_shared.htmlEscape)(listTag);
29
+ const listClassString = listClass ? ` class="${(0, __mdit_vue_shared.htmlEscape)(listClass)}"` : "";
30
+ const itemTagString = "li";
31
+ const itemClassString = itemClass ? ` class="${(0, __mdit_vue_shared.htmlEscape)(itemClass)}"` : "";
32
+ const linkTagString = (0, __mdit_vue_shared.htmlEscape)(linkTag);
33
+ const linkClassString = linkClass ? ` class="${(0, __mdit_vue_shared.htmlEscape)(linkClass)}"` : "";
34
+ const linkTo = (link) => linkTag === "router-link" ? ` to="${link}"` : ` href="${link}"`;
35
+ const renderHeaders = (headers) => `\
36
+ <${listTagString}${listClassString}>\
37
+ ${headers.map((header) => `\
38
+ <${itemTagString}${itemClassString}>\
39
+ <${linkTagString}${linkClassString}${linkTo(header.link)}>\
40
+ ${header.title}\
41
+ </${linkTagString}>\
42
+ ${header.children.length > 0 ? renderHeaders(header.children) : ""}\
43
+ </${itemTagString}>\
44
+ `).join("")}\
45
+ </${listTagString}>`;
46
+ return renderHeaders;
23
47
  };
24
48
 
25
- const createTocBlockRule = ({
26
- pattern,
27
- containerTag,
28
- containerClass
29
- }) => (state, startLine, endLine, silent) => {
30
- if (state.sCount[startLine] - state.blkIndent >= 4) {
31
- return false;
32
- }
33
- const pos = state.bMarks[startLine] + state.tShift[startLine];
34
- const max = state.eMarks[startLine];
35
- const lineFirstToken = state.src.slice(pos, max).split(" ")[0];
36
- if (!pattern.test(lineFirstToken)) return false;
37
- if (silent) return true;
38
- state.line = startLine + 1;
39
- const tokenOpen = state.push("toc_open", containerTag, 1);
40
- tokenOpen.markup = "";
41
- tokenOpen.map = [startLine, state.line];
42
- if (containerClass) {
43
- tokenOpen.attrSet("class", containerClass);
44
- }
45
- const tokenBody = state.push("toc_body", "", 0);
46
- tokenBody.markup = lineFirstToken;
47
- tokenBody.map = [startLine, state.line];
48
- tokenBody.hidden = true;
49
- const tokenClose = state.push("toc_close", containerTag, -1);
50
- tokenClose.markup = "";
51
- tokenBody.map = [startLine, state.line];
52
- return true;
49
+ //#endregion
50
+ //#region src/create-toc-block-rule.ts
51
+ /**
52
+ * Forked and modified from markdown-it-toc-done-right
53
+ *
54
+ * - remove the `inlineOptions` support
55
+ * - use markdown-it default renderer to render token whenever possible
56
+ *
57
+ * @see https://github.com/nagaozen/markdown-it-toc-done-right
58
+ */
59
+ const createTocBlockRule = ({ pattern, containerTag, containerClass }) => (state, startLine, endLine, silent) => {
60
+ if (state.sCount[startLine] - state.blkIndent >= 4) return false;
61
+ const pos = state.bMarks[startLine] + state.tShift[startLine];
62
+ const max = state.eMarks[startLine];
63
+ const lineFirstToken = state.src.slice(pos, max).split(" ")[0];
64
+ if (!pattern.test(lineFirstToken)) return false;
65
+ if (silent) return true;
66
+ state.line = startLine + 1;
67
+ const tokenOpen = state.push("toc_open", containerTag, 1);
68
+ tokenOpen.markup = "";
69
+ tokenOpen.map = [startLine, state.line];
70
+ if (containerClass) tokenOpen.attrSet("class", containerClass);
71
+ const tokenBody = state.push("toc_body", "", 0);
72
+ tokenBody.markup = lineFirstToken;
73
+ tokenBody.map = [startLine, state.line];
74
+ tokenBody.hidden = true;
75
+ const tokenClose = state.push("toc_close", containerTag, -1);
76
+ tokenClose.markup = "";
77
+ tokenBody.map = [startLine, state.line];
78
+ return true;
53
79
  };
54
80
 
55
- const tocPlugin = (md, {
56
- pattern = /^\[\[toc\]\]$/i,
57
- slugify = shared.slugify,
58
- format,
59
- level = [2, 3],
60
- shouldAllowNested = false,
61
- containerTag = "nav",
62
- containerClass = "table-of-contents",
63
- listTag = "ul",
64
- listClass = "",
65
- itemClass = "",
66
- linkTag = "a",
67
- linkClass = ""
68
- } = {}) => {
69
- md.block.ruler.before(
70
- "heading",
71
- "toc",
72
- createTocBlockRule({
73
- pattern,
74
- containerTag,
75
- containerClass
76
- }),
77
- {
78
- alt: ["paragraph", "reference", "blockquote"]
79
- }
80
- );
81
- const renderHeaders = createRenderHeaders({
82
- listTag,
83
- listClass,
84
- itemClass,
85
- linkTag,
86
- linkClass
87
- });
88
- md.renderer.rules.toc_body = (tokens) => renderHeaders(
89
- shared.resolveHeadersFromTokens(tokens, {
90
- level,
91
- shouldAllowHtml: true,
92
- shouldAllowNested,
93
- shouldEscapeText: true,
94
- slugify,
95
- format
96
- })
97
- );
81
+ //#endregion
82
+ //#region src/toc-plugin.ts
83
+ /**
84
+ * Generate table of contents
85
+ *
86
+ * Forked and modified from markdown-it-toc-done-right:
87
+ *
88
+ * @see https://github.com/nagaozen/markdown-it-toc-done-right
89
+ */
90
+ const tocPlugin = (md, { pattern = /^\[\[toc\]\]$/i, slugify = __mdit_vue_shared.slugify, format, level = [2, 3], shouldAllowNested = false, containerTag = "nav", containerClass = "table-of-contents", listTag = "ul", listClass = "", itemClass = "", linkTag = "a", linkClass = "" } = {}) => {
91
+ md.block.ruler.before("heading", "toc", createTocBlockRule({
92
+ pattern,
93
+ containerTag,
94
+ containerClass
95
+ }), { alt: [
96
+ "paragraph",
97
+ "reference",
98
+ "blockquote"
99
+ ] });
100
+ const renderHeaders = createRenderHeaders({
101
+ listTag,
102
+ listClass,
103
+ itemClass,
104
+ linkTag,
105
+ linkClass
106
+ });
107
+ md.renderer.rules.toc_body = (tokens) => renderHeaders((0, __mdit_vue_shared.resolveHeadersFromTokens)(tokens, {
108
+ level,
109
+ shouldAllowHtml: true,
110
+ shouldAllowNested,
111
+ shouldEscapeText: true,
112
+ slugify,
113
+ format
114
+ }));
98
115
  };
99
116
 
117
+ //#endregion
100
118
  exports.createRenderHeaders = createRenderHeaders;
101
119
  exports.createTocBlockRule = createTocBlockRule;
102
- exports.tocPlugin = tocPlugin;
120
+ exports.tocPlugin = tocPlugin;
package/dist/index.d.cts CHANGED
@@ -1,92 +1,101 @@
1
- import { MarkdownItHeader } from '@mdit-vue/types';
2
- import { RuleBlock } from 'markdown-it/lib/parser_block.mjs';
3
- import { PluginWithOptions } from 'markdown-it';
1
+ import { MarkdownItHeader } from "@mdit-vue/types";
2
+ import { RuleBlock } from "markdown-it/lib/parser_block.mjs";
3
+ import { PluginWithOptions } from "markdown-it";
4
4
 
5
+ //#region src/types.d.ts
5
6
  /**
6
7
  * Options of @mdit-vue/plugin-toc
7
8
  */
8
9
  interface TocPluginOptions {
9
- /**
10
- * The pattern serving as the TOC placeholder in your markdown
11
- *
12
- * @default /^\[\[toc\]\]$/i
13
- */
14
- pattern?: RegExp;
15
- /**
16
- * A custom slugification function
17
- *
18
- * Should use the same slugify function with markdown-it-anchor
19
- * to ensure the link is matched
20
- */
21
- slugify?: (str: string) => string;
22
- /**
23
- * A function for formatting headings
24
- */
25
- format?: (str: string) => string;
26
- /**
27
- * Heading level that going to be included in the TOC
28
- *
29
- * Should be a subset of markdown-it-anchor's `level` option
30
- * to ensure the link is existed
31
- *
32
- * @default [2,3]
33
- */
34
- level?: number[];
35
- /**
36
- * Should allow headers inside nested blocks or not
37
- *
38
- * If set to `true`, headers inside blockquote, list, etc. would also be included.
39
- *
40
- * @default false
41
- */
42
- shouldAllowNested?: boolean;
43
- /**
44
- * HTML tag of the TOC container
45
- *
46
- * @default 'nav'
47
- */
48
- containerTag?: string;
49
- /**
50
- * The class for the TOC container
51
- *
52
- * @default 'table-of-contents'
53
- */
54
- containerClass?: string;
55
- /**
56
- * HTML tag of the TOC list
57
- *
58
- * @default 'ul'
59
- */
60
- listTag?: 'ol' | 'ul';
61
- /**
62
- * The class for the TOC list
63
- *
64
- * @default ''
65
- */
66
- listClass?: string;
67
- /**
68
- * The class for the `<li>` tag
69
- *
70
- * @default ''
71
- */
72
- itemClass?: string;
73
- /**
74
- * The tag of the link inside `<li>` tag
75
- *
76
- * @default 'a'
77
- */
78
- linkTag?: 'a' | 'router-link';
79
- /**
80
- * The class for the link inside the `<li>` tag
81
- *
82
- * @default ''
83
- */
84
- linkClass?: string;
10
+ /**
11
+ * The pattern serving as the TOC placeholder in your markdown
12
+ *
13
+ * @default /^\[\[toc\]\]$/i
14
+ */
15
+ pattern?: RegExp;
16
+ /**
17
+ * A custom slugification function
18
+ *
19
+ * Should use the same slugify function with markdown-it-anchor
20
+ * to ensure the link is matched
21
+ */
22
+ slugify?: (str: string) => string;
23
+ /**
24
+ * A function for formatting headings
25
+ */
26
+ format?: (str: string) => string;
27
+ /**
28
+ * Heading level that going to be included in the TOC
29
+ *
30
+ * Should be a subset of markdown-it-anchor's `level` option
31
+ * to ensure the link is existed
32
+ *
33
+ * @default [2,3]
34
+ */
35
+ level?: number[];
36
+ /**
37
+ * Should allow headers inside nested blocks or not
38
+ *
39
+ * If set to `true`, headers inside blockquote, list, etc. would also be included.
40
+ *
41
+ * @default false
42
+ */
43
+ shouldAllowNested?: boolean;
44
+ /**
45
+ * HTML tag of the TOC container
46
+ *
47
+ * @default 'nav'
48
+ */
49
+ containerTag?: string;
50
+ /**
51
+ * The class for the TOC container
52
+ *
53
+ * @default 'table-of-contents'
54
+ */
55
+ containerClass?: string;
56
+ /**
57
+ * HTML tag of the TOC list
58
+ *
59
+ * @default 'ul'
60
+ */
61
+ listTag?: 'ol' | 'ul';
62
+ /**
63
+ * The class for the TOC list
64
+ *
65
+ * @default ''
66
+ */
67
+ listClass?: string;
68
+ /**
69
+ * The class for the `<li>` tag
70
+ *
71
+ * @default ''
72
+ */
73
+ itemClass?: string;
74
+ /**
75
+ * The tag of the link inside `<li>` tag
76
+ *
77
+ * @default 'a'
78
+ */
79
+ linkTag?: 'a' | 'router-link';
80
+ /**
81
+ * The class for the link inside the `<li>` tag
82
+ *
83
+ * @default ''
84
+ */
85
+ linkClass?: string;
85
86
  }
86
-
87
+ //#endregion
88
+ //#region src/create-render-headers.d.ts
87
89
  type RenderHeadersFn = (headers: MarkdownItHeader[]) => string;
88
- declare const createRenderHeaders: ({ listTag, listClass, itemClass, linkTag, linkClass, }: Pick<Required<TocPluginOptions>, "itemClass" | "linkClass" | "linkTag" | "listClass" | "listTag">) => RenderHeadersFn;
89
-
90
+ declare const createRenderHeaders: ({
91
+ listTag,
92
+ listClass,
93
+ itemClass,
94
+ linkTag,
95
+ linkClass
96
+ }: Pick<Required<TocPluginOptions>, "itemClass" | "linkClass" | "linkTag" | "listClass" | "listTag">) => RenderHeadersFn;
97
+ //#endregion
98
+ //#region src/create-toc-block-rule.d.ts
90
99
  /**
91
100
  * Forked and modified from markdown-it-toc-done-right
92
101
  *
@@ -95,8 +104,13 @@ declare const createRenderHeaders: ({ listTag, listClass, itemClass, linkTag, li
95
104
  *
96
105
  * @see https://github.com/nagaozen/markdown-it-toc-done-right
97
106
  */
98
- declare const createTocBlockRule: ({ pattern, containerTag, containerClass, }: Pick<Required<TocPluginOptions>, "containerClass" | "containerTag" | "pattern">) => RuleBlock;
99
-
107
+ declare const createTocBlockRule: ({
108
+ pattern,
109
+ containerTag,
110
+ containerClass
111
+ }: Pick<Required<TocPluginOptions>, "containerClass" | "containerTag" | "pattern">) => RuleBlock;
112
+ //#endregion
113
+ //#region src/toc-plugin.d.ts
100
114
  /**
101
115
  * Generate table of contents
102
116
  *
@@ -105,6 +119,5 @@ declare const createTocBlockRule: ({ pattern, containerTag, containerClass, }: P
105
119
  * @see https://github.com/nagaozen/markdown-it-toc-done-right
106
120
  */
107
121
  declare const tocPlugin: PluginWithOptions<TocPluginOptions>;
108
-
109
- export { createRenderHeaders, createTocBlockRule, tocPlugin };
110
- export type { TocPluginOptions };
122
+ //#endregion
123
+ export { TocPluginOptions, createRenderHeaders, createTocBlockRule, tocPlugin };
package/dist/index.d.mts CHANGED
@@ -1,92 +1,101 @@
1
- import { MarkdownItHeader } from '@mdit-vue/types';
2
- import { RuleBlock } from 'markdown-it/lib/parser_block.mjs';
3
- import { PluginWithOptions } from 'markdown-it';
1
+ import { MarkdownItHeader } from "@mdit-vue/types";
2
+ import { RuleBlock } from "markdown-it/lib/parser_block.mjs";
3
+ import { PluginWithOptions } from "markdown-it";
4
4
 
5
+ //#region src/types.d.ts
5
6
  /**
6
7
  * Options of @mdit-vue/plugin-toc
7
8
  */
8
9
  interface TocPluginOptions {
9
- /**
10
- * The pattern serving as the TOC placeholder in your markdown
11
- *
12
- * @default /^\[\[toc\]\]$/i
13
- */
14
- pattern?: RegExp;
15
- /**
16
- * A custom slugification function
17
- *
18
- * Should use the same slugify function with markdown-it-anchor
19
- * to ensure the link is matched
20
- */
21
- slugify?: (str: string) => string;
22
- /**
23
- * A function for formatting headings
24
- */
25
- format?: (str: string) => string;
26
- /**
27
- * Heading level that going to be included in the TOC
28
- *
29
- * Should be a subset of markdown-it-anchor's `level` option
30
- * to ensure the link is existed
31
- *
32
- * @default [2,3]
33
- */
34
- level?: number[];
35
- /**
36
- * Should allow headers inside nested blocks or not
37
- *
38
- * If set to `true`, headers inside blockquote, list, etc. would also be included.
39
- *
40
- * @default false
41
- */
42
- shouldAllowNested?: boolean;
43
- /**
44
- * HTML tag of the TOC container
45
- *
46
- * @default 'nav'
47
- */
48
- containerTag?: string;
49
- /**
50
- * The class for the TOC container
51
- *
52
- * @default 'table-of-contents'
53
- */
54
- containerClass?: string;
55
- /**
56
- * HTML tag of the TOC list
57
- *
58
- * @default 'ul'
59
- */
60
- listTag?: 'ol' | 'ul';
61
- /**
62
- * The class for the TOC list
63
- *
64
- * @default ''
65
- */
66
- listClass?: string;
67
- /**
68
- * The class for the `<li>` tag
69
- *
70
- * @default ''
71
- */
72
- itemClass?: string;
73
- /**
74
- * The tag of the link inside `<li>` tag
75
- *
76
- * @default 'a'
77
- */
78
- linkTag?: 'a' | 'router-link';
79
- /**
80
- * The class for the link inside the `<li>` tag
81
- *
82
- * @default ''
83
- */
84
- linkClass?: string;
10
+ /**
11
+ * The pattern serving as the TOC placeholder in your markdown
12
+ *
13
+ * @default /^\[\[toc\]\]$/i
14
+ */
15
+ pattern?: RegExp;
16
+ /**
17
+ * A custom slugification function
18
+ *
19
+ * Should use the same slugify function with markdown-it-anchor
20
+ * to ensure the link is matched
21
+ */
22
+ slugify?: (str: string) => string;
23
+ /**
24
+ * A function for formatting headings
25
+ */
26
+ format?: (str: string) => string;
27
+ /**
28
+ * Heading level that going to be included in the TOC
29
+ *
30
+ * Should be a subset of markdown-it-anchor's `level` option
31
+ * to ensure the link is existed
32
+ *
33
+ * @default [2,3]
34
+ */
35
+ level?: number[];
36
+ /**
37
+ * Should allow headers inside nested blocks or not
38
+ *
39
+ * If set to `true`, headers inside blockquote, list, etc. would also be included.
40
+ *
41
+ * @default false
42
+ */
43
+ shouldAllowNested?: boolean;
44
+ /**
45
+ * HTML tag of the TOC container
46
+ *
47
+ * @default 'nav'
48
+ */
49
+ containerTag?: string;
50
+ /**
51
+ * The class for the TOC container
52
+ *
53
+ * @default 'table-of-contents'
54
+ */
55
+ containerClass?: string;
56
+ /**
57
+ * HTML tag of the TOC list
58
+ *
59
+ * @default 'ul'
60
+ */
61
+ listTag?: 'ol' | 'ul';
62
+ /**
63
+ * The class for the TOC list
64
+ *
65
+ * @default ''
66
+ */
67
+ listClass?: string;
68
+ /**
69
+ * The class for the `<li>` tag
70
+ *
71
+ * @default ''
72
+ */
73
+ itemClass?: string;
74
+ /**
75
+ * The tag of the link inside `<li>` tag
76
+ *
77
+ * @default 'a'
78
+ */
79
+ linkTag?: 'a' | 'router-link';
80
+ /**
81
+ * The class for the link inside the `<li>` tag
82
+ *
83
+ * @default ''
84
+ */
85
+ linkClass?: string;
85
86
  }
86
-
87
+ //#endregion
88
+ //#region src/create-render-headers.d.ts
87
89
  type RenderHeadersFn = (headers: MarkdownItHeader[]) => string;
88
- declare const createRenderHeaders: ({ listTag, listClass, itemClass, linkTag, linkClass, }: Pick<Required<TocPluginOptions>, "itemClass" | "linkClass" | "linkTag" | "listClass" | "listTag">) => RenderHeadersFn;
89
-
90
+ declare const createRenderHeaders: ({
91
+ listTag,
92
+ listClass,
93
+ itemClass,
94
+ linkTag,
95
+ linkClass
96
+ }: Pick<Required<TocPluginOptions>, "itemClass" | "linkClass" | "linkTag" | "listClass" | "listTag">) => RenderHeadersFn;
97
+ //#endregion
98
+ //#region src/create-toc-block-rule.d.ts
90
99
  /**
91
100
  * Forked and modified from markdown-it-toc-done-right
92
101
  *
@@ -95,8 +104,13 @@ declare const createRenderHeaders: ({ listTag, listClass, itemClass, linkTag, li
95
104
  *
96
105
  * @see https://github.com/nagaozen/markdown-it-toc-done-right
97
106
  */
98
- declare const createTocBlockRule: ({ pattern, containerTag, containerClass, }: Pick<Required<TocPluginOptions>, "containerClass" | "containerTag" | "pattern">) => RuleBlock;
99
-
107
+ declare const createTocBlockRule: ({
108
+ pattern,
109
+ containerTag,
110
+ containerClass
111
+ }: Pick<Required<TocPluginOptions>, "containerClass" | "containerTag" | "pattern">) => RuleBlock;
112
+ //#endregion
113
+ //#region src/toc-plugin.d.ts
100
114
  /**
101
115
  * Generate table of contents
102
116
  *
@@ -105,6 +119,5 @@ declare const createTocBlockRule: ({ pattern, containerTag, containerClass, }: P
105
119
  * @see https://github.com/nagaozen/markdown-it-toc-done-right
106
120
  */
107
121
  declare const tocPlugin: PluginWithOptions<TocPluginOptions>;
108
-
109
- export { createRenderHeaders, createTocBlockRule, tocPlugin };
110
- export type { TocPluginOptions };
122
+ //#endregion
123
+ export { TocPluginOptions, createRenderHeaders, createTocBlockRule, tocPlugin };
package/dist/index.mjs CHANGED
@@ -1,98 +1,95 @@
1
- import { htmlEscape, resolveHeadersFromTokens, slugify } from '@mdit-vue/shared';
1
+ import { htmlEscape, resolveHeadersFromTokens, slugify } from "@mdit-vue/shared";
2
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 = (link) => linkTag === "router-link" ? ` to="${link}"` : ` href="${link}"`;
17
- const renderHeaders = (headers) => `<${listTagString}${listClassString}>${headers.map(
18
- (header) => `<${itemTagString}${itemClassString}><${linkTagString}${linkClassString}${linkTo(header.link)}>${header.title}</${linkTagString}>${header.children.length > 0 ? renderHeaders(header.children) : ""}</${itemTagString}>`
19
- ).join("")}</${listTagString}>`;
20
- return renderHeaders;
3
+ //#region src/create-render-headers.ts
4
+ const createRenderHeaders = ({ listTag, listClass, itemClass, linkTag, linkClass }) => {
5
+ const listTagString = htmlEscape(listTag);
6
+ const listClassString = listClass ? ` class="${htmlEscape(listClass)}"` : "";
7
+ const itemTagString = "li";
8
+ const itemClassString = itemClass ? ` class="${htmlEscape(itemClass)}"` : "";
9
+ const linkTagString = htmlEscape(linkTag);
10
+ const linkClassString = linkClass ? ` class="${htmlEscape(linkClass)}"` : "";
11
+ const linkTo = (link) => linkTag === "router-link" ? ` to="${link}"` : ` href="${link}"`;
12
+ const renderHeaders = (headers) => `\
13
+ <${listTagString}${listClassString}>\
14
+ ${headers.map((header) => `\
15
+ <${itemTagString}${itemClassString}>\
16
+ <${linkTagString}${linkClassString}${linkTo(header.link)}>\
17
+ ${header.title}\
18
+ </${linkTagString}>\
19
+ ${header.children.length > 0 ? renderHeaders(header.children) : ""}\
20
+ </${itemTagString}>\
21
+ `).join("")}\
22
+ </${listTagString}>`;
23
+ return renderHeaders;
21
24
  };
22
25
 
23
- const createTocBlockRule = ({
24
- pattern,
25
- containerTag,
26
- containerClass
27
- }) => (state, startLine, endLine, silent) => {
28
- if (state.sCount[startLine] - state.blkIndent >= 4) {
29
- return false;
30
- }
31
- const pos = state.bMarks[startLine] + state.tShift[startLine];
32
- const max = state.eMarks[startLine];
33
- const lineFirstToken = state.src.slice(pos, max).split(" ")[0];
34
- if (!pattern.test(lineFirstToken)) return false;
35
- if (silent) return true;
36
- state.line = startLine + 1;
37
- const tokenOpen = state.push("toc_open", containerTag, 1);
38
- tokenOpen.markup = "";
39
- tokenOpen.map = [startLine, state.line];
40
- if (containerClass) {
41
- tokenOpen.attrSet("class", containerClass);
42
- }
43
- const tokenBody = state.push("toc_body", "", 0);
44
- tokenBody.markup = lineFirstToken;
45
- tokenBody.map = [startLine, state.line];
46
- tokenBody.hidden = true;
47
- const tokenClose = state.push("toc_close", containerTag, -1);
48
- tokenClose.markup = "";
49
- tokenBody.map = [startLine, state.line];
50
- return true;
26
+ //#endregion
27
+ //#region src/create-toc-block-rule.ts
28
+ /**
29
+ * Forked and modified from markdown-it-toc-done-right
30
+ *
31
+ * - remove the `inlineOptions` support
32
+ * - use markdown-it default renderer to render token whenever possible
33
+ *
34
+ * @see https://github.com/nagaozen/markdown-it-toc-done-right
35
+ */
36
+ const createTocBlockRule = ({ pattern, containerTag, containerClass }) => (state, startLine, endLine, silent) => {
37
+ if (state.sCount[startLine] - state.blkIndent >= 4) return false;
38
+ const pos = state.bMarks[startLine] + state.tShift[startLine];
39
+ const max = state.eMarks[startLine];
40
+ const lineFirstToken = state.src.slice(pos, max).split(" ")[0];
41
+ if (!pattern.test(lineFirstToken)) return false;
42
+ if (silent) return true;
43
+ state.line = startLine + 1;
44
+ const tokenOpen = state.push("toc_open", containerTag, 1);
45
+ tokenOpen.markup = "";
46
+ tokenOpen.map = [startLine, state.line];
47
+ if (containerClass) tokenOpen.attrSet("class", containerClass);
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;
51
56
  };
52
57
 
53
- const tocPlugin = (md, {
54
- pattern = /^\[\[toc\]\]$/i,
55
- slugify: slugify$1 = slugify,
56
- format,
57
- level = [2, 3],
58
- shouldAllowNested = false,
59
- containerTag = "nav",
60
- containerClass = "table-of-contents",
61
- listTag = "ul",
62
- listClass = "",
63
- itemClass = "",
64
- linkTag = "a",
65
- linkClass = ""
66
- } = {}) => {
67
- md.block.ruler.before(
68
- "heading",
69
- "toc",
70
- createTocBlockRule({
71
- pattern,
72
- containerTag,
73
- containerClass
74
- }),
75
- {
76
- alt: ["paragraph", "reference", "blockquote"]
77
- }
78
- );
79
- const renderHeaders = createRenderHeaders({
80
- listTag,
81
- listClass,
82
- itemClass,
83
- linkTag,
84
- linkClass
85
- });
86
- md.renderer.rules.toc_body = (tokens) => renderHeaders(
87
- resolveHeadersFromTokens(tokens, {
88
- level,
89
- shouldAllowHtml: true,
90
- shouldAllowNested,
91
- shouldEscapeText: true,
92
- slugify: slugify$1,
93
- format
94
- })
95
- );
58
+ //#endregion
59
+ //#region src/toc-plugin.ts
60
+ /**
61
+ * Generate table of contents
62
+ *
63
+ * Forked and modified from markdown-it-toc-done-right:
64
+ *
65
+ * @see https://github.com/nagaozen/markdown-it-toc-done-right
66
+ */
67
+ const tocPlugin = (md, { pattern = /^\[\[toc\]\]$/i, slugify: slugify$1 = slugify, format, level = [2, 3], shouldAllowNested = false, containerTag = "nav", containerClass = "table-of-contents", listTag = "ul", listClass = "", itemClass = "", linkTag = "a", linkClass = "" } = {}) => {
68
+ md.block.ruler.before("heading", "toc", createTocBlockRule({
69
+ pattern,
70
+ containerTag,
71
+ containerClass
72
+ }), { alt: [
73
+ "paragraph",
74
+ "reference",
75
+ "blockquote"
76
+ ] });
77
+ const renderHeaders = createRenderHeaders({
78
+ listTag,
79
+ listClass,
80
+ itemClass,
81
+ linkTag,
82
+ linkClass
83
+ });
84
+ md.renderer.rules.toc_body = (tokens) => renderHeaders(resolveHeadersFromTokens(tokens, {
85
+ level,
86
+ shouldAllowHtml: true,
87
+ shouldAllowNested,
88
+ shouldEscapeText: true,
89
+ slugify: slugify$1,
90
+ format
91
+ }));
96
92
  };
97
93
 
98
- export { createRenderHeaders, createTocBlockRule, tocPlugin };
94
+ //#endregion
95
+ export { createRenderHeaders, createTocBlockRule, tocPlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdit-vue/plugin-toc",
3
- "version": "2.1.4",
3
+ "version": "3.0.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",
@@ -22,37 +22,37 @@
22
22
  "type": "module",
23
23
  "exports": {
24
24
  ".": {
25
- "import": {
26
- "types": "./dist/index.d.mts",
27
- "default": "./dist/index.mjs"
28
- },
29
- "require": {
30
- "types": "./dist/index.d.cts",
31
- "default": "./dist/index.cjs"
32
- }
33
- }
25
+ "import": "./dist/index.mjs",
26
+ "require": "./dist/index.cjs"
27
+ },
28
+ "./package.json": "./package.json"
34
29
  },
35
30
  "main": "./dist/index.cjs",
36
31
  "module": "./dist/index.mjs",
37
- "types": "./dist/index.d.ts",
32
+ "types": "./dist/index.d.cts",
38
33
  "files": [
39
34
  "./dist"
40
35
  ],
41
36
  "scripts": {
42
- "build": "unbuild",
37
+ "build": "tsdown",
43
38
  "test": "vitest"
44
39
  },
45
40
  "dependencies": {
46
- "@mdit-vue/shared": "2.1.4",
47
- "@mdit-vue/types": "2.1.4",
41
+ "@mdit-vue/shared": "3.0.0",
42
+ "@mdit-vue/types": "3.0.0",
48
43
  "@types/markdown-it": "^14.1.2",
49
44
  "markdown-it": "^14.1.0"
50
45
  },
51
46
  "devDependencies": {
52
- "markdown-it-anchor": "^9.2.0"
47
+ "markdown-it-anchor": "^9.2.0",
48
+ "tsdown": "^0.14.0",
49
+ "vitest": "^3.2.4"
50
+ },
51
+ "engines": {
52
+ "node": ">=20.0.0"
53
53
  },
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  },
57
- "gitHead": "01a6daa37fff48ea3992becfd4ba8c0f97a605ad"
57
+ "gitHead": "bbb62a47109c2932505b3f570b963f70d590914f"
58
58
  }