Files
yt2press/docs/.vitepress/theme/plugins/yt-parse.ts
T
2026-01-20 18:21:53 +07:00

41 lines
1.1 KiB
TypeScript

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 = /^(?<min>\d{1,2}):(?<sec>\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",
);
});
},
};
}