From 53ed81f0320e0bfc66450c16d03a0947d48e70c9 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Tue, 27 May 2025 09:30:49 -0500 Subject: [PATCH] Recursively build ToC from HTML markup --- .../publications/table_of_contents.svelte.ts | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/lib/components/publications/table_of_contents.svelte.ts b/src/lib/components/publications/table_of_contents.svelte.ts index c0120d5..3efddd1 100644 --- a/src/lib/components/publications/table_of_contents.svelte.ts +++ b/src/lib/components/publications/table_of_contents.svelte.ts @@ -91,13 +91,26 @@ export class TableOfContents { } } - buildTocFromDocument(parentElement: HTMLElement): void { - const entries: TocEntry[] = []; - + /** + * Builds a table of contents from the DOM subtree rooted at `parentElement`. + * + * @param parentElement The root of the DOM subtree containing the content to be added to the + * ToC. + * @param parentAddress The address of the event corresponding to the DOM subtree root indicated + * by `parentElement`. + * + * This function is intended for use on segments of HTML markup that are not directly derived + * from a structure publication of the kind supported by `PublicationTree`. It may be used to + * produce a table of contents from the contents of a kind `30041` event with AsciiDoc markup, or + * from a kind `30023` event with Markdown content. + */ + buildTocFromDocument( + parentElement: HTMLElement, + parentEntry: TocEntry, + depth: number = 1 + ) { parentElement - .querySelectorAll( - 'h1, h2, h3, h4, h5, h6' - ) + .querySelectorAll(`h${depth}`) .forEach((header) => { const title = header.textContent?.trim(); const id = header.id; @@ -112,10 +125,11 @@ export class TableOfContents { expanded: false, children: null, }; - entries.push(tocEntry); + parentEntry.children ??= []; + parentEntry.children.push(tocEntry); + + this.buildTocFromDocument(header, tocEntry, depth + 1); } }); - - // TODO: Update ToC state within the component. } }