Skip to content

Commit 5e44918

Browse files
committed
remarkTermをリファクタリング
1 parent 0913a74 commit 5e44918

File tree

2 files changed

+34
-80
lines changed

2 files changed

+34
-80
lines changed

src/remark/remark-term.ts

Lines changed: 32 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Plugin } from "unified";
22
import type { Nodes, Root, RootContent } from "mdast";
3-
import "mdast-util-mdx-jsx";
43
import { phrasing } from "mdast-util-phrasing";
54

65
/**
@@ -31,99 +30,52 @@ function transform(node: Nodes) {
3130
transform(child);
3231
}
3332

34-
node.children = transformChildren(
35-
node.children.flatMap((child) => transformChild(child)),
33+
node.children = wrapDelimitedPhrasingAsTerm(
34+
node.children.flatMap((child) => isolateTermDelimiters(child)),
3635
);
3736
}
3837

39-
function transformChild(node: RootContent): RootContent[] {
38+
function isolateTermDelimiters(node: RootContent): RootContent[] {
4039
if (node.type !== "text") return [node];
4140

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+
}));
8348
}
8449

85-
function transformChildren(children: RootContent[]): RootContent[] {
86-
const transformedChildren: RootContent[] = [];
50+
function wrapDelimitedPhrasingAsTerm(children: RootContent[]): RootContent[] {
51+
const result: RootContent[] = [];
8752

8853
let i = 0;
8954
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];
9358
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 === "]]"
9766
) {
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+
});
11573
i += 3;
116-
continue;
74+
} else {
75+
result.push(children[i]);
76+
i += 1;
11777
}
118-
119-
transformedChildren.push({
120-
type: "mdxJsxTextElement",
121-
name: "Term",
122-
attributes: [],
123-
children: [innerElement],
124-
});
125-
i += 3;
12678
}
12779

128-
return transformedChildren;
80+
return result;
12981
}

src/types/mdast-mdx-jsx.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// mdastのノードにMDX JSXのノードを追加
2+
import "mdast-util-mdx-jsx";

0 commit comments

Comments
 (0)