]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/PageContent.php
enh(recently updated): show updatedBy and updated_at
[bookstack] / app / Entities / Tools / PageContent.php
index a6ba352a834c0f5f9709c03ec79fe42f326d2138..b95131fce2e25561c22131d3d81e3dc4d30a8aac 100644 (file)
@@ -12,6 +12,8 @@ use BookStack\Uploads\ImageRepo;
 use BookStack\Uploads\ImageService;
 use BookStack\Util\HtmlContentFilter;
 use DOMDocument;
+use DOMElement;
+use DOMNode;
 use DOMNodeList;
 use DOMXPath;
 use Illuminate\Support\Str;
@@ -193,6 +195,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) {
@@ -228,9 +239,9 @@ class PageContent
      * A map for existing ID's should be passed in to check for current existence.
      * Returns a pair of strings in the format [old_id, new_id].
      */
-    protected function setUniqueId(\DOMNode $element, array &$idMap): array
+    protected function setUniqueId(DOMNode $element, array &$idMap): array
     {
-        if (!$element instanceof \DOMElement) {
+        if (!$element instanceof DOMElement) {
             return ['', ''];
         }
 
@@ -312,7 +323,7 @@ class PageContent
      */
     protected function headerNodesToLevelList(DOMNodeList $nodeList): array
     {
-        $tree = collect($nodeList)->map(function ($header) {
+        $tree = collect($nodeList)->map(function (DOMElement $header) {
             $text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
             $text = mb_substr($text, 0, 100);