@docusaurus/mdx-loader 3.9.2-canary-6495 → 3.9.2-canary-6526
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/lib/remark/headings/index.d.ts.map +1 -1
- package/lib/remark/headings/index.js +102 -35
- package/lib/remark/headings/index.js.map +1 -1
- package/lib/remark/toc/utils.d.ts.map +1 -1
- package/lib/remark/toc/utils.js +1 -0
- package/lib/remark/toc/utils.js.map +1 -1
- package/package.json +6 -6
- package/src/remark/headings/index.ts +124 -46
- package/src/remark/toc/utils.ts +1 -0
- package/src/types.d.mts +4 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AACjD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AACjD,OAAO,KAAK,EAAU,IAAI,EAAO,MAAM,OAAO,CAAC;AAE/C,MAAM,WAAW,aAAa;IAC5B,mBAAmB,EAAE,OAAO,CAAC;CAC9B;AA6FD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,aAAa,EAAE,EAAE,IAAI,CAgDzC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -8,48 +8,115 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
/* Based on remark-slug (https://github.com/remarkjs/remark-slug) and gatsby-remark-autolink-headers (https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-autolink-headers) */
|
|
10
10
|
const utils_1 = require("@docusaurus/utils");
|
|
11
|
+
function getCommentContentHeadingId(comment) {
|
|
12
|
+
// If the comment has spaces, we only consider the first part
|
|
13
|
+
const firstPart = comment.trim().split(' ')[0];
|
|
14
|
+
// We ignore comments that don't start with # on purpose
|
|
15
|
+
// Forcing users to use a leading # is more explicit
|
|
16
|
+
// In the future it's possible we'd want to allow other types of comments
|
|
17
|
+
// For example class comments like {/* .my-class */}
|
|
18
|
+
if (firstPart?.startsWith('#')) {
|
|
19
|
+
return firstPart.slice(1) || undefined;
|
|
20
|
+
}
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
function getCommentHeadingId(heading) {
|
|
24
|
+
const lastChild = heading.children.at(-1);
|
|
25
|
+
// MDX comment: {/* my-id */} or {/* #my-id */}
|
|
26
|
+
if (lastChild &&
|
|
27
|
+
lastChild.type === 'mdxTextExpression' &&
|
|
28
|
+
lastChild.data?.estree) {
|
|
29
|
+
const program = lastChild.data.estree;
|
|
30
|
+
// We only extract the id from single-comment MDX expressions
|
|
31
|
+
// ✅ {/* #my-id */}
|
|
32
|
+
// ❌ {/* #my-id */ /* #my-id2 */}
|
|
33
|
+
// ❌ {someExpression /* #my-id */}
|
|
34
|
+
if (program.body.length === 0 && program.comments?.length === 1) {
|
|
35
|
+
const commentContent = program.comments[0].value;
|
|
36
|
+
return getCommentContentHeadingId(commentContent);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// HTML comment: <!-- my-id --> or <!-- #my-id -->
|
|
40
|
+
if (lastChild?.type === 'html') {
|
|
41
|
+
const match = /^<!--(?<comment>[\s\S]*)-->$/.exec(lastChild.value);
|
|
42
|
+
if (match?.groups?.comment) {
|
|
43
|
+
const commentContent = match.groups.comment;
|
|
44
|
+
return getCommentContentHeadingId(commentContent);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
// Try to find an explicit id in MD/MDX comments
|
|
50
|
+
function extractCommentId(heading) {
|
|
51
|
+
const commentId = getCommentHeadingId(heading);
|
|
52
|
+
if (commentId) {
|
|
53
|
+
// Remove the last comment node
|
|
54
|
+
heading.children.pop();
|
|
55
|
+
// Trim the trailing space from the last text node ("text " → "text")
|
|
56
|
+
const newLast = heading.children.at(-1);
|
|
57
|
+
if (newLast?.type === 'text') {
|
|
58
|
+
newLast.value = newLast.value.trimEnd();
|
|
59
|
+
}
|
|
60
|
+
return commentId;
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
// Try to find an explicit id in the heading text (classic {#id} syntax)
|
|
65
|
+
function extractClassicSyntaxHeadingId(heading, headingText) {
|
|
66
|
+
const parsedHeading = (0, utils_1.parseMarkdownHeadingId)(headingText, 'classic');
|
|
67
|
+
// Remove the heading text from its id (legacy syntax)
|
|
68
|
+
if (parsedHeading.id) {
|
|
69
|
+
// When there's an id, it is always in the last child node
|
|
70
|
+
const lastNode = heading.children.at(-1);
|
|
71
|
+
if (heading.children.length > 1) {
|
|
72
|
+
const lastNodeText = (0, utils_1.parseMarkdownHeadingId)(lastNode.value, 'classic').text;
|
|
73
|
+
// When the last part contains text + id, remove the id
|
|
74
|
+
if (lastNodeText) {
|
|
75
|
+
lastNode.value = lastNodeText;
|
|
76
|
+
}
|
|
77
|
+
// When last part contains only the id: completely remove that node
|
|
78
|
+
else {
|
|
79
|
+
heading.children.pop();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
lastNode.value = parsedHeading.text;
|
|
84
|
+
}
|
|
85
|
+
return parsedHeading.id;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
11
89
|
const plugin = function plugin({ anchorsMaintainCase, }) {
|
|
12
90
|
return async (root) => {
|
|
13
91
|
const { toString } = await import('mdast-util-to-string');
|
|
14
92
|
const { visit } = await import('unist-util-visit');
|
|
93
|
+
function getHeadingText(heading) {
|
|
94
|
+
const headingTextNodes = heading.children.filter(({ type }) => !['html', 'jsx'].includes(type));
|
|
95
|
+
return toString(headingTextNodes.length > 0 ? headingTextNodes : heading);
|
|
96
|
+
}
|
|
15
97
|
const slugs = (0, utils_1.createSlugger)();
|
|
16
|
-
visit(root, 'heading', (
|
|
17
|
-
const data =
|
|
18
|
-
const properties =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const heading = toString(headingTextNodes.length > 0 ? headingTextNodes : headingNode);
|
|
26
|
-
// Support explicit heading IDs
|
|
27
|
-
const parsedHeading = (0, utils_1.parseMarkdownHeadingId)(heading);
|
|
28
|
-
id =
|
|
29
|
-
parsedHeading.id ??
|
|
30
|
-
slugs.slug(heading, { maintainCase: anchorsMaintainCase });
|
|
31
|
-
if (parsedHeading.id) {
|
|
32
|
-
// When there's an id, it is always in the last child node
|
|
33
|
-
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
|
34
|
-
// node):
|
|
35
|
-
// ## part1 *part2* part3 {#id}
|
|
36
|
-
const lastNode = headingNode.children[headingNode.children.length - 1];
|
|
37
|
-
if (headingNode.children.length > 1) {
|
|
38
|
-
const lastNodeText = (0, utils_1.parseMarkdownHeadingId)(lastNode.value).text;
|
|
39
|
-
// When last part contains test+id, remove the id
|
|
40
|
-
if (lastNodeText) {
|
|
41
|
-
lastNode.value = lastNodeText;
|
|
42
|
-
}
|
|
43
|
-
// When last part contains only the id: completely remove that node
|
|
44
|
-
else {
|
|
45
|
-
headingNode.children.pop();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
lastNode.value = parsedHeading.text;
|
|
50
|
-
}
|
|
98
|
+
visit(root, 'heading', (heading) => {
|
|
99
|
+
const data = heading.data ?? (heading.data = {});
|
|
100
|
+
const properties = data.hProperties ?? (data.hProperties = {});
|
|
101
|
+
// Gives the ability to provide/write a remark plugin that sets an id
|
|
102
|
+
// When an id is already set, we use it instead of running our own plugin
|
|
103
|
+
function extractAlreadyExistingId() {
|
|
104
|
+
if (properties.id) {
|
|
105
|
+
// Not sure why we need to slugify here, historical code
|
|
106
|
+
return slugs.slug(properties.id, { maintainCase: true });
|
|
51
107
|
}
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
function extractIdFromText() {
|
|
111
|
+
const headingText = getHeadingText(heading);
|
|
112
|
+
return (extractClassicSyntaxHeadingId(heading, headingText) ??
|
|
113
|
+
slugs.slug(headingText, { maintainCase: anchorsMaintainCase }));
|
|
52
114
|
}
|
|
115
|
+
// All the ways we can extract an id, ordered by priority
|
|
116
|
+
// /!\ the extraction methods can perform AST cleanup side effects
|
|
117
|
+
const id = extractAlreadyExistingId() ??
|
|
118
|
+
extractCommentId(heading) ??
|
|
119
|
+
extractIdFromText();
|
|
53
120
|
data.id = id;
|
|
54
121
|
properties.id = id;
|
|
55
122
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,gMAAgM;AAEhM,6CAAwE;AAQxE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,gMAAgM;AAEhM,6CAAwE;AAQxE,SAAS,0BAA0B,CAAC,OAAe;IACjD,6DAA6D;IAC7D,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/C,wDAAwD;IACxD,oDAAoD;IACpD,yEAAyE;IACzE,oDAAoD;IACpD,IAAI,SAAS,EAAE,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC/B,OAAO,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;IACzC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,mBAAmB,CAAC,OAAgB;IAC3C,MAAM,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC,EAAE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AAG,IAAI,CASP;AAED,wBAAgB,aAAa,CAC3B,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,MAAM,GACjB,IAAI,IAAI,QAAQ,CAyBlB;AAED,wBAAsB,sBAAsB,CAAC,EAC3C,aAAa,EACb,QAAQ,GACT,EAAE;IACD,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,QAAQ,CAAC;CACpB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAkEpB;AA6CD,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,eAAe,GAAG,OAAO,GAAG,iBAAiB,EACnD,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,MAAM,GACnC,MAAM,CAsBR"}
|
package/lib/remark/toc/utils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/remark/toc/utils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAkBH,sDAIC;AAED,4CAMC;AAED,sDAMC;AAED,4DASC;AAID,8DAiBC;AAED,sCA4BC;AAED,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../../src/remark/toc/utils.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAkBH,sDAIC;AAED,4CAMC;AAED,sDAMC;AAED,4DASC;AAID,8DAiBC;AAED,sCA4BC;AAED,wDAwEC;AA6CD,gDAyBC;;AAlPD,sEAAqC;AAgBrC,SAAgB,qBAAqB,CAAC,OAAgB;IACpD,OAAO,OAAO,CAAC,IAAI,CAAC,MAAM,CACxB,CAAC,IAAI,EAA6B,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,mBAAmB,CACvE,CAAC;AACJ,CAAC;AAED,SAAgB,gBAAgB,CAAC,IAAU;IACzC,IAAI,IAAI,CAAC,IAAI,KAAK,mBAAmB,EAAE,CAAC;QACtC,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,UAAU,GAAI,IAA0B,CAAC,MAAM,CAAC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ClB,KAAK,mBAAmB,CAAC,CAAC,CAAC;YACzB,OAAO,uBAAuB,CAAC,IAAyB,EAAE,QAAQ,CAAC,CAAC;QACtE,CAAC;QACD,KAAK,MAAM;YACT,OAAO,IAAA,qBAAU,EAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,KAAK,SAAS;YACZ,OAAO,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC3C,KAAK,YAAY;YACf,OAAO,SAAS,IAAA,qBAAU,EAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;QAClD,KAAK,UAAU;YACb,OAAO,OAAO,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,CAAC;QACzD,KAAK,QAAQ;YACX,OAAO,WAAW,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,WAAW,CAAC;QACjE,KAAK,QAAQ;YACX,OAAO,QAAQ,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,QAAQ,CAAC;QAC3D,KAAK,MAAM;YACT,OAAO,iBAAiB,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC3C;YACE,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "3.9.2-canary-
|
|
3
|
+
"version": "3.9.2-canary-6526",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/logger": "3.9.2-canary-
|
|
22
|
-
"@docusaurus/utils": "3.9.2-canary-
|
|
23
|
-
"@docusaurus/utils-validation": "3.9.2-canary-
|
|
21
|
+
"@docusaurus/logger": "3.9.2-canary-6526",
|
|
22
|
+
"@docusaurus/utils": "3.9.2-canary-6526",
|
|
23
|
+
"@docusaurus/utils-validation": "3.9.2-canary-6526",
|
|
24
24
|
"@mdx-js/mdx": "^3.0.0",
|
|
25
25
|
"@slorber/remark-comment": "^1.0.0",
|
|
26
26
|
"escape-html": "^1.0.3",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"webpack": "^5.88.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@docusaurus/types": "3.9.2-canary-
|
|
47
|
+
"@docusaurus/types": "3.9.2-canary-6526",
|
|
48
48
|
"@types/escape-html": "^1.0.2",
|
|
49
49
|
"@types/mdast": "^4.0.2",
|
|
50
50
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"engines": {
|
|
65
65
|
"node": ">=20.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "a37ae138931cb80b31664f86a34f97671c00e409"
|
|
68
68
|
}
|
|
@@ -9,12 +9,103 @@
|
|
|
9
9
|
|
|
10
10
|
import {parseMarkdownHeadingId, createSlugger} from '@docusaurus/utils';
|
|
11
11
|
import type {Plugin, Transformer} from 'unified';
|
|
12
|
-
import type {Root, Text} from 'mdast';
|
|
12
|
+
import type {Heading, Root, Text} from 'mdast';
|
|
13
13
|
|
|
14
14
|
export interface PluginOptions {
|
|
15
15
|
anchorsMaintainCase: boolean;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function getCommentContentHeadingId(comment: string): string | undefined {
|
|
19
|
+
// If the comment has spaces, we only consider the first part
|
|
20
|
+
const firstPart = comment.trim().split(' ')[0];
|
|
21
|
+
// We ignore comments that don't start with # on purpose
|
|
22
|
+
// Forcing users to use a leading # is more explicit
|
|
23
|
+
// In the future it's possible we'd want to allow other types of comments
|
|
24
|
+
// For example class comments like {/* .my-class */}
|
|
25
|
+
if (firstPart?.startsWith('#')) {
|
|
26
|
+
return firstPart.slice(1) || undefined;
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getCommentHeadingId(heading: Heading): string | undefined {
|
|
32
|
+
const lastChild = heading.children.at(-1);
|
|
33
|
+
|
|
34
|
+
// MDX comment: {/* my-id */} or {/* #my-id */}
|
|
35
|
+
if (
|
|
36
|
+
lastChild &&
|
|
37
|
+
lastChild.type === 'mdxTextExpression' &&
|
|
38
|
+
lastChild.data?.estree
|
|
39
|
+
) {
|
|
40
|
+
const program = lastChild.data.estree;
|
|
41
|
+
// We only extract the id from single-comment MDX expressions
|
|
42
|
+
// ✅ {/* #my-id */}
|
|
43
|
+
// ❌ {/* #my-id */ /* #my-id2 */}
|
|
44
|
+
// ❌ {someExpression /* #my-id */}
|
|
45
|
+
if (program.body.length === 0 && program.comments?.length === 1) {
|
|
46
|
+
const commentContent = program.comments[0]!.value;
|
|
47
|
+
return getCommentContentHeadingId(commentContent);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// HTML comment: <!-- my-id --> or <!-- #my-id -->
|
|
52
|
+
if (lastChild?.type === 'html') {
|
|
53
|
+
const match = /^<!--(?<comment>[\s\S]*)-->$/.exec(
|
|
54
|
+
(lastChild as unknown as {value: string}).value,
|
|
55
|
+
);
|
|
56
|
+
if (match?.groups?.comment) {
|
|
57
|
+
const commentContent = match.groups.comment;
|
|
58
|
+
return getCommentContentHeadingId(commentContent);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Try to find an explicit id in MD/MDX comments
|
|
66
|
+
function extractCommentId(heading: Heading) {
|
|
67
|
+
const commentId = getCommentHeadingId(heading);
|
|
68
|
+
if (commentId) {
|
|
69
|
+
// Remove the last comment node
|
|
70
|
+
heading.children.pop();
|
|
71
|
+
// Trim the trailing space from the last text node ("text " → "text")
|
|
72
|
+
const newLast = heading.children.at(-1);
|
|
73
|
+
if (newLast?.type === 'text') {
|
|
74
|
+
newLast.value = newLast.value.trimEnd();
|
|
75
|
+
}
|
|
76
|
+
return commentId;
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Try to find an explicit id in the heading text (classic {#id} syntax)
|
|
82
|
+
function extractClassicSyntaxHeadingId(heading: Heading, headingText: string) {
|
|
83
|
+
const parsedHeading = parseMarkdownHeadingId(headingText, 'classic');
|
|
84
|
+
// Remove the heading text from its id (legacy syntax)
|
|
85
|
+
if (parsedHeading.id) {
|
|
86
|
+
// When there's an id, it is always in the last child node
|
|
87
|
+
const lastNode = heading.children.at(-1) as Text;
|
|
88
|
+
if (heading.children.length > 1) {
|
|
89
|
+
const lastNodeText = parseMarkdownHeadingId(
|
|
90
|
+
lastNode.value,
|
|
91
|
+
'classic',
|
|
92
|
+
).text;
|
|
93
|
+
// When the last part contains text + id, remove the id
|
|
94
|
+
if (lastNodeText) {
|
|
95
|
+
lastNode.value = lastNodeText;
|
|
96
|
+
}
|
|
97
|
+
// When last part contains only the id: completely remove that node
|
|
98
|
+
else {
|
|
99
|
+
heading.children.pop();
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
lastNode.value = parsedHeading.text;
|
|
103
|
+
}
|
|
104
|
+
return parsedHeading.id;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
18
109
|
const plugin: Plugin<PluginOptions[], Root> = function plugin({
|
|
19
110
|
anchorsMaintainCase,
|
|
20
111
|
}): Transformer<Root> {
|
|
@@ -22,56 +113,43 @@ const plugin: Plugin<PluginOptions[], Root> = function plugin({
|
|
|
22
113
|
const {toString} = await import('mdast-util-to-string');
|
|
23
114
|
const {visit} = await import('unist-util-visit');
|
|
24
115
|
|
|
116
|
+
function getHeadingText(heading: Heading) {
|
|
117
|
+
const headingTextNodes = heading.children.filter(
|
|
118
|
+
({type}) => !['html', 'jsx'].includes(type),
|
|
119
|
+
);
|
|
120
|
+
return toString(headingTextNodes.length > 0 ? headingTextNodes : heading);
|
|
121
|
+
}
|
|
122
|
+
|
|
25
123
|
const slugs = createSlugger();
|
|
26
|
-
visit(root, 'heading', (
|
|
27
|
-
const data =
|
|
28
|
-
const properties =
|
|
29
|
-
id: string;
|
|
30
|
-
};
|
|
31
|
-
let {id} = properties;
|
|
32
|
-
|
|
33
|
-
if (id) {
|
|
34
|
-
id = slugs.slug(id, {maintainCase: true});
|
|
35
|
-
} else {
|
|
36
|
-
const headingTextNodes = headingNode.children.filter(
|
|
37
|
-
({type}) => !['html', 'jsx'].includes(type),
|
|
38
|
-
);
|
|
39
|
-
const heading = toString(
|
|
40
|
-
headingTextNodes.length > 0 ? headingTextNodes : headingNode,
|
|
41
|
-
);
|
|
124
|
+
visit(root, 'heading', (heading) => {
|
|
125
|
+
const data = heading.data ?? (heading.data = {});
|
|
126
|
+
const properties = data.hProperties ?? (data.hProperties = {});
|
|
42
127
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
id
|
|
47
|
-
|
|
48
|
-
slugs.slug(
|
|
49
|
-
|
|
50
|
-
if (parsedHeading.id) {
|
|
51
|
-
// When there's an id, it is always in the last child node
|
|
52
|
-
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
|
53
|
-
// node):
|
|
54
|
-
// ## part1 *part2* part3 {#id}
|
|
55
|
-
const lastNode = headingNode.children[
|
|
56
|
-
headingNode.children.length - 1
|
|
57
|
-
] as Text;
|
|
58
|
-
|
|
59
|
-
if (headingNode.children.length > 1) {
|
|
60
|
-
const lastNodeText = parseMarkdownHeadingId(lastNode.value).text;
|
|
61
|
-
// When last part contains test+id, remove the id
|
|
62
|
-
if (lastNodeText) {
|
|
63
|
-
lastNode.value = lastNodeText;
|
|
64
|
-
}
|
|
65
|
-
// When last part contains only the id: completely remove that node
|
|
66
|
-
else {
|
|
67
|
-
headingNode.children.pop();
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
lastNode.value = parsedHeading.text;
|
|
71
|
-
}
|
|
128
|
+
// Gives the ability to provide/write a remark plugin that sets an id
|
|
129
|
+
// When an id is already set, we use it instead of running our own plugin
|
|
130
|
+
function extractAlreadyExistingId() {
|
|
131
|
+
if (properties.id) {
|
|
132
|
+
// Not sure why we need to slugify here, historical code
|
|
133
|
+
return slugs.slug(properties.id, {maintainCase: true});
|
|
72
134
|
}
|
|
135
|
+
return undefined;
|
|
73
136
|
}
|
|
74
137
|
|
|
138
|
+
function extractIdFromText() {
|
|
139
|
+
const headingText = getHeadingText(heading);
|
|
140
|
+
return (
|
|
141
|
+
extractClassicSyntaxHeadingId(heading, headingText) ??
|
|
142
|
+
slugs.slug(headingText, {maintainCase: anchorsMaintainCase})
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// All the ways we can extract an id, ordered by priority
|
|
147
|
+
// /!\ the extraction methods can perform AST cleanup side effects
|
|
148
|
+
const id =
|
|
149
|
+
extractAlreadyExistingId() ??
|
|
150
|
+
extractCommentId(heading) ??
|
|
151
|
+
extractIdFromText();
|
|
152
|
+
|
|
75
153
|
data.id = id;
|
|
76
154
|
properties.id = id;
|
|
77
155
|
});
|
package/src/remark/toc/utils.ts
CHANGED