blog/lib/tasks/articles.ts

147 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-06-27 04:22:58 +00:00
import { basename, dirname, join } from "node:path";
2024-06-26 02:21:31 +00:00
import asciidoctor from "asciidoctor";
import { DateTime } from "luxon";
import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypePresetMinify from "rehype-preset-minify";
2024-06-27 04:22:58 +00:00
import rehypePrism from "rehype-prism";
import { DEFAULT_DATE, PRODUCTION } from "../environment.ts";
import rehypeExternalLinks from "rehype-external-links";
2024-06-27 04:22:58 +00:00
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";
2024-06-27 04:22:58 +00:00
import { SITE_DESCRIPTION, SITE_TITLE } from "../constants.ts";
import { JSX } from "preact/jsx-runtime";
2024-06-27 04:22:58 +00:00
import { reloadAssets } from "../assets.ts";
2024-06-27 02:10:50 +00:00
import { lastValueFrom, mergeMap, Observable, Subscriber } from "rxjs";
2024-06-27 04:22:58 +00:00
import { dest, fromGlob, onComplete } from "../rx-utils.ts";
2024-06-27 08:06:39 +00:00
import { VFile } from "vfile";
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const DOCTYPE = encoder.encode("<!doctype html>");
2024-06-27 04:22:58 +00:00
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>`,
);
});
});
2024-06-27 08:06:39 +00:00
function extractImages(sink: Subscriber<VFile>, articlePath: string) {
return function () {
2024-06-27 04:22:58 +00:00
return function (tree: any) {
visit(tree, "element", function (node: any) {
if (node.tagName !== "img") {
return;
}
2024-06-26 02:21:31 +00:00
const imagePath = join(dirname(articlePath));
2024-06-27 08:06:39 +00:00
const image = new VFile({
2024-06-26 02:21:31 +00:00
path: join(basename(articlePath, ".asciidoc"), node.properties.src),
2024-06-27 08:06:39 +00:00
value: Deno.readFileSync(imagePath),
});
2024-06-27 08:06:39 +00:00
sink.next(image);
});
};
};
}
2024-06-27 08:06:39 +00:00
function renderDocument(root: JSX.Element): Uint8Array {
const result = encoder.encode(renderToStaticMarkup(root));
const dest = new Uint8Array(DOCTYPE.length + result.length);
dest.set(DOCTYPE);
dest.set(result, DOCTYPE.length);
return dest;
}
2024-06-27 08:06:39 +00:00
const transformArticle = (sink: Subscriber<VFile>, articles: Article[]) => async (file: VFile) => {
2024-06-27 04:22:58 +00:00
const slug = basename(file.path, ".asciidoc");
2024-06-27 08:06:39 +00:00
const document = Asciidoctor.load(decoder.decode(file.value as Uint8Array), {
2024-06-27 04:22:58 +00:00
extension_registry: EXTENSION_REGISTRY,
});
const date = DateTime.fromISO(document.getAttribute("docdate", { zone: "UTC" }));
const article = { path: file.path, slug, date, document };
2024-06-26 02:21:31 +00:00
2024-06-27 04:22:58 +00:00
if (PRODUCTION && date.equals(DEFAULT_DATE)) {
return;
}
2024-06-27 04:22:58 +00:00
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() }),
});
2024-06-27 04:22:58 +00:00
file.path = join(slug, "index.html");
2024-06-27 08:06:39 +00:00
file.value = renderDocument(content);
2024-06-27 04:22:58 +00:00
articles.push(article);
2024-06-27 08:06:39 +00:00
sink.next(file);
2024-06-27 04:22:58 +00:00
};
2024-06-26 02:21:31 +00:00
2024-06-27 08:06:39 +00:00
const finalizeArticles = (articles: Article[]) => (sink: Subscriber<VFile>) => {
2024-06-26 02:21:31 +00:00
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`,
},
2024-06-26 03:30:43 +00:00
Content: () => renderIndex({ articles }),
});
2024-06-27 08:06:39 +00:00
sink.next(new VFile({ path: "index.html", value: renderDocument(contents) }));
2024-06-27 04:45:39 +00:00
return Promise.resolve();
2024-06-27 01:28:17 +00:00
};
2024-06-27 04:45:39 +00:00
export const articles = async () => {
2024-06-27 01:28:17 +00:00
reloadAssets();
2024-06-27 04:22:58 +00:00
const articles: Article[] = [];
2024-06-27 01:28:17 +00:00
2024-06-27 04:45:39 +00:00
await lastValueFrom(
2024-06-27 02:10:50 +00:00
fromGlob("articles/**/*.asciidoc").pipe(
2024-06-27 01:28:17 +00:00
mergeMap(
(file) =>
2024-06-27 08:06:39 +00:00
new Observable<VFile>((subscriber) => {
2024-06-27 01:28:17 +00:00
transformArticle(
2024-06-27 08:06:39 +00:00
subscriber,
2024-06-27 01:28:17 +00:00
articles,
)(file).then(() => subscriber.complete());
}),
),
onComplete(finalizeArticles(articles)),
2024-06-27 02:10:50 +00:00
mergeMap(dest("dist")),
),
2024-06-27 04:45:39 +00:00
);
2024-06-27 01:28:17 +00:00
};