]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/PageContent.php
Added test for logical-theme-system command registration
[bookstack] / app / Entities / Tools / PageContent.php
index 52a10ae872ebbcf55827acf345296b20a4a327da..c60cf03113a9cceaa980f040325b5df8cacd0e8d 100644 (file)
@@ -156,7 +156,7 @@ class PageContent
     /**
      * Parse a base64 image URI into the data and extension.
      *
-     * @return array{extension: array, data: string}
+     * @return array{extension: string, data: string}
      */
     protected function parseBase64ImageUri(string $uri): array
     {
@@ -193,6 +193,15 @@ class PageContent
             }
         }
 
+        // Set ids on nested header nodes
+        $nestedHeaders = $xPath->query('//body//*//h1|//body//*//h2|//body//*//h3|//body//*//h4|//body//*//h5|//body//*//h6');
+        foreach ($nestedHeaders as $nestedHeader) {
+            [$oldId, $newId] = $this->setUniqueId($nestedHeader, $idMap);
+            if ($newId && $newId !== $oldId) {
+                $this->updateLinks($xPath, '#' . $oldId, '#' . $newId);
+            }
+        }
+
         // Ensure no duplicate ids within child items
         $idElems = $xPath->query('//body//*//*[@id]');
         foreach ($idElems as $domElem) {
@@ -230,7 +239,7 @@ class PageContent
      */
     protected function setUniqueId(\DOMNode $element, array &$idMap): array
     {
-        if (get_class($element) !== 'DOMElement') {
+        if (!$element instanceof \DOMElement) {
             return ['', ''];
         }
 
@@ -242,7 +251,7 @@ class PageContent
             return [$existingId, $existingId];
         }
 
-        // Create an unique id for the element
+        // Create a unique id for the element
         // Uses the content as a basis to ensure output is the same every time
         // the same content is passed through.
         $contentId = 'bkmrk-' . mb_substr(strtolower(preg_replace('/\s+/', '-', trim($element->nodeValue))), 0, 20);
@@ -390,7 +399,7 @@ class PageContent
      */
     protected function fetchSectionOfPage(Page $page, string $sectionId): string
     {
-        $topLevelTags = ['table', 'ul', 'ol'];
+        $topLevelTags = ['table', 'ul', 'ol', 'pre'];
         $doc = $this->loadDocumentFromHtml($page->html);
 
         // Search included content for the id given and blank out if not exists.