Skip to content

format #1748

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

format #1748

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
28 changes: 14 additions & 14 deletions src/block/node/IconNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ import type { IconNode } from "./type.ts";
const iconRegExp = /\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/;

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 }));
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],
});
72 changes: 36 additions & 36 deletions src/block/node/ImageNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,52 @@ import type { NodeCreator } from "./creator.ts";
import type { ImageNode, PlainNode } from "./type.ts";

const srcFirstStrongImageRegExp =
/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i;
/\[https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?(?:\s+https?:\/\/[^\s\]]+)?\]/i;
const linkFirstStrongImageRegExp =
/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?\]/i;
/\[https?:\/\/[^\s\]]+\s+https?:\/\/[^\s\]]+\.(?:png|jpe?g|gif|svg)(?:\?[^\]\s]+)?\]/i;
const srcFirstStrongGyazoImageRegExp =
/\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?(?:\s+https?:\/\/[^\s\]]+)?\]/;
/\[https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?(?:\s+https?:\/\/[^\s\]]+)?\]/;
const linkFirstStrongGyazoImageRegExp =
/\[https?:\/\/[^\s\]]+\s+https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?\]/;
/\[https?:\/\/[^\s\]]+\s+https?:\/\/(?:[0-9a-z-]+\.)?gyazo\.com\/[0-9a-f]{32}(?:\/raw)?\]/;

const isImageUrl = (text: string): boolean =>
/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg)(\?[^\]\s]+)?$/i.test(text) ||
isGyazoImageUrl(text);
/^https?:\/\/[^\s\]]+\.(png|jpe?g|gif|svg)(\?[^\]\s]+)?$/i.test(text) ||
isGyazoImageUrl(text);

const isGyazoImageUrl = (text: string): boolean =>
/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}(\/raw)?$/.test(text);
/^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}(\/raw)?$/.test(text);

const createImageNode: NodeCreator<ImageNode | PlainNode> = (raw, opts) => {
if (opts.context === "table") {
return createPlainNode(raw, opts);
}

const index = raw.search(/\s/);
const first =
index !== -1 ? raw.substring(1, index) : raw.substring(1, raw.length - 1);
const second =
index !== -1 ? raw.substring(index, raw.length - 1).trimLeft() : "";
const [src, link] = isImageUrl(second) ? [second, first] : [first, second];

return [
{
type: "image",
raw,
src: /^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}$/.test(src)
? `${src}/thumb/1000`
: src,
link,
},
];
if (opts.context === "table") {
return createPlainNode(raw, opts);
}

const index = raw.search(/\s/);
const first =
index !== -1 ? raw.substring(1, index) : raw.substring(1, raw.length - 1);
const second =
index !== -1 ? raw.substring(index, raw.length - 1).replace(/^\s+/, '') : "";
const [src, link] = isImageUrl(second) ? [second, first] : [first, second];

return [
{
type: "image",
raw,
src: /^https?:\/\/([0-9a-z-]\.)?gyazo\.com\/[0-9a-f]{32}$/.test(src)
? `${src}/thumb/1000`
: src,
link,
},
];
};

export const ImageNodeParser = createNodeParser(createImageNode, {
parseOnNested: true,
parseOnQuoted: true,
patterns: [
srcFirstStrongImageRegExp,
linkFirstStrongImageRegExp,
srcFirstStrongGyazoImageRegExp,
linkFirstStrongGyazoImageRegExp,
],
parseOnNested: true,
parseOnQuoted: true,
patterns: [
srcFirstStrongImageRegExp,
linkFirstStrongImageRegExp,
srcFirstStrongGyazoImageRegExp,
linkFirstStrongGyazoImageRegExp,
],
});
36 changes: 18 additions & 18 deletions src/block/node/StrongIconNode.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { createNodeParser, type NodeCreator } from "./creator.ts";
import { createPlainNode } from "./PlainNode.ts";
import { type NodeCreator, createNodeParser } from "./creator.ts";
import type { PlainNode, StrongIconNode } from "./type.ts";

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

const createStrongIconNode: NodeCreator<StrongIconNode | PlainNode> = (
raw,
opts
raw,
opts,
) => {
if (opts.context === "table") return createPlainNode(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 }));
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