concurrently and frontmatter
This commit is contained in:
1 parent
7831ae4b97
commit
2d18c9b3d6
5 files changed
+312
-13
No files matched your search
@@ -1,6 +1,8 @@
|
||||
// @ts-check
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { readFileSync, mkdirSync, writeFileSync } from "fs";
|
||||
import { join as joinPath } from 'path'
|
||||
import matter from 'gray-matter';
|
||||
|
||||
const PORT = 8080
|
||||
|
||||
@@ -12,10 +14,11 @@ wss.on('connection', (ws) => {
|
||||
console.log('New client connected');
|
||||
|
||||
let filename = ''
|
||||
const ROOT_DIR = 'docs';
|
||||
|
||||
ws.on('message', (message, isBinary) => {
|
||||
if (message instanceof Buffer && isBinary) {
|
||||
writeFileSync(filename, message);
|
||||
writeFileSync(joinPath(ROOT_DIR, filename), message);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -24,37 +27,54 @@ wss.on('connection', (ws) => {
|
||||
|
||||
switch (name) {
|
||||
case 'download_q': {
|
||||
const { q } = payload;
|
||||
const { q, duration } = payload;
|
||||
console.log(`Start ${q}`)
|
||||
|
||||
const q_stem = q.replace(/\.[a-z0-9]+$/, "");
|
||||
|
||||
try {
|
||||
mkdirSync(`docs/${q}`);
|
||||
mkdirSync(joinPath(ROOT_DIR, q_stem));
|
||||
} catch (e) {}
|
||||
|
||||
let outMd = "";
|
||||
let isChanged = false;
|
||||
|
||||
const re = /^(?<min>\d{1,2}):(?<sec>\d{2})\s*$/;
|
||||
const ts = [];
|
||||
|
||||
for (const line of readFileSync(`docs/${q}.md`, "utf-8").trimEnd().split("\n")) {
|
||||
const { data, content } = matter(readFileSync(joinPath(ROOT_DIR, `${q_stem}.md`), 'utf-8'));
|
||||
data.video = '/' + q;
|
||||
|
||||
for (const line of content.split("\n")) {
|
||||
const m = re.exec(line);
|
||||
if (m?.groups) {
|
||||
const { min, sec } = m.groups;
|
||||
const t = `${min}_${sec}`;
|
||||
ts.push(t);
|
||||
outMd += `\n\n\n`;
|
||||
isChanged = true;
|
||||
} else {
|
||||
outMd += `${line}\n`;
|
||||
}
|
||||
}
|
||||
|
||||
writeFileSync(`docs/${q}.md`, outMd);
|
||||
const interval = data.interval;
|
||||
if (typeof interval === 'number') {
|
||||
for (let t = 0; t < duration; t += interval) {
|
||||
ts.push(t);
|
||||
isChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify({ name: "seek_q_ts", q, ts }));
|
||||
if (isChanged){
|
||||
writeFileSync(joinPath(ROOT_DIR, `${q_stem}.md`), matter.stringify(outMd, data));
|
||||
ws.send(JSON.stringify({ name: "seek_q_ts", q, ts }));
|
||||
}
|
||||
} break;
|
||||
case 'timed_q' : {
|
||||
const { q, t } = payload;
|
||||
filename = t ? `docs/${q}/${t}.png` : '';
|
||||
const q_stem = q.replace(/\.[a-z0-9]+$/, "");
|
||||
filename = t ? joinPath(q_stem, `${t}.png`) : '';
|
||||
} break;
|
||||
case 'done' : {
|
||||
const { q } = payload
|
||||
|
||||
Reference in new issue
Block a user