Files
2026-06-25 16:34:27 +07:00

53 lines
1.4 KiB
TypeScript

import { type Plugin, type ViteDevServer } from "vite";
import { readFileSync, mkdirSync, writeFileSync } from "fs";
import sharp from "sharp";
export default function ytParsePlugin(): Plugin {
return {
name: "clipboard-server-spawn",
configureServer(server: ViteDevServer) {
server.ws.on("download-q", ({ q }) => {
try {
mkdirSync(`docs/${q}`);
mkdirSync(`docs/public/images/${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}](/images/${q}/${t}.webp)`;
} else {
outMd += line + "\n";
}
}
writeFileSync(`docs/${q}.md`, outMd);
});
server.ws.on("timed-q", ({ q, t, data }) => {
sharp(
Buffer.from(data.replace(/^data:image\/png;base64,/, ""), "base64"),
)
.rotate()
.resize({
width: 800,
height: 600,
fit: "inside",
withoutEnlargement: true,
})
.webp({ quality: 75 })
.toFile(`docs/public/images/${q}/${t}.png`);
});
},
};
}