Skip to content

untie generateIconNodeCreator function #1747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions src/block/node/IconNode.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
import { createPlainNode } from "./PlainNode.ts";
import { createNodeParser } from "./creator.ts";

import type { NodeCreator } from "./creator.ts";
import type { IconNode, PlainNode, StrongIconNode } from "./type.ts";
import type { IconNode } from "./type.ts";

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

export function generateIconNodeCreator(
type: IconNode["type"],
): NodeCreator<IconNode>;
export function generateIconNodeCreator(
type: StrongIconNode["type"],
): NodeCreator<StrongIconNode | PlainNode>;
export function generateIconNodeCreator(
type: (IconNode | StrongIconNode)["type"],
): NodeCreator<IconNode | StrongIconNode | PlainNode> {
return (raw, opts) => {
if (type === "strongIcon" && opts.context === "table") {
return createPlainNode(raw, opts);
}

const target =
type === "icon"
? raw.substring(1, raw.length - 1)
: raw.substring(2, raw.length - 2);
const index = target.lastIndexOf(".icon");
const path = target.substring(0, index);
const pathType = path.startsWith("/") ? "root" : "relative";
const numStr = target.substring(index + 5, target.length);
const num = numStr.startsWith("*")
? Number.parseInt(numStr.substring(1), 10)
: 1;
return new Array(num).fill({}).map(() => ({ path, pathType, type, raw }));
};
}

const createIconNode = generateIconNodeCreator("icon");
const createIconNode: NodeCreator<IconNode> = (raw) => {
const target = raw.substring(1, raw.length - 1);
const index = target.lastIndexOf(".icon");
const path = target.substring(0, index);
const pathType = path.startsWith("/") ? "root" : "relative";
const numStr = target.substring(index + 5, target.length);
const num = numStr.startsWith("*")
? Number.parseInt(numStr.substring(1), 10)
: 1;
return new Array(num)
.fill({})
.map(() => ({ path, pathType, type: "icon", raw }));
};

export const IconNodeParser = createNodeParser(createIconNode, {
parseOnNested: true,
parseOnQuoted: true,
patterns: [iconRegExp],
parseOnNested: true,
parseOnQuoted: true,
patterns: [iconRegExp],
});
30 changes: 24 additions & 6 deletions src/block/node/StrongIconNode.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import { generateIconNodeCreator } from "./IconNode.ts";
import { createNodeParser } from "./creator.ts";
import { createNodeParser, type NodeCreator } from "./creator.ts";
import { createPlainNode } from "./PlainNode.ts";
import type { PlainNode, StrongIconNode } from "./type.ts";

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

const createStrongIconNode = generateIconNodeCreator("strongIcon");
const createStrongIconNode: NodeCreator<StrongIconNode | PlainNode> = (
raw,
opts
) => {
if (opts.context === "table") return createPlainNode(raw, opts);

const target = raw.substring(2, raw.length - 2);
const index = target.lastIndexOf(".icon");
const path = target.substring(0, index);
const pathType = path.startsWith("/") ? "root" : "relative";
const numStr = target.substring(index + 5, target.length);
const num = numStr.startsWith("*")
? Number.parseInt(numStr.substring(1), 10)
: 1;
return new Array(num)
.fill({})
.map(() => ({ path, pathType, type: "strongIcon", raw }));
};

export const StrongIconNodeParser = createNodeParser(createStrongIconNode, {
parseOnNested: false,
parseOnQuoted: true,
patterns: [strongIconRegExp],
parseOnNested: false,
parseOnQuoted: true,
patterns: [strongIconRegExp],
});
Loading