aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/nuldoc-src/components/TableOfContents.ts
blob: 37796ffa068cb642b157a83958a663b469682b23 (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
import { TocEntry, TocRoot } from "../markdown/document.ts";
import { elem, Element } from "../dom.ts";

type Props = {
  toc: TocRoot;
};

export default function TableOfContents({ toc }: Props): Element {
  return elem(
    "nav",
    { class: "toc" },
    elem("h2", {}, "目次"),
    elem(
      "ul",
      {},
      ...toc.entries.map((entry) => TocEntryComponent({ entry })),
    ),
  );
}

function TocEntryComponent({ entry }: { entry: TocEntry }): Element {
  return elem(
    "li",
    {},
    elem("a", { href: `#${entry.id}` }, entry.text),
    entry.children.length > 0
      ? elem(
        "ul",
        {},
        ...entry.children.map((child) => TocEntryComponent({ entry: child })),
      )
      : null,
  );
}