Skip to content

Commit e210202

Browse files
authored
minor: parseMetaの実装、options属性をmeta属性に変更 (#5)
* wip * fmt * wip * done * lint fix * re-render example * lint-fix
1 parent e260042 commit e210202

File tree

11 files changed

+309
-80
lines changed

11 files changed

+309
-80
lines changed

examples/render/input/katex-0.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ return x;
1111
<KatexDef _="foo" />
1212
<KatexDef _={"bar"} />
1313

14-
```math!def
14+
```math def
1515
\def\idmat{I}
1616
\gdef\Tr#1{\operatorname{Tr}\left[{#1}\right]}
1717
```
@@ -29,6 +29,6 @@ return x;
2929

3030
<KatexReset />
3131

32-
```math {1,2,3} {1-2} lang=js
32+
```math
3333
\Tr \idmat = 1
3434
```

examples/render/input/math-corner.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
5+
6+
$ab\}c$

examples/render/input/save-0.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ A=B
1818

1919
---
2020

21-
```math!save
21+
```math $
2222
A=B
2323
```
2424

2525
定義は[$]となる。
2626

2727
---
2828

29-
```math!save $foo inline
29+
```math $foo inline
3030
A=B
3131
```
3232

examples/render/output/ex1.mdx.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@ function _createMdxContent(props) {
129129
globalContext: JSON.parse('""'),
130130
defContext: globalThis[_rehypeKatexContext0],
131131
content: "a+b",
132-
options: "math inline",
132+
meta: JSON.parse(
133+
'{"category":"show","mode":"inline","subCategory":"normal"}',
134+
),
133135
}),
134136
" は ",
135137
_jsx(LumaKatex, {
136138
globalContext: JSON.parse('""'),
137139
defContext: globalThis[_rehypeKatexContext0],
138140
content: "b+a",
139-
options: "math inline",
141+
meta: JSON.parse(
142+
'{"category":"show","mode":"inline","subCategory":"normal"}',
143+
),
140144
}),
141145
" と等しい。",
142146
],
@@ -147,7 +151,9 @@ function _createMdxContent(props) {
147151
defContext: globalThis[_rehypeKatexContext0],
148152
content:
149153
"\\begin{aligned} a + b &= b + a \\\\ a \\times b &= b \\times a \\end{aligned}",
150-
options: true,
154+
meta: JSON.parse(
155+
'{"category":"show","mode":"display","subCategory":"normal"}',
156+
),
151157
}),
152158
(() => {
153159
delete globalThis[_rehypeKatexContext0];

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function _createMdxContent(props) {
77
if (!LumaMdxLayout) _missingMdxReference("LumaMdxLayout", true);
88
return _jsxs(LumaMdxLayout, {
99
file: JSON.parse(
10-
'{"data":{},"history":[],"messages":[],"value":"import { someDef } from \\"./someDef.txt\\";\\n\\n{\\n(() => {\\nlet x = 4;\\nreturn x;\\n})()\\n}\\n\\n<KatexDef _={someDef} />\\n<KatexDef _=\\"foo\\" />\\n<KatexDef _={\\"bar\\"} />\\n\\n```math!def\\n\\\\def\\\\idmat{I}\\n\\\\gdef\\\\Tr#1{\\\\operatorname{Tr}\\\\left[{#1}\\\\right]}\\n```\\n\\n```math\\n\\\\Tr \\\\idmat = 1\\n```\\n\\n```math trans\\n1 + 1\\n= S(O) + S(O)\\n= S(O + S(O))\\n= S(S(O))\\n```\\n\\n<KatexReset />\\n\\n```math {1,2,3} {1-2} lang=js\\n\\\\Tr \\\\idmat = 1\\n```\\n"}',
10+
'{"data":{},"history":[],"messages":[],"value":"import { someDef } from \\"./someDef.txt\\";\\n\\n{\\n(() => {\\nlet x = 4;\\nreturn x;\\n})()\\n}\\n\\n<KatexDef _={someDef} />\\n<KatexDef _=\\"foo\\" />\\n<KatexDef _={\\"bar\\"} />\\n\\n```math def\\n\\\\def\\\\idmat{I}\\n\\\\gdef\\\\Tr#1{\\\\operatorname{Tr}\\\\left[{#1}\\\\right]}\\n```\\n\\n```math\\n\\\\Tr \\\\idmat = 1\\n```\\n\\n```math trans\\n1 + 1\\n= S(O) + S(O)\\n= S(O + S(O))\\n= S(S(O))\\n```\\n\\n<KatexReset />\\n\\n```math\\n\\\\Tr \\\\idmat = 1\\n```\\n"}',
1111
),
1212
meta: JSON.parse("null"),
1313
toc: JSON.parse("[]"),
@@ -34,14 +34,18 @@ function _createMdxContent(props) {
3434
globalContext: JSON.parse('""'),
3535
defContext: globalThis[_rehypeKatexContext0],
3636
content: "\\Tr \\idmat = 1",
37-
options: true,
37+
meta: JSON.parse(
38+
'{"category":"show","mode":"display","subCategory":"normal"}',
39+
),
3840
}),
3941
"\n",
4042
_jsx(LumaKatex, {
4143
globalContext: JSON.parse('""'),
4244
defContext: globalThis[_rehypeKatexContext0],
4345
content: "1 + 1 = S(O) + S(O) = S(O + S(O)) = S(S(O))",
44-
options: "trans",
46+
meta: JSON.parse(
47+
'{"category":"show","mode":"display","subCategory":"normal"}',
48+
),
4549
}),
4650
"\n",
4751
void (globalThis[_rehypeKatexContext0] = ""),
@@ -50,7 +54,9 @@ function _createMdxContent(props) {
5054
globalContext: JSON.parse('""'),
5155
defContext: globalThis[_rehypeKatexContext0],
5256
content: "\\Tr \\idmat = 1",
53-
options: "{1,2,3} {1-2} lang=js",
57+
meta: JSON.parse(
58+
'{"category":"show","mode":"display","subCategory":"normal"}',
59+
),
5460
}),
5561
(() => {
5662
delete globalThis[_rehypeKatexContext0];
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2+
const _rehypeKatexContext0 = Symbol();
3+
function _createMdxContent(props) {
4+
const _components = {
5+
p: "p",
6+
...props.components,
7+
},
8+
{ LumaKatex, LumaMdxLayout } = _components;
9+
if (!LumaKatex) _missingMdxReference("LumaKatex", true);
10+
if (!LumaMdxLayout) _missingMdxReference("LumaMdxLayout", true);
11+
return _jsxs(LumaMdxLayout, {
12+
file: JSON.parse(
13+
'{"data":{},"history":[],"messages":[],"value":"\\n\\n\\n\\n\\n$ab\\\\}c$\\n"}',
14+
),
15+
meta: JSON.parse("null"),
16+
toc: JSON.parse("[]"),
17+
headers: [],
18+
children: [
19+
void (globalThis[_rehypeKatexContext0] = ""),
20+
"\n",
21+
_jsx(_components.p, {
22+
children: _jsx(LumaKatex, {
23+
globalContext: JSON.parse('""'),
24+
defContext: globalThis[_rehypeKatexContext0],
25+
content: "ab\\}c",
26+
meta: JSON.parse(
27+
'{"category":"show","mode":"inline","subCategory":"normal"}',
28+
),
29+
}),
30+
}),
31+
(() => {
32+
delete globalThis[_rehypeKatexContext0];
33+
})(),
34+
],
35+
});
36+
}
37+
export default function MDXContent(props = {}) {
38+
const { wrapper: MDXLayout } = props.components || {};
39+
return MDXLayout
40+
? _jsx(MDXLayout, {
41+
...props,
42+
children: _jsx(_createMdxContent, {
43+
...props,
44+
}),
45+
})
46+
: _createMdxContent(props);
47+
}
48+
function _missingMdxReference(id, component) {
49+
throw new Error(
50+
"Expected " +
51+
(component ? "component" : "object") +
52+
" `" +
53+
id +
54+
"` to be defined: you likely forgot to import, pass, or provide it.",
55+
);
56+
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function _createMdxContent(props) {
1313
if (!Term) _missingMdxReference("Term", true);
1414
return _jsxs(LumaMdxLayout, {
1515
file: JSON.parse(
16-
'{"data":{},"history":[],"messages":[],"value":"<Save>\\n```math inline\\nA=B\\n```\\n</Save>\\n\\n定義は[$]となる。\\n\\n---\\n\\n<Save $bar>\\n```math inline\\nA=B\\n```\\n</Save>\\n\\n定義は[$bar]となる。\\n\\n---\\n\\n```math!save\\n A=B\\n```\\n\\n定義は[$]となる。\\n\\n---\\n\\n```math!save $foo inline\\n A=B\\n```\\n\\n定義は[$foo]となる。\\n"}',
16+
'{"data":{},"history":[],"messages":[],"value":"<Save>\\n```math inline\\nA=B\\n```\\n</Save>\\n\\n定義は[$]となる。\\n\\n---\\n\\n<Save $bar>\\n```math inline\\nA=B\\n```\\n</Save>\\n\\n定義は[$bar]となる。\\n\\n---\\n\\n```math $\\n A=B\\n```\\n\\n定義は[$]となる。\\n\\n---\\n\\n```math $foo inline\\n A=B\\n```\\n\\n定義は[$foo]となる。\\n"}',
1717
),
1818
meta: JSON.parse("null"),
1919
toc: JSON.parse("[]"),
@@ -37,7 +37,9 @@ function _createMdxContent(props) {
3737
globalContext: JSON.parse('""'),
3838
defContext: globalThis[_rehypeKatexContext0],
3939
content: "A=B",
40-
options: "inline",
40+
meta: JSON.parse(
41+
'{"category":"show","mode":"inline","subCategory":"normal"}',
42+
),
4143
}),
4244
}),
4345
"となる。",
@@ -62,7 +64,9 @@ function _createMdxContent(props) {
6264
globalContext: JSON.parse('""'),
6365
defContext: globalThis[_rehypeKatexContext0],
6466
content: "A=B",
65-
options: "inline",
67+
meta: JSON.parse(
68+
'{"category":"show","mode":"inline","subCategory":"normal"}',
69+
),
6670
}),
6771
}),
6872
"となる。",
@@ -87,7 +91,9 @@ function _createMdxContent(props) {
8791
globalContext: JSON.parse('""'),
8892
defContext: globalThis[_rehypeKatexContext0],
8993
content: "A=B",
90-
options: true,
94+
meta: JSON.parse(
95+
'{"category":"save","saveName":"","saved":{"category":"show","mode":"display","subCategory":"normal"}}',
96+
),
9197
}),
9298
}),
9399
"となる。",
@@ -112,7 +118,9 @@ function _createMdxContent(props) {
112118
globalContext: JSON.parse('""'),
113119
defContext: globalThis[_rehypeKatexContext0],
114120
content: "A=B",
115-
options: "$foo inline",
121+
meta: JSON.parse(
122+
'{"category":"save","saveName":"foo","saved":{"category":"show","mode":"inline","subCategory":"normal"}}',
123+
),
116124
}),
117125
}),
118126
"となる。",

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"./rehype-code-meta": "./dist/rehype-code-meta.js",
1919
"./rehype-replace-text": "./dist/rehype-replace-text.js",
2020
"./rehype-counter": "./dist/rehype-counter.js",
21+
"./katex-ex/parse-meta": "./dist/katex-ex/parse-meta.js",
2122
"./types": "./dist/types.js"
2223
},
2324
"scripts": {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { parseMeta } from "../parse-meta.js";
2+
import { describe, expect, it } from "vitest";
3+
4+
describe("parseMeta", () => {
5+
it("show", () => {
6+
expect(parseMeta("")).toStrictEqual({
7+
category: "show",
8+
mode: "display",
9+
subCategory: "normal",
10+
});
11+
expect(parseMeta("inline")).toStrictEqual({
12+
category: "show",
13+
mode: "inline",
14+
subCategory: "normal",
15+
});
16+
expect(parseMeta("inline-block")).toStrictEqual({
17+
category: "show",
18+
mode: "inline-block",
19+
subCategory: "normal",
20+
});
21+
expect(parseMeta("trans1")).toStrictEqual({
22+
category: "show",
23+
mode: "display",
24+
subCategory: "transform1",
25+
});
26+
expect(parseMeta("display trans2")).toStrictEqual({
27+
category: "show",
28+
mode: "display",
29+
subCategory: "transform2",
30+
});
31+
});
32+
33+
it("def", () => {
34+
expect(parseMeta("def")).toStrictEqual({
35+
category: "def",
36+
});
37+
});
38+
39+
it("def", () => {
40+
expect(parseMeta("$foo")).toStrictEqual({
41+
category: "save",
42+
saveName: "foo",
43+
saved: {
44+
category: "show",
45+
mode: "display",
46+
subCategory: "normal",
47+
},
48+
});
49+
expect(parseMeta("$foo trans1")).toStrictEqual({
50+
category: "save",
51+
saveName: "foo",
52+
saved: {
53+
category: "show",
54+
mode: "display",
55+
subCategory: "transform1",
56+
},
57+
});
58+
expect(parseMeta("inline $")).toStrictEqual({
59+
category: "save",
60+
saveName: "",
61+
saved: {
62+
category: "show",
63+
mode: "inline",
64+
subCategory: "normal",
65+
},
66+
});
67+
});
68+
});

src/katex-ex/parse-meta.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export type KatexShowMode = "inline" | "inline-block" | "display";
2+
3+
export type KatexLumaMetaDef = {
4+
readonly category: "def";
5+
};
6+
export type KatexLumaMetaSave = {
7+
readonly category: "save";
8+
readonly saveName: string;
9+
readonly saved: KatexLumaMetaShow;
10+
};
11+
export type KatexLumaMetaShow = {
12+
readonly category: "show";
13+
readonly mode: KatexShowMode;
14+
readonly subCategory: "normal" | "transform1" | "transform2";
15+
};
16+
export type KatexLumaMeta =
17+
| KatexLumaMetaDef
18+
| KatexLumaMetaSave
19+
| KatexLumaMetaShow;
20+
21+
export const parseMeta = (meta: string): KatexLumaMeta => {
22+
const parts = meta.trim().split(/\s+/);
23+
24+
let mode: KatexShowMode = "display";
25+
let subCategory: "normal" | "transform1" | "transform2" = "normal";
26+
let saveName: string | null = null;
27+
28+
for (const part of parts) {
29+
if (part === "inline" || part === "inline-block" || part === "display") {
30+
mode = part;
31+
} else if (part === "trans1" || part === "transform1") {
32+
subCategory = "transform1";
33+
} else if (part === "trans2" || part === "transform2") {
34+
subCategory = "transform2";
35+
} else if (part === "def") {
36+
return { category: "def" };
37+
} else if (part.startsWith("$")) {
38+
saveName = part.slice(1);
39+
}
40+
}
41+
42+
const showMeta: KatexLumaMetaShow = {
43+
category: "show",
44+
mode,
45+
subCategory,
46+
};
47+
48+
if (saveName !== null) {
49+
return {
50+
category: "save",
51+
saveName,
52+
saved: showMeta,
53+
};
54+
}
55+
56+
return showMeta;
57+
};

0 commit comments

Comments
 (0)