summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/ndoc/document.ts
blob: c99fb7f08fc54373f306058c24d5e781b1964e91 (plain)
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
import { join } from "std/path/mod.ts";
import { Config } from "../config.ts";
import { DocBookError } from "../errors.ts";
import { Revision, stringToDate } from "../revision.ts";
import { Element, findFirstChildElement } from "../dom.ts";

export type Document = {
  root: Element;
  sourceFilePath: string;
  uuid: string;
  link: string;
  title: string;
  description: string; // TODO: should it be markup text?
  tags: string[];
  revisions: Revision[];
};

export function createNewDocumentFromRootElement(
  root: Element,
  meta: {
    article: {
      uuid: string;
      title: string;
      description: string;
      tags: string[];
      revisions: {
        date: string;
        remark: string;
        isInternal?: boolean;
      }[];
    };
  },
  sourceFilePath: string,
  config: Config,
): Document {
  const article = findFirstChildElement(root, "article");
  if (!article) {
    throw new DocBookError(
      `[docbook.new] <article> element not found`,
    );
  }

  const cwd = Deno.cwd();
  const contentDir = join(cwd, config.locations.contentDir);
  const link = sourceFilePath.replace(contentDir, "").replace(".xml", "/");
  return {
    root: root,
    sourceFilePath: sourceFilePath,
    uuid: meta.article.uuid,
    link: link,
    title: meta.article.title,
    description: meta.article.description,
    tags: meta.article.tags,
    revisions: meta.article.revisions.map((r, i) => ({
      number: i,
      date: stringToDate(r.date),
      remark: r.remark,
      isInternal: !!r.isInternal,
    })),
  };
}