blog/lib/tasks/articles.ts

138 lines
4.9 KiB
TypeScript

import { readFileSync } from "node:fs";
import { basename, dirname, join } from "node:path";
import asciidoctor from "asciidoctor";
import File from "vinyl";
import { DateTime } from "luxon";
import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypePresetMinify from "rehype-preset-minify";
import rehypePrism from "rehype-prism";
import { DEFAULT_DATE, PRODUCTION } from "../environment.ts";
import rehypeExternalLinks from "rehype-external-links";
import { visit } from "unist-util-visit";
import renderLayout from "../views/layout.tsx";
import renderArticleLayout, { Article } from "../views/article.tsx";
import renderIndex from "../views/index.tsx";
import { renderToStaticMarkup } from "preact-render-to-string";
import { SITE_DESCRIPTION, SITE_TITLE } from "../constants.ts";
import { JSX } from "preact/jsx-runtime";
import { reloadAssets } from "../assets.ts";
import { lastValueFrom, mergeMap, Observable, Subscriber } from "rxjs";
import { dest, fromGlob, onComplete } from "../rx-utils.ts";
import { Buffer } from "node:buffer";
const Asciidoctor = asciidoctor();
const EXTENSION_REGISTRY = Asciidoctor.Extensions.create();
EXTENSION_REGISTRY.inlineMacro("abbr", function () {
this.process(function (parent, target, attributes) {
return this.createInline(
parent,
"quoted",
`<abbr title="${attributes["$positional"]}">${target}</abbr>`,
);
});
});
function extractImages(sink: (image: File) => void, articlePath: string) {
return function () {
return function (tree: any) {
visit(tree, "element", function (node: any) {
if (node.tagName !== "img") {
return;
}
const imagePath = join(dirname(articlePath));
const image = new File({
path: join(basename(articlePath, ".asciidoc"), node.properties.src),
contents: readFileSync(imagePath),
});
sink(image);
});
};
};
}
function renderDocument(root: JSX.Element): Buffer {
return Buffer.concat([Buffer.from("<!doctype html>"), Buffer.from(renderToStaticMarkup(root))]);
}
const transformArticle = (sink: (file: File) => void, articles: Article[]) => async (file: File) => {
const slug = basename(file.path, ".asciidoc");
const document = Asciidoctor.load(file.contents!.toString(), {
extension_registry: EXTENSION_REGISTRY,
});
const date = DateTime.fromISO(document.getAttribute("docdate", { zone: "UTC" }));
const article = { path: file.path, slug, date, document };
if (PRODUCTION && date.equals(DEFAULT_DATE)) {
return;
}
const vfile = await unified()
.use(rehypeParse as any)
.use(rehypeExternalLinks, { rel: "noopener", target: "_blank" })
.use(extractImages(sink, file.path))
.use(rehypePrism)
.use(rehypePresetMinify as any)
.use(rehypeStringify as any)
.process(document.convert());
const content = renderLayout({
title: document.getDoctitle({}) as string,
meta: {
description: document.getAttribute("description"),
keywords: document.getAttribute("keywords"),
"og:title": document.getDoctitle({}) as string,
"og:type": "article",
"og:article:published_time": document.getAttribute("docdate"),
"og:url": `https://adaedra.eu/${slug}/`,
"og:description": document.getAttribute("description"),
"og:site_name": SITE_TITLE,
},
Content: () => renderArticleLayout({ article, body: vfile.toString() }),
});
file.path = join(slug, "index.html");
file.contents = renderDocument(content);
articles.push(article);
sink(file);
};
const finalizeArticles = (articles: Article[]) => async (sink: Subscriber<File>) => {
articles.sort(({ date: a }, { date: b }) => b.diff(a).toMillis());
const contents = renderLayout({
meta: {
description: SITE_DESCRIPTION,
"og:title": SITE_TITLE,
"og:type": "website",
"og:url": `https://adaedra.eu`,
},
Content: () => renderIndex({ articles }),
});
sink.next(new File({ path: "index.html", contents: renderDocument(contents) }));
};
export const articles = () => {
reloadAssets();
const articles: Article[] = [];
return lastValueFrom(
fromGlob("articles/**/*.asciidoc").pipe(
mergeMap(
(file) =>
new Observable<File>((subscriber) => {
transformArticle(
(f) => subscriber.next(f),
articles,
)(file).then(() => subscriber.complete());
}),
),
onComplete(finalizeArticles(articles)),
mergeMap(dest("dist")),
),
).then(() => {});
};