import { pool } from "../async-lib/mod.ts"; import { encodeTitleURI } from "../scrapbox-userscript-std/dom.ts"; import type { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts"; declare const scrapbox: Scrapbox; export interface SerializeOption { level?: number; } export const serialize = async (text: string, options?: SerializeOption): Promise => { const lines = text.split("\n"); const pooled = pool( 5, lines, async (line) => { const title = line.match(/\[([^\[$!"#%&'()\*\+,\-\.\/\{\|\}<>_~][^\[\]]*)\]/)?.[1]; if (!title) return line; const res = await fetch( `/api/pages/${scrapbox.Project.name}/${encodeTitleURI(title)}/text` ); if (!res.ok) return line; const [headLine, ...rest] = (await res.text()).split("\n"); return ["[/icons/hr.icon]", `[*** ${headLine}]`, ...rest].join("\n"); }, ); return (await Promise.all([...pooled])) .map((result, i) => { if (result.success) return result.value; // 失敗したら元の文字列を返す console.warn(result.reason); return lines[i]; }) .join("\n"); };