summaryrefslogtreecommitdiff
path: root/src/helper/parse-header.ts
blob: 92e0e526e72662116eeb2027ec64d0fcd3bc0ac0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
  };
}