@cudoment/cudoc 0.1.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.
Files changed (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +58 -0
  3. package/dist/ast.d.ts +2 -0
  4. package/dist/ast.d.ts.map +1 -0
  5. package/dist/ast.js +2 -0
  6. package/dist/ast.js.map +1 -0
  7. package/dist/embed.d.ts +5 -0
  8. package/dist/embed.d.ts.map +1 -0
  9. package/dist/embed.js +5 -0
  10. package/dist/embed.js.map +1 -0
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +3 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/internal/core/ast/guards.d.ts +13 -0
  16. package/dist/internal/core/ast/guards.d.ts.map +1 -0
  17. package/dist/internal/core/ast/guards.js +21 -0
  18. package/dist/internal/core/ast/guards.js.map +1 -0
  19. package/dist/internal/core/ast/index.d.ts +6 -0
  20. package/dist/internal/core/ast/index.d.ts.map +1 -0
  21. package/dist/internal/core/ast/index.js +4 -0
  22. package/dist/internal/core/ast/index.js.map +1 -0
  23. package/dist/internal/core/ast/types.d.ts +49 -0
  24. package/dist/internal/core/ast/types.d.ts.map +1 -0
  25. package/dist/internal/core/ast/types.js +25 -0
  26. package/dist/internal/core/ast/types.js.map +1 -0
  27. package/dist/internal/core/ast/validate.d.ts +16 -0
  28. package/dist/internal/core/ast/validate.d.ts.map +1 -0
  29. package/dist/internal/core/ast/validate.js +90 -0
  30. package/dist/internal/core/ast/validate.js.map +1 -0
  31. package/dist/internal/core/index.d.ts +9 -0
  32. package/dist/internal/core/index.d.ts.map +1 -0
  33. package/dist/internal/core/index.js +7 -0
  34. package/dist/internal/core/index.js.map +1 -0
  35. package/dist/internal/core/mdx/index.d.ts +2 -0
  36. package/dist/internal/core/mdx/index.d.ts.map +1 -0
  37. package/dist/internal/core/mdx/index.js +2 -0
  38. package/dist/internal/core/mdx/index.js.map +1 -0
  39. package/dist/internal/core/mdx/jsx.d.ts +25 -0
  40. package/dist/internal/core/mdx/jsx.d.ts.map +1 -0
  41. package/dist/internal/core/mdx/jsx.js +43 -0
  42. package/dist/internal/core/mdx/jsx.js.map +1 -0
  43. package/dist/internal/core/query/index.d.ts +7 -0
  44. package/dist/internal/core/query/index.d.ts.map +1 -0
  45. package/dist/internal/core/query/index.js +4 -0
  46. package/dist/internal/core/query/index.js.map +1 -0
  47. package/dist/internal/core/query/nodes.d.ts +61 -0
  48. package/dist/internal/core/query/nodes.d.ts.map +1 -0
  49. package/dist/internal/core/query/nodes.js +151 -0
  50. package/dist/internal/core/query/nodes.js.map +1 -0
  51. package/dist/internal/core/query/sections.d.ts +72 -0
  52. package/dist/internal/core/query/sections.d.ts.map +1 -0
  53. package/dist/internal/core/query/sections.js +178 -0
  54. package/dist/internal/core/query/sections.js.map +1 -0
  55. package/dist/internal/core/query/tables.d.ts +32 -0
  56. package/dist/internal/core/query/tables.d.ts.map +1 -0
  57. package/dist/internal/core/query/tables.js +46 -0
  58. package/dist/internal/core/query/tables.js.map +1 -0
  59. package/dist/internal/core/selectors.d.ts +41 -0
  60. package/dist/internal/core/selectors.d.ts.map +1 -0
  61. package/dist/internal/core/selectors.js +87 -0
  62. package/dist/internal/core/selectors.js.map +1 -0
  63. package/dist/internal/core/syntax/delimiters.d.ts +45 -0
  64. package/dist/internal/core/syntax/delimiters.d.ts.map +1 -0
  65. package/dist/internal/core/syntax/delimiters.js +81 -0
  66. package/dist/internal/core/syntax/delimiters.js.map +1 -0
  67. package/dist/internal/core/syntax/heading-metadata.d.ts +59 -0
  68. package/dist/internal/core/syntax/heading-metadata.d.ts.map +1 -0
  69. package/dist/internal/core/syntax/heading-metadata.js +178 -0
  70. package/dist/internal/core/syntax/heading-metadata.js.map +1 -0
  71. package/dist/internal/core/syntax/index.d.ts +5 -0
  72. package/dist/internal/core/syntax/index.d.ts.map +1 -0
  73. package/dist/internal/core/syntax/index.js +3 -0
  74. package/dist/internal/core/syntax/index.js.map +1 -0
  75. package/dist/internal/core/walk.d.ts +40 -0
  76. package/dist/internal/core/walk.d.ts.map +1 -0
  77. package/dist/internal/core/walk.js +53 -0
  78. package/dist/internal/core/walk.js.map +1 -0
  79. package/dist/mdx.d.ts +2 -0
  80. package/dist/mdx.d.ts.map +1 -0
  81. package/dist/mdx.js +2 -0
  82. package/dist/mdx.js.map +1 -0
  83. package/dist/node/export-ast.d.ts +52 -0
  84. package/dist/node/export-ast.d.ts.map +1 -0
  85. package/dist/node/export-ast.js +117 -0
  86. package/dist/node/export-ast.js.map +1 -0
  87. package/dist/node/index.d.ts +7 -0
  88. package/dist/node/index.d.ts.map +1 -0
  89. package/dist/node/index.js +4 -0
  90. package/dist/node/index.js.map +1 -0
  91. package/dist/node/load-ast-file.d.ts +9 -0
  92. package/dist/node/load-ast-file.d.ts.map +1 -0
  93. package/dist/node/load-ast-file.js +32 -0
  94. package/dist/node/load-ast-file.js.map +1 -0
  95. package/dist/node/load-ast.d.ts +11 -0
  96. package/dist/node/load-ast.d.ts.map +1 -0
  97. package/dist/node/load-ast.js +17 -0
  98. package/dist/node/load-ast.js.map +1 -0
  99. package/dist/node/paths.d.ts +31 -0
  100. package/dist/node/paths.d.ts.map +1 -0
  101. package/dist/node/paths.js +54 -0
  102. package/dist/node/paths.js.map +1 -0
  103. package/dist/query.d.ts +2 -0
  104. package/dist/query.d.ts.map +1 -0
  105. package/dist/query.js +2 -0
  106. package/dist/query.js.map +1 -0
  107. package/dist/syntax.d.ts +2 -0
  108. package/dist/syntax.d.ts.map +1 -0
  109. package/dist/syntax.js +2 -0
  110. package/dist/syntax.js.map +1 -0
  111. package/package.json +94 -0
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Declarative selectors.
3
+ *
4
+ * A caller cannot pass a predicate function through a bundler config, so the
5
+ * conditions a transform needs are described as plain data and evaluated here.
6
+ */
7
+ /** Concatenates the text of a phrasing subtree, ignoring node kinds. */
8
+ export const getInlineText = (nodes) => nodes.map((node) => getInlineTextFromNode(node)).join("");
9
+ const getInlineTextFromNode = (node) => {
10
+ if (node.type === "text")
11
+ return node.value;
12
+ if ("value" in node && typeof node.value === "string")
13
+ return node.value;
14
+ if ("children" in node && Array.isArray(node.children)) {
15
+ return getInlineText(node.children);
16
+ }
17
+ return "";
18
+ };
19
+ /** Collapses runs of whitespace and lowercases, for tolerant comparison. */
20
+ export const normalizeTitle = (value) => value.replace(/\s+/g, " ").trim().toLowerCase();
21
+ /** Drops every space, so a title written with or without one still matches. */
22
+ export const compactTitle = (value) => normalizeTitle(value).replace(/\s/g, "");
23
+ /** Collapses whitespace but keeps case, for exact header matching. */
24
+ export const normalizeHeaderText = (value) => value.replace(/\s+/g, " ").trim();
25
+ export const assertSectionSelector = (value, optionPath) => {
26
+ if (!value || typeof value !== "object") {
27
+ throw new TypeError(`${optionPath} must be an object`);
28
+ }
29
+ const { depth, titles } = value;
30
+ if (!Array.isArray(titles) ||
31
+ titles.length === 0 ||
32
+ !titles.every((title) => typeof title === "string" && title.trim())) {
33
+ throw new TypeError(`${optionPath}.titles must be a non-empty string array`);
34
+ }
35
+ if (depth !== undefined) {
36
+ const depths = Array.isArray(depth) ? depth : [depth];
37
+ if (!depths.every((one) => Number.isInteger(one) && one >= 1 && one <= 6)) {
38
+ throw new TypeError(`${optionPath}.depth must be 1-6, or an array of them`);
39
+ }
40
+ }
41
+ return value;
42
+ };
43
+ /**
44
+ * Tests a heading against a selector.
45
+ *
46
+ * Both a whitespace-normalized and a space-free comparison are accepted, so an
47
+ * author writing the title with or without an internal space still matches.
48
+ */
49
+ export const matchesSectionHeading = (heading, selector,
50
+ /** Text extractor, so a caller can strip generated nodes first. */
51
+ readText = (node) => getInlineText(node.children)) => {
52
+ if (selector.depth !== undefined) {
53
+ const depths = Array.isArray(selector.depth)
54
+ ? selector.depth
55
+ : [selector.depth];
56
+ if (!depths.includes(heading.depth))
57
+ return false;
58
+ }
59
+ const text = normalizeTitle(readText(heading));
60
+ const compact = compactTitle(text);
61
+ return selector.titles.some((title) => normalizeTitle(title) === text || compactTitle(title) === compact);
62
+ };
63
+ /**
64
+ * Finds the heading that introduces the node at `index`.
65
+ *
66
+ * The search continues through ancestors, because a table may sit inside a
67
+ * container whose own siblings hold the heading.
68
+ */
69
+ export const findPreviousHeading = (parent, index, ancestors = []) => {
70
+ for (let i = index - 1; i >= 0; i -= 1) {
71
+ const candidate = parent.children[i];
72
+ if (candidate?.type === "heading")
73
+ return candidate;
74
+ }
75
+ for (let i = ancestors.length - 1; i >= 0; i -= 1) {
76
+ const ancestor = ancestors[i];
77
+ if (!ancestor)
78
+ continue;
79
+ for (let siblingIndex = ancestor.index - 1; siblingIndex >= 0; siblingIndex -= 1) {
80
+ const candidate = ancestor.parent.children[siblingIndex];
81
+ if (candidate?.type === "heading")
82
+ return candidate;
83
+ }
84
+ }
85
+ return undefined;
86
+ };
87
+ //# sourceMappingURL=selectors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"selectors.js","sourceRoot":"","sources":["../../../src/internal/core/selectors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,wEAAwE;AACxE,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAiC,EAAU,EAAE,CACzE,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;AAE3D,MAAM,qBAAqB,GAAG,CAAC,IAAqB,EAAU,EAAE;IAC9D,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM;QAAE,OAAO,IAAI,CAAC,KAAK,CAAA;IAC3C,IAAI,OAAO,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC,KAAK,CAAA;IACxE,IAAI,UAAU,IAAI,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QACvD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;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,MAAc,EACd,KAAa,EACb,YAAgC,EAAE,EACb,EAAE;IACvB,KAAK,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACvC,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;QACpC,IAAI,SAAS,EAAE,IAAI,KAAK,SAAS;YAAE,OAAO,SAAS,CAAA;IACrD,CAAC;IAED,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAClD,MAAM,QAAQ,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA;QAC7B,IAAI,CAAC,QAAQ;YAAE,SAAQ;QAEvB,KACE,IAAI,YAAY,GAAG,QAAQ,CAAC,KAAK,GAAG,CAAC,EACrC,YAAY,IAAI,CAAC,EACjB,YAAY,IAAI,CAAC,EACjB,CAAC;YACD,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAA;YACxD,IAAI,SAAS,EAAE,IAAI,KAAK,SAAS;gBAAE,OAAO,SAAS,CAAA;QACrD,CAAC;IACH,CAAC;IAED,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA"}
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Delimiter-based syntax patterns.
3
+ *
4
+ * Options that reach a plugin through a bundler config must survive JSON
5
+ * serialization, so callers describe inline syntax with a delimiter pair such
6
+ * as `["(#", ")"]` instead of passing a `RegExp`. Patterns are built here.
7
+ *
8
+ * Delimiters are matched after the document is parsed, so they must be inert to
9
+ * the parser itself. In MDX that rules out `{` and `}`, which start a
10
+ * JavaScript expression, and `<`, which starts an element.
11
+ */
12
+ /** An opening and a closing marker, in that order. */
13
+ export type DelimiterPair = readonly [open: string, close: string];
14
+ export declare const assertDelimiterPair: (value: unknown, optionPath: string) => DelimiterPair;
15
+ /**
16
+ * Builds the pattern that matches `open`, a lazily captured body, then `close`.
17
+ * The body is lazy so the first closing marker ends the match, which is how a
18
+ * negated character class behaves for a single-character closing delimiter.
19
+ */
20
+ export declare const createDelimiterPattern: ([open, close]: DelimiterPair, flags?: string) => RegExp;
21
+ export declare const createGlobalDelimiterPattern: (pair: DelimiterPair) => RegExp;
22
+ export type DelimitedPart = {
23
+ type: "text";
24
+ value: string;
25
+ } | {
26
+ type: "delimited";
27
+ value: string;
28
+ };
29
+ /**
30
+ * Splits `value` into plain text and delimited segments.
31
+ *
32
+ * A segment whose body is blank is left in place as text: an author writing
33
+ * `(@)` means the literal characters, not an empty badge.
34
+ */
35
+ export declare const splitByDelimiters: (value: string, pair: DelimiterPair) => DelimitedPart[];
36
+ /** Reads the first non-blank delimited body, or `undefined` when there is none. */
37
+ export declare const extractDelimitedValue: (value: string, pair: DelimiterPair) => string | undefined;
38
+ export declare const hasDelimitedValue: (value: string, pair: DelimiterPair) => boolean;
39
+ /**
40
+ * Removes every non-blank delimited segment for each pair. Blank bodies are
41
+ * kept, matching {@link splitByDelimiters}.
42
+ */
43
+ export declare const stripDelimited: (value: string, pairs: readonly DelimiterPair[]) => string;
44
+ export declare const stripDelimitedAndTrimEnd: (value: string, pairs: readonly DelimiterPair[]) => string;
45
+ //# sourceMappingURL=delimiters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delimiters.d.ts","sourceRoot":"","sources":["../../../../src/internal/core/syntax/delimiters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,sDAAsD;AACtD,MAAM,MAAM,aAAa,GAAG,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;AAOlE,eAAO,MAAM,mBAAmB,GAC9B,OAAO,OAAO,EACd,YAAY,MAAM,KACjB,aAiBF,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GACjC,eAAe,aAAa,EAC5B,cAAU,KACT,MAC2E,CAAA;AAE9E,eAAO,MAAM,4BAA4B,GAAI,MAAM,aAAa,KAAG,MAChC,CAAA;AAEnC,MAAM,MAAM,aAAa,GACvB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAA;AAExE;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAC5B,OAAO,MAAM,EACb,MAAM,aAAa,KAClB,aAAa,EA6Bf,CAAA;AAED,mFAAmF;AACnF,eAAO,MAAM,qBAAqB,GAChC,OAAO,MAAM,EACb,MAAM,aAAa,KAClB,MAAM,GAAG,SAIX,CAAA;AAED,eAAO,MAAM,iBAAiB,GAC5B,OAAO,MAAM,EACb,MAAM,aAAa,KAClB,OAAmD,CAAA;AAEtD;;;GAGG;AACH,eAAO,MAAM,cAAc,GACzB,OAAO,MAAM,EACb,OAAO,SAAS,aAAa,EAAE,KAC9B,MAQA,CAAA;AAEH,eAAO,MAAM,wBAAwB,GACnC,OAAO,MAAM,EACb,OAAO,SAAS,aAAa,EAAE,KAC9B,MAAgD,CAAA"}
@@ -0,0 +1,81 @@
1
+ /**
2
+ * Delimiter-based syntax patterns.
3
+ *
4
+ * Options that reach a plugin through a bundler config must survive JSON
5
+ * serialization, so callers describe inline syntax with a delimiter pair such
6
+ * as `["(#", ")"]` instead of passing a `RegExp`. Patterns are built here.
7
+ *
8
+ * Delimiters are matched after the document is parsed, so they must be inert to
9
+ * the parser itself. In MDX that rules out `{` and `}`, which start a
10
+ * JavaScript expression, and `<`, which starts an element.
11
+ */
12
+ const REGEXP_SPECIAL_CHARACTERS = /[.*+?^${}()|[\]\\]/g;
13
+ const escapeRegExp = (value) => value.replace(REGEXP_SPECIAL_CHARACTERS, "\\$&");
14
+ export const assertDelimiterPair = (value, optionPath) => {
15
+ if (!Array.isArray(value) ||
16
+ value.length !== 2 ||
17
+ typeof value[0] !== "string" ||
18
+ typeof value[1] !== "string") {
19
+ throw new TypeError(`${optionPath} must be a pair of strings, for example ["(#", ")"]`);
20
+ }
21
+ if (!value[0] || !value[1]) {
22
+ throw new TypeError(`${optionPath} must not contain an empty delimiter`);
23
+ }
24
+ return [value[0], value[1]];
25
+ };
26
+ /**
27
+ * Builds the pattern that matches `open`, a lazily captured body, then `close`.
28
+ * The body is lazy so the first closing marker ends the match, which is how a
29
+ * negated character class behaves for a single-character closing delimiter.
30
+ */
31
+ export const createDelimiterPattern = ([open, close], flags = "") => new RegExp(`${escapeRegExp(open)}([\\s\\S]*?)${escapeRegExp(close)}`, flags);
32
+ export const createGlobalDelimiterPattern = (pair) => createDelimiterPattern(pair, "g");
33
+ /**
34
+ * Splits `value` into plain text and delimited segments.
35
+ *
36
+ * A segment whose body is blank is left in place as text: an author writing
37
+ * `(@)` means the literal characters, not an empty badge.
38
+ */
39
+ export const splitByDelimiters = (value, pair) => {
40
+ const matches = [...value.matchAll(createGlobalDelimiterPattern(pair))];
41
+ if (matches.length === 0)
42
+ return [{ type: "text", value }];
43
+ const parts = [];
44
+ let lastIndex = 0;
45
+ for (const match of matches) {
46
+ const matchIndex = match.index;
47
+ if (typeof matchIndex !== "number")
48
+ continue;
49
+ const fullMatch = match[0];
50
+ const body = (match[1] ?? "").trim();
51
+ if (!body)
52
+ continue;
53
+ if (matchIndex > lastIndex) {
54
+ const beforeText = value.substring(lastIndex, matchIndex);
55
+ if (beforeText)
56
+ parts.push({ type: "text", value: beforeText });
57
+ }
58
+ parts.push({ type: "delimited", value: body });
59
+ lastIndex = matchIndex + fullMatch.length;
60
+ }
61
+ if (lastIndex < value.length) {
62
+ const afterText = value.substring(lastIndex);
63
+ if (afterText)
64
+ parts.push({ type: "text", value: afterText });
65
+ }
66
+ return parts;
67
+ };
68
+ /** Reads the first non-blank delimited body, or `undefined` when there is none. */
69
+ export const extractDelimitedValue = (value, pair) => {
70
+ const match = value.match(createDelimiterPattern(pair));
71
+ const body = match?.[1]?.trim();
72
+ return body || undefined;
73
+ };
74
+ export const hasDelimitedValue = (value, pair) => createDelimiterPattern(pair).test(value);
75
+ /**
76
+ * Removes every non-blank delimited segment for each pair. Blank bodies are
77
+ * kept, matching {@link splitByDelimiters}.
78
+ */
79
+ export const stripDelimited = (value, pairs) => pairs.reduce((current, pair) => current.replace(createGlobalDelimiterPattern(pair), (match, body) => (body.trim() ? "" : match)), value);
80
+ export const stripDelimitedAndTrimEnd = (value, pairs) => stripDelimited(value, pairs).trimEnd();
81
+ //# sourceMappingURL=delimiters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delimiters.js","sourceRoot":"","sources":["../../../../src/internal/core/syntax/delimiters.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAKH,MAAM,yBAAyB,GAAG,qBAAqB,CAAA;AAEvD,MAAM,YAAY,GAAG,CAAC,KAAa,EAAU,EAAE,CAC7C,KAAK,CAAC,OAAO,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAA;AAElD,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,KAAc,EACd,UAAkB,EACH,EAAE;IACjB,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACrB,KAAK,CAAC,MAAM,KAAK,CAAC;QAClB,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ;QAC5B,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,EAC5B,CAAC;QACD,MAAM,IAAI,SAAS,CACjB,GAAG,UAAU,qDAAqD,CACnE,CAAA;IACH,CAAC;IAED,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,GAAG,UAAU,sCAAsC,CAAC,CAAA;IAC1E,CAAC;IAED,OAAO,CAAC,KAA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E,CAChB,OAAO,CAAC,OAAO,CACb,4BAA4B,CAAC,IAAI,CAAC,EAClC,CAAC,KAAa,EAAE,IAAY,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAC5D,EACH,KAAK,CACN,CAAA;AAEH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,KAAa,EACb,KAA+B,EACvB,EAAE,CAAC,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,OAAO,EAAE,CAAA"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Heading metadata syntax: an explicit anchor id and an optional badge written
3
+ * inline in the heading text.
4
+ *
5
+ * The same parsing runs during compilation and during reference checking, so a
6
+ * checker never disagrees with the rendered anchor.
7
+ */
8
+ import type { Heading, PhrasingContent, Root } from "mdast";
9
+ import type { MdxJsxTextElement } from "mdast-util-mdx-jsx";
10
+ import { type DelimiterPair } from "./delimiters.js";
11
+ export declare const DEFAULT_HEADING_DEPTHS: readonly [2, 3, 4, 5];
12
+ export declare const DEFAULT_ID_DELIMITERS: DelimiterPair;
13
+ export declare const DEFAULT_BADGE_DELIMITERS: DelimiterPair;
14
+ export declare const DEFAULT_ANCHOR_NAME = "Anchor";
15
+ export type AnchorOptions = {
16
+ /** Element name emitted for the anchor. */
17
+ name?: string;
18
+ /** Attribute carrying the id. */
19
+ idAttribute?: string;
20
+ /** Attribute carrying the heading level, or `false` to omit it. */
21
+ levelAttribute?: string | false;
22
+ /** Prefix placed before the depth, so depth 2 becomes `h2` by default. */
23
+ levelPrefix?: string;
24
+ /** Attribute carrying the badge text. */
25
+ badgeAttribute?: string;
26
+ };
27
+ export type HeadingMetadataOptions = {
28
+ /** Heading depths that may carry metadata. */
29
+ depths?: number[];
30
+ idDelimiters?: DelimiterPair;
31
+ badgeDelimiters?: DelimiterPair;
32
+ anchor?: AnchorOptions;
33
+ };
34
+ export type ResolvedHeadingMetadataOptions = {
35
+ depths: number[];
36
+ idDelimiters: DelimiterPair;
37
+ badgeDelimiters: DelimiterPair;
38
+ anchor: {
39
+ name: string;
40
+ idAttribute: string;
41
+ levelAttribute: string | false;
42
+ levelPrefix: string;
43
+ badgeAttribute: string;
44
+ };
45
+ };
46
+ export declare const resolveHeadingMetadataOptions: (options?: HeadingMetadataOptions) => ResolvedHeadingMetadataOptions;
47
+ /**
48
+ * Moves the metadata written in a heading's text into an anchor element.
49
+ *
50
+ * When several text nodes carry metadata the last id and the last badge win,
51
+ * and only the text node that holds the final match has its trailing space
52
+ * trimmed, so the visible heading text keeps its internal spacing.
53
+ */
54
+ export declare const transformHeadingAnchor: (node: Heading, options: ResolvedHeadingMetadataOptions) => void;
55
+ /** Applies {@link transformHeadingAnchor} to every heading in a tree. */
56
+ export declare const transformHeadingAnchors: (tree: Root, options: ResolvedHeadingMetadataOptions) => void;
57
+ export declare const findAnchorNode: (nodes: PhrasingContent[], anchorName?: string) => MdxJsxTextElement | undefined;
58
+ export declare const extractAnchorId: (anchor: MdxJsxTextElement | undefined, idAttribute?: string) => string | undefined;
59
+ //# sourceMappingURL=heading-metadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heading-metadata.d.ts","sourceRoot":"","sources":["../../../../src/internal/core/syntax/heading-metadata.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,IAAI,EAAQ,MAAM,OAAO,CAAA;AACjE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAE3D,OAAO,EAML,KAAK,aAAa,EACnB,MAAM,iBAAiB,CAAA;AAGxB,eAAO,MAAM,sBAAsB,uBAAwB,CAAA;AAC3D,eAAO,MAAM,qBAAqB,EAAE,aAA2B,CAAA;AAC/D,eAAO,MAAM,wBAAwB,EAAE,aAA2B,CAAA;AAClE,eAAO,MAAM,mBAAmB,WAAW,CAAA;AAE3C,MAAM,MAA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B,QAAQ,iBAAiB,GAAG,SAAS,EACrC,oBAAkB,KACjB,MAAM,GAAG,SAAqD,CAAA"}
@@ -0,0 +1,178 @@
1
+ /**
2
+ * Heading metadata syntax: an explicit anchor id and an optional badge written
3
+ * inline in the heading text.
4
+ *
5
+ * The same parsing runs during compilation and during reference checking, so a
6
+ * checker never disagrees with the rendered anchor.
7
+ */
8
+ import { visit } from "unist-util-visit";
9
+ import { assertDelimiterPair, hasDelimitedValue, extractDelimitedValue, stripDelimited, stripDelimitedAndTrimEnd, } from "./delimiters.js";
10
+ import { readStringAttribute, setStringAttribute } from "../mdx/jsx.js";
11
+ export const DEFAULT_HEADING_DEPTHS = [2, 3, 4, 5];
12
+ export const DEFAULT_ID_DELIMITERS = ["(#", ")"];
13
+ export const DEFAULT_BADGE_DELIMITERS = ["(@", ")"];
14
+ export const DEFAULT_ANCHOR_NAME = "Anchor";
15
+ const assertDepths = (value, optionPath) => {
16
+ if (!Array.isArray(value) ||
17
+ value.length === 0 ||
18
+ !value.every((depth) => Number.isInteger(depth) && depth >= 1 && depth <= 6)) {
19
+ throw new TypeError(`${optionPath} must be a non-empty array of 1-6`);
20
+ }
21
+ return [...value];
22
+ };
23
+ export const resolveHeadingMetadataOptions = (options = {}) => {
24
+ const anchor = options.anchor ?? {};
25
+ const levelAttribute = anchor.levelAttribute === false
26
+ ? false
27
+ : (anchor.levelAttribute ?? "headerLevel");
28
+ if (levelAttribute !== false && typeof levelAttribute !== "string") {
29
+ throw new TypeError("headingMetadata.anchor.levelAttribute must be a string or false");
30
+ }
31
+ return {
32
+ depths: options.depths
33
+ ? assertDepths(options.depths, "headingMetadata.depths")
34
+ : [...DEFAULT_HEADING_DEPTHS],
35
+ idDelimiters: options.idDelimiters
36
+ ? assertDelimiterPair(options.idDelimiters, "headingMetadata.idDelimiters")
37
+ : DEFAULT_ID_DELIMITERS,
38
+ badgeDelimiters: options.badgeDelimiters
39
+ ? assertDelimiterPair(options.badgeDelimiters, "headingMetadata.badgeDelimiters")
40
+ : DEFAULT_BADGE_DELIMITERS,
41
+ anchor: {
42
+ name: anchor.name ?? DEFAULT_ANCHOR_NAME,
43
+ idAttribute: anchor.idAttribute ?? "id",
44
+ levelAttribute,
45
+ levelPrefix: anchor.levelPrefix ?? "h",
46
+ badgeAttribute: anchor.badgeAttribute ?? "badge",
47
+ },
48
+ };
49
+ };
50
+ const isMetadataContainer = (node) => ["strong", "emphasis", "delete"].includes(node.type) &&
51
+ "children" in node &&
52
+ Array.isArray(node.children);
53
+ const collectMetadataTextNodes = (nodes) => {
54
+ const textNodes = [];
55
+ for (const node of nodes) {
56
+ if (node.type === "text") {
57
+ textNodes.push(node);
58
+ }
59
+ else if (isMetadataContainer(node)) {
60
+ textNodes.push(...collectMetadataTextNodes(node.children));
61
+ }
62
+ }
63
+ return textNodes;
64
+ };
65
+ const pruneEmptyMetadataNodes = (nodes) => {
66
+ const nextNodes = [];
67
+ for (const node of nodes) {
68
+ if (node.type === "text") {
69
+ if (node.value)
70
+ nextNodes.push(node);
71
+ continue;
72
+ }
73
+ if (!isMetadataContainer(node)) {
74
+ nextNodes.push(node);
75
+ continue;
76
+ }
77
+ node.children = pruneEmptyMetadataNodes(node.children);
78
+ if (node.children.length > 0)
79
+ nextNodes.push(node);
80
+ }
81
+ return nextNodes;
82
+ };
83
+ const createAnchorNode = ({ badge, depth, existingAnchor, id, options, }) => {
84
+ const { anchor } = options;
85
+ const attributes = existingAnchor
86
+ ? structuredClone(existingAnchor.attributes)
87
+ : [];
88
+ if (id) {
89
+ setStringAttribute(attributes, anchor.idAttribute, id);
90
+ if (anchor.levelAttribute !== false) {
91
+ setStringAttribute(attributes, anchor.levelAttribute, `${anchor.levelPrefix}${depth}`);
92
+ }
93
+ }
94
+ if (badge)
95
+ setStringAttribute(attributes, anchor.badgeAttribute, badge);
96
+ return {
97
+ ...(existingAnchor ? structuredClone(existingAnchor) : {}),
98
+ type: "mdxJsxTextElement",
99
+ name: anchor.name,
100
+ attributes,
101
+ children: existingAnchor ? structuredClone(existingAnchor.children) : [],
102
+ };
103
+ };
104
+ /**
105
+ * Moves the metadata written in a heading's text into an anchor element.
106
+ *
107
+ * When several text nodes carry metadata the last id and the last badge win,
108
+ * and only the text node that holds the final match has its trailing space
109
+ * trimmed, so the visible heading text keeps its internal spacing.
110
+ */
111
+ export const transformHeadingAnchor = (node, options) => {
112
+ if (!options.depths.includes(node.depth))
113
+ return;
114
+ const pairs = [options.badgeDelimiters, options.idDelimiters];
115
+ const textNodes = collectMetadataTextNodes(node.children);
116
+ const candidates = textNodes.flatMap((textNode) => {
117
+ const badge = extractDelimitedValue(textNode.value, options.badgeDelimiters);
118
+ const id = extractDelimitedValue(textNode.value, options.idDelimiters);
119
+ return badge || id ? [{ textNode, badge, id }] : [];
120
+ });
121
+ const target = candidates[candidates.length - 1];
122
+ if (!target)
123
+ return;
124
+ const badge = [...candidates]
125
+ .reverse()
126
+ .find((candidate) => candidate.badge)?.badge;
127
+ const id = [...candidates].reverse().find((candidate) => candidate.id)?.id;
128
+ for (const textNode of textNodes) {
129
+ if (!hasDelimitedValue(textNode.value, options.badgeDelimiters) &&
130
+ !hasDelimitedValue(textNode.value, options.idDelimiters)) {
131
+ continue;
132
+ }
133
+ const nextText = textNode === target.textNode
134
+ ? stripDelimitedAndTrimEnd(textNode.value, pairs)
135
+ : stripDelimited(textNode.value, pairs);
136
+ textNode.value = nextText.trim() ? nextText : "";
137
+ }
138
+ node.children = pruneEmptyMetadataNodes(node.children);
139
+ const anchorIdx = node.children.findIndex((child) => child.type === "mdxJsxTextElement" &&
140
+ "name" in child &&
141
+ child.name === options.anchor.name);
142
+ const existingAnchor = anchorIdx === -1
143
+ ? undefined
144
+ : node.children[anchorIdx];
145
+ const anchorNode = createAnchorNode({
146
+ badge,
147
+ depth: node.depth,
148
+ existingAnchor,
149
+ id,
150
+ options,
151
+ });
152
+ const textIdx = node.children.indexOf(target.textNode);
153
+ if (anchorIdx !== -1) {
154
+ node.children[anchorIdx] = anchorNode;
155
+ }
156
+ else if (textIdx !== -1) {
157
+ node.children.splice(textIdx + 1, 0, anchorNode);
158
+ }
159
+ else {
160
+ node.children.push(anchorNode);
161
+ }
162
+ };
163
+ /** Applies {@link transformHeadingAnchor} to every heading in a tree. */
164
+ export const transformHeadingAnchors = (tree, options) => {
165
+ visit(tree, "heading", (heading) => {
166
+ transformHeadingAnchor(heading, options);
167
+ });
168
+ };
169
+ export const findAnchorNode = (nodes, anchorName = DEFAULT_ANCHOR_NAME) => {
170
+ for (const node of nodes) {
171
+ if ("attributes" in node && node.name === anchorName) {
172
+ return node;
173
+ }
174
+ }
175
+ return undefined;
176
+ };
177
+ export const extractAnchorId = (anchor, idAttribute = "id") => readStringAttribute(anchor, idAttribute);
178
+ //# sourceMappingURL=heading-metadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heading-metadata.js","sourceRoot":"","sources":["../../../../src/internal/core/syntax/heading-metadata.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EACL,mBAAmB,EACnB,iBAAiB,EACjB,qBAAqB,EACrB,cAAc,EACd,wBAAwB,GAEzB,MAAM,iBAAiB,CAAA;AACxB,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAEvE,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAU,CAAA;AAC3D,MAAM,CAAC,MAAM,qBAAqB,GAAkB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;AAC/D,MAAM,CAAC,MAAM,wBAAwB,GAAkB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;AAClE,MAAM,CAAC,MAAM,mBAAmB,GAAG,QAAQ,CAAA;AAoC3C,MAAM,YAAY,GAAG,CAAC,KAAc,EAAE,UAAkB,EAAY,EAAE;IACpE,IACE,CAAC,KAAK,CAAC,OAAO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CAAC,CAAA;IAEtD,IAAI,SAAS,KAAK,CAAC,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,UAAU,CAAA;IACvC,CAAC;SAAM,IAAI,OAAO,KAAK,CAAC,CAAC,EAAE,CAAC;QAC1B,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,GAAG,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAA;IAClD,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IAChC,CAAC;AACH,CAAC,CAAA;AAED,yEAAyE;AACzE,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,IAAU,EACV,OAAuC,EACjC,EAAE;IACR,KAAK,CAAC,IAAI,EAAE,SAAS,EAAE,CAAC,OAAgB,EAAE,EAAE;QAC1C,sBAAsB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IAC1C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,KAAwB,EACxB,aAAqB,mBAAmB,EACT,EAAE;IACjC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,YAAY,IAAI,IAAI,IAAI,IAAI,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YACrD,OAAO,IAAI,CAAA;QACb,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,MAAqC,EACrC,WAAW,GAAG,IAAI,EACE,EAAE,CAAC,mBAAmB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA"}
@@ -0,0 +1,5 @@
1
+ export { assertDelimiterPair, createDelimiterPattern, createGlobalDelimiterPattern, extractDelimitedValue, hasDelimitedValue, splitByDelimiters, stripDelimited, stripDelimitedAndTrimEnd, } from "./delimiters.js";
2
+ export type { DelimitedPart, DelimiterPair } from "./delimiters.js";
3
+ export { DEFAULT_ANCHOR_NAME, DEFAULT_BADGE_DELIMITERS, DEFAULT_HEADING_DEPTHS, DEFAULT_ID_DELIMITERS, extractAnchorId, findAnchorNode, resolveHeadingMetadataOptions, transformHeadingAnchor, transformHeadingAnchors, } from "./heading-metadata.js";
4
+ export type { AnchorOptions, HeadingMetadataOptions, ResolvedHeadingMetadataOptions, } from "./heading-metadata.js";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/internal/core/syntax/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,4BAA4B,EAC5B,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,wBAAwB,GACzB,MAAM,iBAAiB,CAAA;AACxB,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACnE,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,eAAe,EACf,cAAc,EACd,6BAA6B,EAC7B,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,uBAAuB,CAAA;AAC9B,YAAY,EACV,aAAa,EACb,sBAAsB,EACtB,8BAA8B,GAC/B,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,3 @@
1
+ export { assertDelimiterPair, createDelimiterPattern, createGlobalDelimiterPattern, extractDelimitedValue, hasDelimitedValue, splitByDelimiters, stripDelimited, stripDelimitedAndTrimEnd, } from "./delimiters.js";
2
+ export { DEFAULT_ANCHOR_NAME, DEFAULT_BADGE_DELIMITERS, DEFAULT_HEADING_DEPTHS, DEFAULT_ID_DELIMITERS, extractAnchorId, findAnchorNode, resolveHeadingMetadataOptions, transformHeadingAnchor, transformHeadingAnchors, } from "./heading-metadata.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/internal/core/syntax/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,sBAAsB,EACtB,4BAA4B,EAC5B,qBAAqB,EACrB,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,wBAAwB,GACzB,MAAM,iBAAiB,CAAA;AAExB,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,EACtB,qBAAqB,EACrB,eAAe,EACf,cAAc,EACd,6BAA6B,EAC7B,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,uBAAuB,CAAA"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Single-pass traversal shared by every cudoc transform.
3
+ *
4
+ * Transforms run in one walk rather than one walk each, because ordering
5
+ * between them is a contract: a transform that rewrites a whole table has to
6
+ * see cells that earlier transforms already normalized.
7
+ */
8
+ import type { Node, Parent, Root } from "mdast";
9
+ /** Where a node sits in its parent, recorded for each ancestor. */
10
+ export type AncestorLocation = {
11
+ index: number;
12
+ parent: Parent;
13
+ };
14
+ /** State carried through one document. Extend it per plugin. */
15
+ export type TransformState = {
16
+ /** Raw source text, when the host provided it. Needed to read table cells. */
17
+ source: string | null;
18
+ };
19
+ export type TransformContext<State extends TransformState = TransformState> = {
20
+ ancestors?: AncestorLocation[];
21
+ index?: number;
22
+ node: Node;
23
+ parent?: Parent;
24
+ state: State;
25
+ };
26
+ export type Transform<State extends TransformState = TransformState> = (context: TransformContext<State>) => void;
27
+ /**
28
+ * Runs `preTransforms` on the way down and `postTransforms` on the way up.
29
+ *
30
+ * A pre transform may replace the node it was given; the walk re-reads the slot
31
+ * from the parent afterwards so the replacement, not the original, is what gets
32
+ * descended into and what the post transforms see.
33
+ */
34
+ export declare const walk: <State extends TransformState>({ postTransforms, preTransforms, state, tree, }: {
35
+ postTransforms: Transform<State>[];
36
+ preTransforms: Transform<State>[];
37
+ state: State;
38
+ tree: Root;
39
+ }) => void;
40
+ //# sourceMappingURL=walk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.d.ts","sourceRoot":"","sources":["../../../src/internal/core/walk.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAA;AAE/C,mEAAmE;AACnE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,gEAAgE;AAChE,MAAM,MAAM,cAAc,GAAG;IAC3B,8EAA8E;IAC9E,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACtB,CAAA;AAED,MAAM,MAAM,gBAAgB,CAAC,KAAK,SAAS,cAAc,GAAG,cAAc,IAAI;IAC5E,SAAS,CAAC,EAAE,gBAAgB,EAAE,CAAA;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED,MAAM,MAAM,SAAS,CAAC,KAAK,SAAS,cAAc,GAAG,cAAc,IAAI,CACrE,OAAO,EAAE,gBAAgB,CAAC,KAAK,CAAC,KAC7B,IAAI,CAAA;AAKT;;;;;;GAMG;AACH,eAAO,MAAM,IAAI,GAAI,KAAK,SAAS,cAAc,EAAE,iDAKhD;IACD,cAAc,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,CAAA;IAClC,aAAa,EAAE,SAAS,CAAC,KAAK,CAAC,EAAE,CAAA;IACjC,KAAK,EAAE,KAAK,CAAA;IACZ,IAAI,EAAE,IAAI,CAAA;CACX,KAAG,IAqDH,CAAA"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Single-pass traversal shared by every cudoc transform.
3
+ *
4
+ * Transforms run in one walk rather than one walk each, because ordering
5
+ * between them is a contract: a transform that rewrites a whole table has to
6
+ * see cells that earlier transforms already normalized.
7
+ */
8
+ const hasChildren = (node) => "children" in node && Array.isArray(node.children);
9
+ /**
10
+ * Runs `preTransforms` on the way down and `postTransforms` on the way up.
11
+ *
12
+ * A pre transform may replace the node it was given; the walk re-reads the slot
13
+ * from the parent afterwards so the replacement, not the original, is what gets
14
+ * descended into and what the post transforms see.
15
+ */
16
+ export const walk = ({ postTransforms, preTransforms, state, tree, }) => {
17
+ const runTransforms = (transforms, context) => {
18
+ for (const transform of transforms)
19
+ transform(context);
20
+ };
21
+ const visitNode = (node, parent, index, ancestors = []) => {
22
+ runTransforms(preTransforms, {
23
+ ancestors,
24
+ index,
25
+ node,
26
+ parent,
27
+ state,
28
+ });
29
+ const currentNode = parent && typeof index === "number" ? parent.children[index] : node;
30
+ if (!currentNode)
31
+ return;
32
+ if (hasChildren(currentNode)) {
33
+ const childAncestors = parent && typeof index === "number"
34
+ ? [...ancestors, { index, parent }]
35
+ : ancestors;
36
+ for (let childIndex = 0; childIndex < currentNode.children.length; childIndex += 1) {
37
+ const child = currentNode.children[childIndex];
38
+ if (!child)
39
+ continue;
40
+ visitNode(child, currentNode, childIndex, childAncestors);
41
+ }
42
+ }
43
+ runTransforms(postTransforms, {
44
+ ancestors,
45
+ index,
46
+ node: currentNode,
47
+ parent,
48
+ state,
49
+ });
50
+ };
51
+ visitNode(tree);
52
+ };
53
+ //# sourceMappingURL=walk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.js","sourceRoot":"","sources":["../../../src/internal/core/walk.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA4BH,MAAM,WAAW,GAAG,CAAC,IAAU,EAAkB,EAAE,CACjD,UAAU,IAAI,IAAI,IAAI,KAAK,CAAC,OAAO,CAAE,IAAe,CAAC,QAAQ,CAAC,CAAA;AAEhE;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAA+B,EACjD,cAAc,EACd,aAAa,EACb,KAAK,EACL,IAAI,GAML,EAAQ,EAAE;IACT,MA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ACjB,CAAC,CAAA"}
package/dist/mdx.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./internal/core/mdx/index.js";
2
+ //# sourceMappingURL=mdx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mdx.d.ts","sourceRoot":"","sources":["../src/mdx.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA"}
package/dist/mdx.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./internal/core/mdx/index.js";
2
+ //# sourceMappingURL=mdx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mdx.js","sourceRoot":"","sources":["../src/mdx.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA"}