Update (front post): Fix parsing issue
This commit is contained in:
@@ -189,21 +189,26 @@ private function injectTableOfContents($html)
|
||||
}
|
||||
|
||||
// Create the Table of Contents
|
||||
$toc = '<div class="p-3 rounded-3 bg-light mb-3"><ol>';
|
||||
$h2Elements->each(function (Crawler $node, $i) use (&$toc) {
|
||||
$content = $node->text();
|
||||
$id = 'link-'.$i; // Creating a simple id based on the index
|
||||
$node->getNode(0)->setAttribute('id', $id); // Set the id to the h2 tag
|
||||
$toc .= "<li class=\"py-1\"><a class=\"text-decoration-none hover-text-decoration-underline\" href='#{$id}'>{$content}</a></li>";
|
||||
$domDocument = $crawler->getNode(0)->ownerDocument;
|
||||
$tocDiv = $domDocument->createElement('div');
|
||||
$tocDiv->setAttribute('class', 'p-3 rounded-3 bg-light mb-3');
|
||||
$ol = $domDocument->createElement('ol');
|
||||
$tocDiv->appendChild($ol);
|
||||
|
||||
$h2Elements->each(function (Crawler $node, $i) use ($ol) {
|
||||
$content = htmlspecialchars($node->text(), ENT_XML1 | ENT_COMPAT, 'UTF-8');
|
||||
$id = 'link-'.$i;
|
||||
$node->getNode(0)->setAttribute('id', $id);
|
||||
$li = $ol->appendChild(new \DOMElement('li'));
|
||||
$li->setAttribute('class', 'py-1');
|
||||
$a = $li->appendChild(new \DOMElement('a', $content));
|
||||
$a->setAttribute('class', 'text-decoration-none hover-text-decoration-underline');
|
||||
$a->setAttribute('href', '#'.$id);
|
||||
});
|
||||
$toc .= '</ol></div>';
|
||||
|
||||
// Insert TOC after h1
|
||||
$domDocument = $crawler->getNode(0)->ownerDocument;
|
||||
$fragment = $domDocument->createDocumentFragment();
|
||||
$fragment->appendXML($toc);
|
||||
$h1Node = $crawler->filter('h1')->getNode(0);
|
||||
$h1Node->parentNode->insertBefore($fragment, $h1Node->nextSibling);
|
||||
$h1Node->parentNode->insertBefore($tocDiv, $h1Node->nextSibling);
|
||||
|
||||
// Get the updated HTML
|
||||
$updatedHtml = $crawler->filter('body')->html();
|
||||
|
||||
Reference in New Issue
Block a user