You are here

function toc_node_add_anchors in TOC Node 7

Add anchors, gather link data, add TOC tags which are used to work out the order of headings later.

Parameters

$dom_document:

$anchor_count:

$links:

$heading_level:

1 call to toc_node_add_anchors()
toc_node_generate in ./toc_node.module
Generate the TOC.

File

./toc_node.module, line 386

Code

function toc_node_add_anchors(&$dom_document, &$anchor_count, &$links, $heading_level) {
  $headers = $dom_document
    ->getElementsByTagName('h' . $heading_level);
  foreach ($headers as $header) {
    $anchor_count++;

    // TOC links.
    $label = trim($header->nodeValue);
    $links['toc-' . $anchor_count]['label'] = $label;
    $links['toc-' . $anchor_count]['level'] = $heading_level;

    // Headings, add class.
    $classes = $header
      ->getAttribute('class');
    $header
      ->setAttribute('class', $classes . ' toc-headings');

    // Anchors above headings.
    $anchor_div = $dom_document
      ->createElement('div');
    $anchor_div
      ->setAttribute('class', 'toc-item-anchor');
    $target = $dom_document
      ->createElement('a');
    $target
      ->setAttribute('name', 'toc-' . $anchor_count);
    $anchor_div
      ->appendChild($target);
    $header->parentNode
      ->insertBefore($anchor_div, $header);
    $toc_tag = $dom_document
      ->createElement('toc');
    $toc_tag
      ->setAttribute('class', 'toc-' . $anchor_count);
    $header
      ->appendChild($toc_tag);
  }
}