|
1 | 1 | import type { Plugin } from "unified"; |
2 | 2 | import type { Nodes, Root, RootContent } from "mdast"; |
3 | | -import "mdast-util-mdx-jsx"; |
4 | 3 | import { phrasing } from "mdast-util-phrasing"; |
5 | 4 |
|
6 | 5 | /** |
@@ -31,99 +30,52 @@ function transform(node: Nodes) { |
31 | 30 | transform(child); |
32 | 31 | } |
33 | 32 |
|
34 | | - node.children = transformChildren( |
35 | | - node.children.flatMap((child) => transformChild(child)), |
| 33 | + node.children = wrapDelimitedPhrasingAsTerm( |
| 34 | + node.children.flatMap((child) => isolateTermDelimiters(child)), |
36 | 35 | ); |
37 | 36 | } |
38 | 37 |
|
39 | | -function transformChild(node: RootContent): RootContent[] { |
| 38 | +function isolateTermDelimiters(node: RootContent): RootContent[] { |
40 | 39 | if (node.type !== "text") return [node]; |
41 | 40 |
|
42 | | - const transformedChildren: RootContent[] = []; |
43 | | - let text = node.value; |
44 | | - |
45 | | - if (text.startsWith("]]")) { |
46 | | - transformedChildren.push({ type: "text", value: "]]" }); |
47 | | - text = text.slice(2); |
48 | | - } |
49 | | - |
50 | | - if (text.endsWith("[[")) { |
51 | | - text = text.slice(0, -2); |
52 | | - } |
53 | | - |
54 | | - for (const segmentedText of text.split(/(\[\[.*?\]\])/)) { |
55 | | - if (segmentedText === "") { |
56 | | - continue; |
57 | | - } |
58 | | - if (segmentedText.startsWith("[[")) { |
59 | | - transformedChildren.push({ |
60 | | - type: "mdxJsxTextElement", |
61 | | - name: "Term", |
62 | | - attributes: [], |
63 | | - children: [ |
64 | | - { |
65 | | - type: "text", |
66 | | - value: segmentedText.slice(2, -2), |
67 | | - }, |
68 | | - ], |
69 | | - }); |
70 | | - } else { |
71 | | - transformedChildren.push({ |
72 | | - type: "text", |
73 | | - value: segmentedText, |
74 | | - }); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - if (node.value.endsWith("[[")) { |
79 | | - transformedChildren.push({ type: "text", value: "[[" }); |
80 | | - } |
81 | | - |
82 | | - return transformedChildren; |
| 41 | + return node.value |
| 42 | + .split(/(\[\[|\]\])/) |
| 43 | + .filter((segment) => segment !== "") |
| 44 | + .map((segment) => ({ |
| 45 | + type: "text", |
| 46 | + value: segment, |
| 47 | + })); |
83 | 48 | } |
84 | 49 |
|
85 | | -function transformChildren(children: RootContent[]): RootContent[] { |
86 | | - const transformedChildren: RootContent[] = []; |
| 50 | +function wrapDelimitedPhrasingAsTerm(children: RootContent[]): RootContent[] { |
| 51 | + const result: RootContent[] = []; |
87 | 52 |
|
88 | 53 | let i = 0; |
89 | 54 | while (i < children.length) { |
90 | | - const openingBracket = children[i]; |
91 | | - const innerElement = children[i + 1]; |
92 | | - const closingBracket = children[i + 2]; |
| 55 | + const openingDelimiter = children[i]; |
| 56 | + const innerNode = children[i + 1]; |
| 57 | + const closingDelimiter = children[i + 2]; |
93 | 58 | if ( |
94 | | - openingBracket.type !== "text" || |
95 | | - openingBracket.value !== "[[" || |
96 | | - !innerElement |
| 59 | + openingDelimiter.type === "text" && |
| 60 | + openingDelimiter.value === "[[" && |
| 61 | + innerNode && |
| 62 | + phrasing(innerNode) && |
| 63 | + closingDelimiter && |
| 64 | + closingDelimiter.type === "text" && |
| 65 | + closingDelimiter.value === "]]" |
97 | 66 | ) { |
98 | | - transformedChildren.push(openingBracket); |
99 | | - i++; |
100 | | - continue; |
101 | | - } |
102 | | - |
103 | | - if ( |
104 | | - !closingBracket || |
105 | | - closingBracket.type !== "text" || |
106 | | - closingBracket.value !== "]]" |
107 | | - ) { |
108 | | - transformedChildren.push(openingBracket, innerElement); |
109 | | - i += 2; |
110 | | - continue; |
111 | | - } |
112 | | - |
113 | | - if (!phrasing(innerElement)) { |
114 | | - transformedChildren.push(openingBracket, innerElement, closingBracket); |
| 67 | + result.push({ |
| 68 | + type: "mdxJsxTextElement", |
| 69 | + name: "Term", |
| 70 | + attributes: [], |
| 71 | + children: [innerNode], |
| 72 | + }); |
115 | 73 | i += 3; |
116 | | - continue; |
| 74 | + } else { |
| 75 | + result.push(children[i]); |
| 76 | + i += 1; |
117 | 77 | } |
118 | | - |
119 | | - transformedChildren.push({ |
120 | | - type: "mdxJsxTextElement", |
121 | | - name: "Term", |
122 | | - attributes: [], |
123 | | - children: [innerElement], |
124 | | - }); |
125 | | - i += 3; |
126 | 78 | } |
127 | 79 |
|
128 | | - return transformedChildren; |
| 80 | + return result; |
129 | 81 | } |
0 commit comments