@docusaurus/mdx-loader 0.0.0-4621 → 0.0.0-4624
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/toc/index.d.ts.map +1 -1
- package/lib/remark/toc/index.js +16 -2
- package/lib/remark/toc/index.js.map +1 -1
- package/package.json +5 -5
- package/src/remark/toc/index.ts +23 -4
- package/lib/remark/toc/search.d.ts +0 -14
- package/lib/remark/toc/search.d.ts.map +0 -1
- package/lib/remark/toc/search.js +0 -66
- package/lib/remark/toc/search.js.map +0 -1
- package/src/remark/toc/search.ts +0 -80
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAWjD,UAAU,aAAa;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAyCD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC,CA+BpC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
package/lib/remark/toc/index.js
CHANGED
|
@@ -10,7 +10,9 @@ const tslib_1 = require("tslib");
|
|
|
10
10
|
const parser_1 = require("@babel/parser");
|
|
11
11
|
const traverse_1 = (0, tslib_1.__importDefault)(require("@babel/traverse"));
|
|
12
12
|
const stringify_object_1 = (0, tslib_1.__importDefault)(require("stringify-object"));
|
|
13
|
-
const
|
|
13
|
+
const mdast_util_to_string_1 = (0, tslib_1.__importDefault)(require("mdast-util-to-string"));
|
|
14
|
+
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
15
|
+
const utils_1 = require("../utils");
|
|
14
16
|
const parseOptions = {
|
|
15
17
|
plugins: ['jsx'],
|
|
16
18
|
sourceType: 'module',
|
|
@@ -55,7 +57,19 @@ const getOrCreateExistingTargetIndex = (children, name) => {
|
|
|
55
57
|
const plugin = (options = {}) => {
|
|
56
58
|
const name = options.name || 'toc';
|
|
57
59
|
const transformer = (node) => {
|
|
58
|
-
const headings =
|
|
60
|
+
const headings = [];
|
|
61
|
+
(0, unist_util_visit_1.default)(node, 'heading', (child, _index, parent) => {
|
|
62
|
+
const value = (0, mdast_util_to_string_1.default)(child);
|
|
63
|
+
// depth:1 headings are titles and not included in the TOC
|
|
64
|
+
if (parent !== node || !value || child.depth < 2) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
headings.push({
|
|
68
|
+
value: (0, utils_1.toValue)(child),
|
|
69
|
+
id: child.data.id,
|
|
70
|
+
level: child.depth,
|
|
71
|
+
});
|
|
72
|
+
});
|
|
59
73
|
const { children } = node;
|
|
60
74
|
const targetIndex = getOrCreateExistingTargetIndex(children, name);
|
|
61
75
|
if (headings && headings.length) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,0CAAwD;AAExD,4EAAuC;AACvC,qFAA+C;AAC/C,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,0CAAwD;AAExD,4EAAuC;AACvC,qFAA+C;AAC/C,6FAA4C;AAC5C,qFAAqC;AACrC,oCAAiC;AAOjC,MAAM,YAAY,GAAkB;IAClC,OAAO,EAAE,CAAC,KAAK,CAAC;IAChB,UAAU,EAAE,QAAQ;CACrB,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,KAAW,EAAoB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;AAC5E,MAAM,UAAU,GAAG,CAAC,KAAa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,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-4624",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/mdx-loader.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@babel/parser": "^7.17.3",
|
|
22
22
|
"@babel/traverse": "^7.17.3",
|
|
23
|
-
"@docusaurus/logger": "0.0.0-
|
|
24
|
-
"@docusaurus/utils": "0.0.0-
|
|
23
|
+
"@docusaurus/logger": "0.0.0-4624",
|
|
24
|
+
"@docusaurus/utils": "0.0.0-4624",
|
|
25
25
|
"@mdx-js/mdx": "^1.6.22",
|
|
26
26
|
"escape-html": "^1.0.3",
|
|
27
27
|
"file-loader": "^6.2.0",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"webpack": "^5.69.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@docusaurus/types": "0.0.0-
|
|
39
|
+
"@docusaurus/types": "0.0.0-4624",
|
|
40
40
|
"@types/escape-html": "^1.0.1",
|
|
41
41
|
"@types/mdast": "^3.0.7",
|
|
42
42
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": ">=14"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "2973b9893d7369100eda9f94b62aeb0055da99ad"
|
|
58
58
|
}
|
package/src/remark/toc/index.ts
CHANGED
|
@@ -9,10 +9,14 @@ import {parse, type ParserOptions} from '@babel/parser';
|
|
|
9
9
|
import type {Identifier} from '@babel/types';
|
|
10
10
|
import traverse from '@babel/traverse';
|
|
11
11
|
import stringifyObject from 'stringify-object';
|
|
12
|
-
import
|
|
13
|
-
import
|
|
12
|
+
import toString from 'mdast-util-to-string';
|
|
13
|
+
import visit from 'unist-util-visit';
|
|
14
|
+
import {toValue} from '../utils';
|
|
15
|
+
|
|
16
|
+
import type {TOCItem} from '@docusaurus/types';
|
|
14
17
|
import type {Node, Parent} from 'unist';
|
|
15
|
-
import type {Literal} from 'mdast';
|
|
18
|
+
import type {Heading, Literal} from 'mdast';
|
|
19
|
+
import type {Plugin, Transformer} from 'unified';
|
|
16
20
|
|
|
17
21
|
const parseOptions: ParserOptions = {
|
|
18
22
|
plugins: ['jsx'],
|
|
@@ -70,7 +74,22 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
|
|
|
70
74
|
const name = options.name || 'toc';
|
|
71
75
|
|
|
72
76
|
const transformer: Transformer = (node) => {
|
|
73
|
-
const headings =
|
|
77
|
+
const headings: TOCItem[] = [];
|
|
78
|
+
|
|
79
|
+
visit(node, 'heading', (child: Heading, _index, parent) => {
|
|
80
|
+
const value = toString(child);
|
|
81
|
+
|
|
82
|
+
// depth:1 headings are titles and not included in the TOC
|
|
83
|
+
if (parent !== node || !value || child.depth < 2) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
headings.push({
|
|
88
|
+
value: toValue(child),
|
|
89
|
+
id: child.data!.id as string,
|
|
90
|
+
level: child.depth,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
74
93
|
const {children} = node as Parent<Literal>;
|
|
75
94
|
const targetIndex = getOrCreateExistingTargetIndex(children, name);
|
|
76
95
|
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
import type { TOCItem } from '@docusaurus/types';
|
|
8
|
-
import type { Node } from 'unist';
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
* Generate a TOC AST from the raw Markdown contents
|
|
12
|
-
*/
|
|
13
|
-
export default function search(node: Node): TOCItem[];
|
|
14
|
-
//# sourceMappingURL=search.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/search.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,OAAO,CAAC;AAUhC;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,EAAE,CAsDpD"}
|
package/lib/remark/toc/search.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
-
*
|
|
5
|
-
* This source code is licensed under the MIT license found in the
|
|
6
|
-
* LICENSE file in the root directory of this source tree.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
const tslib_1 = require("tslib");
|
|
10
|
-
const mdast_util_to_string_1 = (0, tslib_1.__importDefault)(require("mdast-util-to-string"));
|
|
11
|
-
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
12
|
-
const utils_1 = require("../utils");
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* Generate a TOC AST from the raw Markdown contents
|
|
16
|
-
*/
|
|
17
|
-
function search(node) {
|
|
18
|
-
const headings = [];
|
|
19
|
-
(0, unist_util_visit_1.default)(node, 'heading', (child, _index, parent) => {
|
|
20
|
-
const value = (0, mdast_util_to_string_1.default)(child);
|
|
21
|
-
// depth:1 headings are titles and not included in the TOC
|
|
22
|
-
if (parent !== node || !value || child.depth < 2) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
headings.push({
|
|
26
|
-
node: {
|
|
27
|
-
value: (0, utils_1.toValue)(child),
|
|
28
|
-
id: child.data.id,
|
|
29
|
-
children: [],
|
|
30
|
-
level: child.depth,
|
|
31
|
-
},
|
|
32
|
-
level: child.depth,
|
|
33
|
-
parentIndex: -1,
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
// Keep track of which previous index would be the current heading's direct
|
|
37
|
-
// parent. Each entry <i> is the last index of the `headings` array at heading
|
|
38
|
-
// level <i>. We will modify these indices as we iterate through all headings.
|
|
39
|
-
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
40
|
-
// indices 0 and 1 will remain unused.
|
|
41
|
-
const prevIndexForLevel = Array(7).fill(-1);
|
|
42
|
-
headings.forEach((curr, currIndex) => {
|
|
43
|
-
// take the last seen index for each ancestor level. the highest
|
|
44
|
-
// index will be the direct ancestor of the current heading.
|
|
45
|
-
const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
|
|
46
|
-
curr.parentIndex = Math.max(...ancestorLevelIndexes);
|
|
47
|
-
// mark that curr.level was last seen at the current index
|
|
48
|
-
prevIndexForLevel[curr.level] = currIndex;
|
|
49
|
-
});
|
|
50
|
-
const rootNodeIndexes = [];
|
|
51
|
-
// For a given parentIndex, add each Node into that parent's `children` array
|
|
52
|
-
headings.forEach((heading, i) => {
|
|
53
|
-
if (heading.parentIndex >= 0) {
|
|
54
|
-
headings[heading.parentIndex].node.children.push(heading.node);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
rootNodeIndexes.push(i);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
const toc = headings
|
|
61
|
-
.filter((_, k) => rootNodeIndexes.includes(k)) // only return root nodes
|
|
62
|
-
.map((heading) => heading.node); // only return Node, no metadata
|
|
63
|
-
return toc;
|
|
64
|
-
}
|
|
65
|
-
exports.default = search;
|
|
66
|
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CAAC,CAAC;QACpE,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,oBAAoB,CAAC,CAAC;QACrD,0DAA0D;QAC1D,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;IAC5C,CAAC,CAAC,CAAC;IAEH,MAAM,eAAe,GAAa,EAAE,CAAC;IAErC,6EAA6E;IAC7E,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC,EAAE,EAAE;QAC9B,IAAI,OAAO,CAAC,WAAW,IAAI,CAAC,EAAE;YAC5B,QAAQ,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;SAChE;aAAM;YACL,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;SACzB;IACH,CAAC,CAAC,CAAC;IAEH,MAAM,GAAG,GAAG,QAAQ;SACjB,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,yBAAyB;SACvE,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,gCAAgC;IACnE,OAAO,GAAG,CAAC;AACb,CAAC;AAtDD,yBAsDC"}
|
package/src/remark/toc/search.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
-
*
|
|
4
|
-
* This source code is licensed under the MIT license found in the
|
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import toString from 'mdast-util-to-string';
|
|
9
|
-
import visit from 'unist-util-visit';
|
|
10
|
-
import {toValue} from '../utils';
|
|
11
|
-
import type {TOCItem} from '@docusaurus/types';
|
|
12
|
-
import type {Node} from 'unist';
|
|
13
|
-
import type {Heading} from 'mdast';
|
|
14
|
-
|
|
15
|
-
// Intermediate interface for TOC algorithm
|
|
16
|
-
interface SearchItem {
|
|
17
|
-
node: TOCItem;
|
|
18
|
-
level: number;
|
|
19
|
-
parentIndex: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* Generate a TOC AST from the raw Markdown contents
|
|
25
|
-
*/
|
|
26
|
-
export default function search(node: Node): TOCItem[] {
|
|
27
|
-
const headings: SearchItem[] = [];
|
|
28
|
-
|
|
29
|
-
visit(node, 'heading', (child: Heading, _index, parent) => {
|
|
30
|
-
const value = toString(child);
|
|
31
|
-
|
|
32
|
-
// depth:1 headings are titles and not included in the TOC
|
|
33
|
-
if (parent !== node || !value || child.depth < 2) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
headings.push({
|
|
38
|
-
node: {
|
|
39
|
-
value: toValue(child),
|
|
40
|
-
id: child.data!.id as string,
|
|
41
|
-
children: [],
|
|
42
|
-
level: child.depth,
|
|
43
|
-
},
|
|
44
|
-
level: child.depth,
|
|
45
|
-
parentIndex: -1,
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Keep track of which previous index would be the current heading's direct
|
|
50
|
-
// parent. Each entry <i> is the last index of the `headings` array at heading
|
|
51
|
-
// level <i>. We will modify these indices as we iterate through all headings.
|
|
52
|
-
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
53
|
-
// indices 0 and 1 will remain unused.
|
|
54
|
-
const prevIndexForLevel = Array(7).fill(-1);
|
|
55
|
-
|
|
56
|
-
headings.forEach((curr, currIndex) => {
|
|
57
|
-
// take the last seen index for each ancestor level. the highest
|
|
58
|
-
// index will be the direct ancestor of the current heading.
|
|
59
|
-
const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
|
|
60
|
-
curr.parentIndex = Math.max(...ancestorLevelIndexes);
|
|
61
|
-
// mark that curr.level was last seen at the current index
|
|
62
|
-
prevIndexForLevel[curr.level] = currIndex;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const rootNodeIndexes: number[] = [];
|
|
66
|
-
|
|
67
|
-
// For a given parentIndex, add each Node into that parent's `children` array
|
|
68
|
-
headings.forEach((heading, i) => {
|
|
69
|
-
if (heading.parentIndex >= 0) {
|
|
70
|
-
headings[heading.parentIndex].node.children.push(heading.node);
|
|
71
|
-
} else {
|
|
72
|
-
rootNodeIndexes.push(i);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const toc = headings
|
|
77
|
-
.filter((_, k) => rootNodeIndexes.includes(k)) // only return root nodes
|
|
78
|
-
.map((heading) => heading.node); // only return Node, no metadata
|
|
79
|
-
return toc;
|
|
80
|
-
}
|