CoolCMS API

HtmlOutline
in package

FinalYes

Outline of a text page: extracts headings from HTML and adds anchors to them.

Text pages (terms, privacy policy, questions and answers) keep their content as one piece of HTML in cms_system_page_texts.text; the editor writes no id, so headings cannot be linked to. This helper adds them and returns the list from which the layout renders navigation.

IMPORTANT: it runs on the server on purpose. An outline built by JavaScript would not be in the HTML, so readers and search engines would not get it and without a script the section links would not work at all.

Headings without text are skipped (an empty <h2> from the editor is a common artifact) and an id already on a heading is respected, so the editor can name an anchor and keep it across saves.

Table of Contents

Methods

build()  : array{html: string, items: list}
innerHtml()  : string
Content of the root without the root itself.
uniqueId()  : string
Returns an anchor that does not repeat on the page.

Methods

build()

public static build(string $html[, string $tag = 'h2' ]) : array{html: string, items: list}
Parameters
$html : string

page content (a fragment, not a whole document)

$tag : string = 'h2'

heading tag the outline is built from

Return values
array{html: string, items: list}

innerHtml()

Content of the root without the root itself.

private static innerHtml(DOMDocument $document, DOMElement $root) : string
Parameters
$document : DOMDocument
$root : DOMElement
Return values
string

uniqueId()

Returns an anchor that does not repeat on the page.

private static uniqueId(string $base, array<string, true> &$used) : string

IMPORTANT: webalize() turns two headings differing only in diacritics or case into the same value, and two equal ids in a document make the second outline link jump to the first section. The numbered suffix is the only safeguard.

Parameters
$base : string
$used : array<string, true>
Return values
string
On this page

Search results