diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-04-09 20:27:54 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-04-09 22:24:37 +0900 |
| commit | dbc3e0dfd893435f31cca39873f7ba9bf13b93a6 (patch) | |
| tree | 2cee4227ec6a995a9ddc99894dce604523833dc7 /vhosts/blog/nuldoc-src/djot/parse.ts | |
| parent | bba1212ab46ed85c2ed3b646f2362bdbb1f45b63 (diff) | |
| download | nsfisis.dev-dbc3e0dfd893435f31cca39873f7ba9bf13b93a6.tar.gz nsfisis.dev-dbc3e0dfd893435f31cca39873f7ba9bf13b93a6.tar.zst nsfisis.dev-dbc3e0dfd893435f31cca39873f7ba9bf13b93a6.zip | |
feat(blog/nuldoc): change format of nuldoc builder from .ndoc to .dj
Diffstat (limited to 'vhosts/blog/nuldoc-src/djot/parse.ts')
| -rw-r--r-- | vhosts/blog/nuldoc-src/djot/parse.ts | 33 |
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)); +} |
