diff --git a/TableOfContents.php b/TableOfContents.php index 1fc5f2c..c657044 100644 --- a/TableOfContents.php +++ b/TableOfContents.php @@ -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; @@ -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. *