Sayfa.TOC (Sayfa v0.5.0)

Copy Markdown View Source

Extracts table of contents from rendered HTML.

Parses heading elements (h2-h6) from HTML and returns a list of TOC entries with level, text, and anchor ID. Skips h1 as that is typically the page title.

Requires headings to have anchor IDs (enabled via MDEx header_id_prefix option).

Examples

iex> html = ~s(<h2><a href="#intro" aria-hidden="true" class="anchor" id="intro"></a>Introduction</h2>)
iex> Sayfa.TOC.extract(html)
[%{level: 2, text: "Introduction", id: "intro"}]

Summary

Functions

Extracts table of contents entries from HTML.

Functions

extract(html)

@spec extract(String.t()) :: [%{level: integer(), text: String.t(), id: String.t()}]

Extracts table of contents entries from HTML.

Returns a list of maps with :level, :text, and :id keys. Only extracts h2-h6 headings (h1 is skipped as the page title). Returns an empty list if no headings are found.

Examples

iex> html = ~s(<h2><a href="#intro" aria-hidden="true" class="anchor" id="intro"></a>Introduction</h2>)
iex> Sayfa.TOC.extract(html)
[%{level: 2, text: "Introduction", id: "intro"}]

iex> Sayfa.TOC.extract("<p>No headings here</p>")
[]

iex> Sayfa.TOC.extract("")
[]