aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/nuldoc/nuldoc-src/components/TableOfContents.ts
blob: 1eb79e98bfd5dc3b5e2e2d6f921176db5d88624f (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
import { TocEntry, TocRoot } from "../markdown/document.ts";
import { a, Element, h2, li, nav, ul } from "../dom.ts";

type Props = {
  toc: TocRoot;
};

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

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