From 8c34d810af95fae0ef846f54370a8c88bfab7123 Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sat, 11 Apr 2026 14:24:49 -0700 Subject: initial commit --- src/helper/parse-header.ts | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/helper/parse-header.ts (limited to 'src/helper/parse-header.ts') diff --git a/src/helper/parse-header.ts b/src/helper/parse-header.ts new file mode 100644 index 0000000..92e0e52 --- /dev/null +++ b/src/helper/parse-header.ts @@ -0,0 +1,73 @@ + + + +/*** EXPORT ------------------------------------------- ***/ + +export interface DocumentMeta { + abstract: string; + category: string; + date: string; + document: string; + title: string; +} + +export default (input: string): DocumentMeta => { + /*** NOTE + This is extremely fragile, the source document must be formatted + specifically. This is my blog so who cares? ***/ + + const lines = input.split("\n"); + + /*** extract document ID ***/ + const documentMatch = lines[1]?.match(/^Document:\s+(\S+)/); + const document = documentMatch?.[1] ?? ""; + + /*** extract category ***/ + const categoryMatch = lines[2]?.match(/^Category:\s+(\S+)/); + const category = categoryMatch?.[1] ?? ""; + + /*** extract date ***/ + const dateMatch = lines[2]?.match(/\d{4}.\d{2}.\d{2}/); + const date = dateMatch?.[0] ?? ""; + + /*** find title (first non-empty line after the header block) ***/ + let title = ""; + let titleIndex = -1; + + for (let i = 3; i < lines.length; i++) { + const trimmed = lines[i].trim(); + + if (trimmed && trimmed !== "Abstract") { + title = trimmed; + titleIndex = i; + break; + } + } + + /*** extract abstract (content between "Abstract" and "Body") ***/ + let abstract = ""; + let inAbstract = false; + + for (let i = titleIndex + 1; i < lines.length; i++) { + const trimmed = lines[i].trim(); + + if (trimmed === "Abstract") { + inAbstract = true; + continue; + } + + if (trimmed === "Body") + break; + + if (inAbstract && trimmed) + abstract += (abstract ? " " : "") + trimmed; + } + + return { + abstract, + category, + date, + document, + title + }; +} -- cgit v1.2.3