news/utils/build.js

94 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-02-12 10:30:05 +01:00
import { join } from "https://deno.land/std@0.208.0/path/mod.ts";
import { emptyDir } from "https://deno.land/std@0.196.0/fs/empty_dir.ts";
import { setWeek, nextMonday, format, addDays } from 'npm:date-fns';
import { marked } from 'npm:marked';
import matter from 'npm:front-matter';
2024-02-12 11:00:15 +01:00
const SRC_DIR = "./data";
2024-02-12 10:30:05 +01:00
const DEST_DIR = "./dist";
async function build() {
2024-02-12 12:57:08 +01:00
let issues = [];
2024-02-12 10:30:05 +01:00
for await (const dirEntry of Deno.readDir(SRC_DIR)) {
if (!dirEntry.isDirectory || !dirEntry.name.match(/^\d{4}$/)) {
continue;
}
//const [fn, ext] = dirEntry.name.split(".");
const year = dirEntry.name;
const yearDir = join(SRC_DIR, year);
console.log(`Processing year: ${year}`);
for await (const dirEntry of Deno.readDir(yearDir)) {
const [fn, ext] = dirEntry.name.split(".");
const weekMatch = fn.match(/^week(\d{2})$/)
if (!weekMatch || ext !== "md") {
continue;
}
const week = weekMatch[1]
const mdPath = join(SRC_DIR, year, dirEntry.name);
const source = await Deno.readTextFile(mdPath);
const issue = {
week: `${year}-${week}`,
period: calcPeriod(year, week),
}
await renderData(issue, source)
issues.push(issue)
}
}
await emptyDir(DEST_DIR);
2024-02-12 14:03:51 +01:00
const imgDir = join(DEST_DIR, "img")
await emptyDir(imgDir)
// get images
for (const issue of issues) {
const imgResp = await fetch("https://html2svg.gwei.cz", {
method: 'POST',
body: JSON.stringify({
2024-02-12 14:09:02 +01:00
url: `https://news.web3privacy.info/image/${issue.week}`,
2024-02-12 14:03:51 +01:00
format: "png",
width: 1920,
height: 960,
})
});
const imgFn = join(imgDir, `${issue.week}.png`)
if (imgResp.body) {
const file = await Deno.open(imgFn, { write: true, create: true });
await imgResp.body.pipeTo(file.writable);
}
}
2024-02-12 10:30:05 +01:00
const outputFn = join(DEST_DIR, "index.json");
await writeJSONFile(outputFn, issues);
}
async function renderData(issue, source) {
const fm = matter(source);
const parsed = marked.parse(fm.body);
return Object.assign(issue, fm.attributes, { newsMd: fm.body, newsHtml: parsed });
//const fm = matter(issue.source)
//console.log(fm)
//console.log(parsed)
//return
}
function calcPeriod(year, week) {
const weekStart = setWeek(nextMonday(new Date(Number(year), 0, 4)), Number(week), {
weekStartsOn: 1,
firstWeekContainsDate: 4,
});
const weekEnd = addDays(weekStart, 6);
return [ weekStart, weekEnd ];
}
async function writeJSONFile(fn, data) {
console.log(`File written: ${fn}`);
return Deno.writeTextFile(fn, JSON.stringify(data, null, 2));
}
build();