Browse Source

Compute depth of ToC entries when adding them

master
buttercat1791 10 months ago
parent
commit
640a126330
  1. 16
      src/lib/components/publications/table_of_contents.svelte.ts

16
src/lib/components/publications/table_of_contents.svelte.ts

@ -1,14 +1,14 @@
import { PublicationTree } from "../../data_structures/publication_tree.ts"; import { PublicationTree } from "../../data_structures/publication_tree.ts";
export interface TocEntry { export interface TocEntry {
address: string;
title: string; title: string;
href: string; href: string;
depth: number;
expanded: boolean; expanded: boolean;
children: Array<TocEntry> | null; children: Array<TocEntry> | null;
} }
// TODO: Include depth in the `TocEntry` interface, and compute it when adding entries to the ToC.
export class TableOfContents { export class TableOfContents {
#tocRoot: TocEntry | null = null; #tocRoot: TocEntry | null = null;
#addresses = $state<Map<string, TocEntry>>(new Map()); #addresses = $state<Map<string, TocEntry>>(new Map());
@ -24,14 +24,6 @@ export class TableOfContents {
* made available to the entire component tree under that page. * made available to the entire component tree under that page.
*/ */
constructor(rootAddress: string, publicationTree: PublicationTree, pagePathname: string) { constructor(rootAddress: string, publicationTree: PublicationTree, pagePathname: string) {
// TODO: Build out the root entry correctly.
this.#tocRoot = {
title: '',
href: '',
expanded: false,
children: null,
};
this.#publicationTree = publicationTree; this.#publicationTree = publicationTree;
this.#pagePathname = pagePathname; this.#pagePathname = pagePathname;
@ -88,8 +80,10 @@ export class TableOfContents {
currentParentTocNode!.children ??= []; currentParentTocNode!.children ??= [];
const childTocEntry: TocEntry = { const childTocEntry: TocEntry = {
address,
title: childEvent.getMatchingTags('title')[0][1], title: childEvent.getMatchingTags('title')[0][1],
href: `${this.#pagePathname}#${this.#normalizeHashPath(childEvent.getMatchingTags('title')[0][1])}`, href: `${this.#pagePathname}#${this.#normalizeHashPath(childEvent.getMatchingTags('title')[0][1])}`,
depth: i + 1,
expanded: false, expanded: false,
children: null, children: null,
}; };
@ -130,8 +124,10 @@ export class TableOfContents {
const href = `${this.#pagePathname}#${id}`; const href = `${this.#pagePathname}#${id}`;
const tocEntry: TocEntry = { const tocEntry: TocEntry = {
address: parentEntry.address,
title, title,
href, href,
depth,
expanded: false, expanded: false,
children: null, children: null,
}; };

Loading…
Cancel
Save