import { getIndentLineCount, getIndentCount } from "../scrapbox-userscript-std/text.ts"; import { makeSocket, patch } from "../scrapbox-userscript-std/mod.ts"; import { pool } from "../async-lib/mod.ts"; import { lightFormat } from "../date-fns/lightFormat.ts"; import type { Scrapbox } from "../scrapbox-jp%2Ftypes/userscript.ts"; declare const scrapbox: Scrapbox; export const convert = (text: string): { promise: Promise; text: string; } => { const [caption, ...lines] = text.split('\n'); // 最初の行を講義の短縮名として使う const baseName = caption.trim(); // 最初の行と同じindent数の行を、切り出すブロックの先頭とする const baseIndent = getIndentCount(caption); const blocks: {lines: string[]; index: number; }[] = [...divide(lines, baseIndent)]; const promise = (async () => { const socket = await makeSocket(); await Promise.all([...pool( 3, blocks, (block) => patch(scrapbox.Project.name, `${baseName}-${block.index}`, (lines) => [ ...lines.map(line => line.text), "目標", ...block.lines, "", "内容", " ", "", lightFormat(new Date(), "#yyyy-MM-dd HH:mm:ss"), ], { socket }), )]); })(); return { promise: null, text: blocks.map((block) => `[${baseName}-${block.index}]`).join("\n"), }; }; function* divide(lines: string[], baseIndent: number) { for (let i = 0; i < lines.length; i++) { if (getIndentCount(lines[i]) !== baseIndent) continue; const index = lines[i].trim().split(".")[0]; if(isNaN(parseInt(index))) continue; const count = getIndentLineCount(i, lines); yield { lines: lines.slice(i, i + 1 + count), index: parseInt(index) }; i += count; } }