From dbc3e0dfd893435f31cca39873f7ba9bf13b93a6 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 9 Apr 2025 20:27:54 +0900 Subject: feat(blog/nuldoc): change format of nuldoc builder from .ndoc to .dj --- vhosts/blog/nuldoc-src/djot/parse.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 vhosts/blog/nuldoc-src/djot/parse.ts (limited to 'vhosts/blog/nuldoc-src/djot/parse.ts') diff --git a/vhosts/blog/nuldoc-src/djot/parse.ts b/vhosts/blog/nuldoc-src/djot/parse.ts new file mode 100644 index 00000000..884ab154 --- /dev/null +++ b/vhosts/blog/nuldoc-src/djot/parse.ts @@ -0,0 +1,33 @@ +import { parse as parseDjot } from "@djot/djot"; +import { parse as parseToml } from "@std/toml"; +import { Config } from "../config.ts"; +import { + createNewDocumentFromDjotDocument, + Document, + PostMetadata, + PostMetadataSchema, +} from "./document.ts"; +import toHtml from "./to_html.ts"; + +export async function parseDjotFile( + filePath: string, + config: Config, +): Promise { + try { + const fileContent = await Deno.readTextFile(filePath); + const parts = fileContent.split(/^---$/m); + const meta = parseMetadata(parts[1]); + const root = parseDjot(parts[2]); + const doc = createNewDocumentFromDjotDocument(root, meta, filePath, config); + return await toHtml(doc); + } catch (e) { + if (e instanceof Error) { + e.message = `${e.message} in ${filePath}`; + } + throw e; + } +} + +function parseMetadata(s: string): PostMetadata { + return PostMetadataSchema.parse(parseToml(s)); +} -- cgit v1.2.3-70-g09d2