Skip to content

Commit aaa561f

Browse files
committed
untie generateIconNodeCreator function
1 parent 45b282b commit aaa561f

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

src/block/node/IconNode.ts

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
1-
import { createPlainNode } from "./PlainNode.ts";
21
import { createNodeParser } from "./creator.ts";
3-
42
import type { NodeCreator } from "./creator.ts";
5-
import type { IconNode, PlainNode, StrongIconNode } from "./type.ts";
3+
import type { IconNode } from "./type.ts";
64

75
const iconRegExp = /\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/;
86

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+
};
3920

4021
export const IconNodeParser = createNodeParser(createIconNode, {
41-
parseOnNested: true,
42-
parseOnQuoted: true,
43-
patterns: [iconRegExp],
22+
parseOnNested: true,
23+
parseOnQuoted: true,
24+
patterns: [iconRegExp],
4425
});

src/block/node/StrongIconNode.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
import { generateIconNodeCreator } from "./IconNode.ts";
2-
import { createNodeParser } from "./creator.ts";
1+
import { createNodeParser, type NodeCreator } from "./creator.ts";
2+
import { createPlainNode } from "./PlainNode.ts";
3+
import type { PlainNode, StrongIconNode } from "./type.ts";
34

45
const strongIconRegExp = /\[\[[^[\]]*\.icon(?:\*\d+)?\]\]/;
56

6-
const createStrongIconNode = generateIconNodeCreator("strongIcon");
7+
const createStrongIconNode: NodeCreator<StrongIconNode | PlainNode> = (
8+
raw,
9+
opts
10+
) => {
11+
if (opts.context === "table") return createPlainNode(raw, opts);
12+
13+
const target = raw.substring(2, raw.length - 2);
14+
const index = target.lastIndexOf(".icon");
15+
const path = target.substring(0, index);
16+
const pathType = path.startsWith("/") ? "root" : "relative";
17+
const numStr = target.substring(index + 5, target.length);
18+
const num = numStr.startsWith("*")
19+
? Number.parseInt(numStr.substring(1), 10)
20+
: 1;
21+
return new Array(num)
22+
.fill({})
23+
.map(() => ({ path, pathType, type: "strongIcon", raw }));
24+
};
725

826
export const StrongIconNodeParser = createNodeParser(createStrongIconNode, {
9-
parseOnNested: false,
10-
parseOnQuoted: true,
11-
patterns: [strongIconRegExp],
27+
parseOnNested: false,
28+
parseOnQuoted: true,
29+
patterns: [strongIconRegExp],
1230
});

0 commit comments

Comments
 (0)