diff --git a/src/block/node/IconNode.ts b/src/block/node/IconNode.ts index 865c281f..a4073e8c 100644 --- a/src/block/node/IconNode.ts +++ b/src/block/node/IconNode.ts @@ -5,21 +5,21 @@ import type { IconNode } from "./type.ts"; const iconRegExp = /\[[^[\]]*\.icon(?:\*[1-9]\d*)?\]/; const createIconNode: NodeCreator = (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], }); diff --git a/src/block/node/ImageNode.ts b/src/block/node/ImageNode.ts index 14d50058..daebb765 100644 --- a/src/block/node/ImageNode.ts +++ b/src/block/node/ImageNode.ts @@ -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 = (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, + ], }); diff --git a/src/block/node/StrongIconNode.ts b/src/block/node/StrongIconNode.ts index cedec5de..e2ccba87 100644 --- a/src/block/node/StrongIconNode.ts +++ b/src/block/node/StrongIconNode.ts @@ -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 = ( - 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], });