summaryrefslogtreecommitdiff
path: root/src/helper/parse-header.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/helper/parse-header.ts')
-rw-r--r--src/helper/parse-header.ts73
1 files changed, 73 insertions, 0 deletions
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
+ };
+}