Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion TableOfContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function getList($document, $headers, &$index = 0)
$li_element = $document->createElement('li');
$a_element = $document->createElement('a');
$a_element->setAttribute('href', "#$id");
$a_element->nodeValue = $curr_header->nodeValue;
$a_element->nodeValue = $this->extractTextWithoutCertainTags($curr_header, ['sup']);
$li_element->appendChild($a_element);

$next_header = ($index + 1 < $headers->length) ? $headers[$index + 1] : null;
Expand All @@ -254,6 +254,33 @@ private function getList($document, $headers, &$index = 0)
return $list_element;
}

/**
* Extract text from a node without the text from certain tags.
*
* @param DOMNode $node
* @param string[] $ignoredTags
* @return string
*/
private function extractTextWithoutCertainTags($node, $ignoredTags)
{
$text = '';

foreach ($node->childNodes as $child) {
if ($child->nodeType === XML_TEXT_NODE) {
// Add text from text nodes
$text .= $child->nodeValue;
} elseif ($child->nodeType === XML_ELEMENT_NODE) {
// Check if the current tag is not in the list of ignored tags
if (!in_array(strtolower($child->tagName), $ignoredTags, true)) {
// Recursively get text from elements that are not in the ignored list
$text .= $this->extractTextWithoutCertainTags($child, $ignoredTags);
}
}
}

return $text;
}

/**
* Generate a slug from a string.
*
Expand Down