import { type Plugin, type ViteDevServer } from "vite"; import { readFileSync, mkdirSync, writeFileSync } from "fs"; export default function ytParsePlugin(): Plugin { return { name: "clipboard-server-spawn", configureServer(server: ViteDevServer) { server.ws.on("download-q", ({ q }) => { try { mkdirSync(`docs/${q}`); } catch (e) {} let outMd = ""; const re = /^(?\d{1,2}):(?\d{2})\s*$/; for (const line of readFileSync(`docs/${q}.md`, "utf-8").trimEnd().split("\n")) { const m = re.exec(line); if (m) { const { min, sec } = m.groups!; const t = parseInt(min) * 60 + parseInt(sec); server.ws.send("seek-q", { t }); outMd += `[${line}](./${q}/${t}.png)`; } else { outMd += line + "\n"; } } writeFileSync(`docs/${q}.md`, outMd); }); server.ws.on("timed-q", ({ q, t, data }) => { writeFileSync( `docs/${q}/${t}.png`, data.replace(/^data:image\/png;base64,/, ""), "base64", ); }); }, }; }