summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/ndoc/document.ts
blob: dfb6d03b0d4bc55cf25323f5285237c9f82327b9 (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
62
63
64
65
66
import { join } from "@std/path";
import { Config } from "../config.ts";
import { NuldocError } from "../errors.ts";
import { Revision, stringToDate } from "../revision.ts";
import { Element, findFirstChildElement } from "../dom.ts";
import { z } from "zod/mod.ts";

export const PostMetadataSchema = z.object({
  article: z.object({
    uuid: z.string(),
    title: z.string(),
    description: z.string(),
    tags: z.array(z.string()),
    revisions: z.array(z.object({
      date: z.string(),
      remark: z.string(),
      isInternal: z.boolean().optional(),
    })),
  }),
});

export type PostMetadata = z.infer<typeof PostMetadataSchema>;

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: PostMetadata,
  sourceFilePath: string,
  config: Config,
): Document {
  const article = findFirstChildElement(root, "article");
  if (!article) {
    throw new NuldocError(
      `[nuldoc.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,
    })),
  };
}