Generating a Table of Contents!
Introduction:
When working with lengthy content or documentation, a table of contents can greatly enhance user experience by providing an organized navigation system. In this blog post, we’ll delve into the TableOfContents , a tool for generating a table of contents. We’ll explore its key methods and functionality to gain a deeper understanding of how it works. Let’s get started!
Understanding the TableOfContents Class:
The TableOfContents library revolves around the TableOfContents class, which acts as the main entry point for generating a table of contents. Let’s explore some of its essential properties and methods:
content: HTMLElement | null: This property represents the content section of the page. It is obtained by querying the DOM using the providedcontentSelectoroption.nodeList: NodeType[] | undefined: This property stores an array ofNodeTypeobjects. EachNodeTypeobject represents a heading element found within the content section.nodeObject: NodeType | null: This property holds the hierarchical structure of the headings, forming a tree-like representation.markup: HTMLElement | null: This property represents the generated table of contents markup.options?: OptionsType: This property holds the options passed to theTableOfContentsclass during initialization.settings: OptionsType: This property contains the merged default options and user-defined options.init(): This method serves as the entry point for generating the table of contents. It invokes other internal methods to find headings, create a hierarchical structure, generate the markup, and append it to the specified container.
Generating the Table of Contents:
Let’s explore the sequence of steps involved in generating the table of contents using the TableOfContents class:
- Finding Headings: The
findAllHeadersmethod is responsible for querying the DOM and finding the headings based on the provided attributes (h1,h2,h3). It creates an array ofNodeTypeobjects, representing each heading element found within the content section. - Creating Hierarchy: The
createHierarchymethod takes the array ofNodeTypeobjects and constructs a hierarchical structure based on the headings’ levels. It assigns child nodes to their respective parent nodes, forming a tree-like structure. - Generating Markup: The
generateMarkupmethod recursively traverses the hierarchical structure and generates the HTML markup for the table of contents. It creates<ul>and<li>elements, and for each heading, it creates an<a>element with appropriate attributes. - Appending Markup: Once the markup is generated, it is appended to the specified container element using the
appendTooption. The generated table of contents becomes part of the DOM, ready for display.
// Initializing the TableOfContents
const toc = new TableOfContents({
attributes: ['h1', 'h2', 'h3'],
appendTo: 'toc',
contentSelector: 'content',
});
// Finding Headings
private findAllHeaders = (): void => {
// ...
};
// Creating Hierarchy
private createHierarchy = (): void => {
// ...
};
// Generating Markup
private generateMarkup = (node: NodeType | null): HTMLElement | null => {
// ...
};
// Appending Markup
public init = (): void => {
// ...
};
