Skip to content

Commit 8f80c38

Browse files
committed
feat(rehype-proc-term): termについてそれを直接囲む要素を表すtermContainerを追加(一部のタグのみ)
1 parent f83f1ed commit 8f80c38

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

examples/render/output/def.mdx.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function _createMdxContent(props) {
4444
text: "用語",
4545
reference: "用語",
4646
refIndex: JSON.parse("0"),
47+
termContainer: JSON.parse("null"),
4748
}),
4849
"を",
4950
"展",
@@ -85,6 +86,7 @@ function _createMdxContent(props) {
8586
text: "用語",
8687
reference: "用語",
8788
refIndex: JSON.parse("1"),
89+
termContainer: JSON.parse("null"),
8890
}),
8991
],
9092
}),
@@ -103,12 +105,14 @@ function _createMdxContent(props) {
103105
text: "用語",
104106
reference: "用語",
105107
refIndex: JSON.parse("2"),
108+
termContainer: JSON.parse("null"),
106109
}),
107110
"の",
108111
_jsx(Term, {
109112
text: "定義",
110113
reference: "定義",
111114
refIndex: JSON.parse("0"),
115+
termContainer: JSON.parse("null"),
112116
}),
113117
"を",
114118
"す",
@@ -133,6 +137,7 @@ function _createMdxContent(props) {
133137
text: "用語",
134138
reference: "用語",
135139
refIndex: JSON.parse("3"),
140+
termContainer: JSON.parse('"h2"'),
136141
}),
137142
"も",
138143
"置",

examples/render/output/save-0.mdx.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function _createMdxContent(props) {
3434
text: "定義",
3535
reference: "定義",
3636
refIndex: JSON.parse("0"),
37+
termContainer: JSON.parse("null"),
3738
}),
3839
"は",
3940
],
@@ -65,6 +66,7 @@ function _createMdxContent(props) {
6566
text: "定義",
6667
reference: "定義",
6768
refIndex: JSON.parse("1"),
69+
termContainer: JSON.parse("null"),
6870
}),
6971
"は",
7072
],
@@ -96,6 +98,7 @@ function _createMdxContent(props) {
9698
text: "定義",
9799
reference: "定義",
98100
refIndex: JSON.parse("2"),
101+
termContainer: JSON.parse("null"),
99102
}),
100103
"は",
101104
],
@@ -127,6 +130,7 @@ function _createMdxContent(props) {
127130
text: "定義",
128131
reference: "定義",
129132
refIndex: JSON.parse("3"),
133+
termContainer: JSON.parse("null"),
130134
}),
131135
"は",
132136
],

src/rehype-proc-term.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,33 @@ export type RehypeProcTermPluginParams = {
3131
readonly termProcessor: TermProcessorProtocol;
3232
};
3333

34+
export type TermContainer = "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
35+
export const parseConatiner = (parent: unknown): TermContainer | null => {
36+
if (typeof parent !== "object" || parent === null) return null;
37+
if (!("type" in parent) || typeof parent.type !== "string") return null;
38+
if (!("tagName" in parent) || typeof parent.tagName !== "string") return null;
39+
if (parent.type !== "element") return null;
40+
41+
switch (parent.tagName) {
42+
case "h1":
43+
case "h2":
44+
case "h3":
45+
case "h4":
46+
case "h5":
47+
case "h6":
48+
return parent.tagName;
49+
}
50+
return null;
51+
};
52+
3453
export type RehypeProcTermPlugin = import("unified").Plugin<
3554
[RehypeProcTermPluginParams],
3655
Root
3756
>;
3857
const rehypeProcTerm: RehypeProcTermPlugin = ({ termProcessor }) => {
3958
return async (tree) => {
4059
const refCount = new Map<string, number>();
41-
await visitAsync(tree, async (node) => {
60+
await visitAsync(tree, async (node, parents) => {
4261
if (node.type !== "text" || typeof node.value !== "string") return;
4362

4463
const newChildren: MdxJsxTextElement["children"] = [];
@@ -59,6 +78,7 @@ const rehypeProcTerm: RehypeProcTermPlugin = ({ termProcessor }) => {
5978
break;
6079
case "term": {
6180
const refIndex = refCount.get(parsed.term) ?? 0;
81+
const termContainer = parseConatiner(parents.at(-1)?.node);
6282
refCount.set(parsed.term, refIndex + 1);
6383
newChildren.push({
6484
type: "mdxJsxTextElement",
@@ -81,6 +101,17 @@ const rehypeProcTerm: RehypeProcTermPlugin = ({ termProcessor }) => {
81101
},
82102
},
83103
},
104+
{
105+
type: "mdxJsxAttribute",
106+
name: "termContainer",
107+
value: {
108+
type: "mdxJsxAttributeValueExpression",
109+
value: "",
110+
data: {
111+
estree: estreeJsonParseOf(termContainer),
112+
},
113+
},
114+
},
84115
],
85116
children: [],
86117
});

0 commit comments

Comments
 (0)