summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/djot/parse.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-04-09 22:24:50 +0900
committernsfisis <nsfisis@gmail.com>2025-04-09 22:24:50 +0900
commitd1b4bc44170196a4dcef5254d092fc387e73792e (patch)
treea6bbe0e61b73c99a815d730ae7956a5124200066 /vhosts/blog/nuldoc-src/djot/parse.ts
parentbba1212ab46ed85c2ed3b646f2362bdbb1f45b63 (diff)
parent4f46d262e6967c9c638b40f3b0246d21b7a9b9dc (diff)
downloadnsfisis.dev-d1b4bc44170196a4dcef5254d092fc387e73792e.tar.gz
nsfisis.dev-d1b4bc44170196a4dcef5254d092fc387e73792e.tar.zst
nsfisis.dev-d1b4bc44170196a4dcef5254d092fc387e73792e.zip
Merge branch 'nuldoc-djot'
Diffstat (limited to 'vhosts/blog/nuldoc-src/djot/parse.ts')
-rw-r--r--vhosts/blog/nuldoc-src/djot/parse.ts33
1 files changed, 33 insertions, 0 deletions
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<Document> {
+ 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));
+}