|
1 |
| -import { createPlainNode } from "./PlainNode.ts"; |
2 | 1 | import { createNodeParser } from "./creator.ts";
|
3 |
| - |
4 | 2 | import type { NodeCreator } from "./creator.ts";
|
5 |
| -import type { IconNode, PlainNode, StrongIconNode } from "./type.ts"; |
| 3 | +import type { IconNode } from "./type.ts"; |
6 | 4 |
|
7 | 5 | const iconRegExp = /\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/;
|
8 | 6 |
|
9 |
| -export function generateIconNodeCreator( |
10 |
| - type: IconNode["type"], |
11 |
| -): NodeCreator<IconNode>; |
12 |
| -export function generateIconNodeCreator( |
13 |
| - type: StrongIconNode["type"], |
14 |
| -): NodeCreator<StrongIconNode | PlainNode>; |
15 |
| -export function generateIconNodeCreator( |
16 |
| - type: (IconNode | StrongIconNode)["type"], |
17 |
| -): NodeCreator<IconNode | StrongIconNode | PlainNode> { |
18 |
| - return (raw, opts) => { |
19 |
| - if (type === "strongIcon" && opts.context === "table") { |
20 |
| - return createPlainNode(raw, opts); |
21 |
| - } |
22 |
| - |
23 |
| - const target = |
24 |
| - type === "icon" |
25 |
| - ? raw.substring(1, raw.length - 1) |
26 |
| - : raw.substring(2, raw.length - 2); |
27 |
| - const index = target.lastIndexOf(".icon"); |
28 |
| - const path = target.substring(0, index); |
29 |
| - const pathType = path.startsWith("/") ? "root" : "relative"; |
30 |
| - const numStr = target.substring(index + 5, target.length); |
31 |
| - const num = numStr.startsWith("*") |
32 |
| - ? Number.parseInt(numStr.substring(1), 10) |
33 |
| - : 1; |
34 |
| - return new Array(num).fill({}).map(() => ({ path, pathType, type, raw })); |
35 |
| - }; |
36 |
| -} |
37 |
| - |
38 |
| -const createIconNode = generateIconNodeCreator("icon"); |
| 7 | +const createIconNode: NodeCreator<IconNode> = (raw) => { |
| 8 | + const target = raw.substring(1, raw.length - 1); |
| 9 | + const index = target.lastIndexOf(".icon"); |
| 10 | + const path = target.substring(0, index); |
| 11 | + const pathType = path.startsWith("/") ? "root" : "relative"; |
| 12 | + const numStr = target.substring(index + 5, target.length); |
| 13 | + const num = numStr.startsWith("*") |
| 14 | + ? Number.parseInt(numStr.substring(1), 10) |
| 15 | + : 1; |
| 16 | + return new Array(num) |
| 17 | + .fill({}) |
| 18 | + .map(() => ({ path, pathType, type: "icon", raw })); |
| 19 | +}; |
39 | 20 |
|
40 | 21 | export const IconNodeParser = createNodeParser(createIconNode, {
|
41 |
| - parseOnNested: true, |
42 |
| - parseOnQuoted: true, |
43 |
| - patterns: [iconRegExp], |
| 22 | + parseOnNested: true, |
| 23 | + parseOnQuoted: true, |
| 24 | + patterns: [iconRegExp], |
44 | 25 | });
|
0 commit comments