]> BookStack Code Mirror - bookstack/commitdiff
feat(PageContent): set unique ids on nested headers 3069/head
authorjulesdevops <redacted>
Thu, 18 Nov 2021 22:01:37 +0000 (23:01 +0100)
committerjulesdevops <redacted>
Sun, 21 Nov 2021 21:45:25 +0000 (22:45 +0100)
app/Entities/Tools/PageContent.php
tests/Entity/PageContentTest.php

index 45bfe8fa1740bf0c9c33cbd2432967a74cd73547..c8204a18186893c874f03dcdb65e820e03c3d638 100644 (file)
@@ -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) {
index 4dace533b1617242c22d3e057a966f2113bbd74f..9524186c8f1cdc64f0b9cd43f215515948cf98b1 100644 (file)
@@ -670,4 +670,24 @@ class PageContentTest extends TestCase
         $page->refresh();
         $this->assertStringContainsString('<img src=""', $page->html);
     }
+
+    public function test_nested_headers_gets_assigned_an_id()
+    {
+        $this->asEditor();
+        $page = Page::query()->first();
+
+        $content = '<table><tbody><tr><td><h5>Simple Test</h5></td></tr></tbody></table>';
+        $this->put($page->getUrl(), [
+            'name'    => $page->name,
+            'html'    => $content,
+            'summary' => '',
+        ]);
+
+        $updatedPage = Page::query()->where('id', '=', $page->id)->first();
+
+        // The top level <table> node will get assign the bkmrk-simple-test id because the system will
+        // take the node value of h5
+        // So the h5 should get the bkmrk-simple-test-1 id
+        $this->assertStringContainsString('<h5 id="bkmrk-simple-test-1">Simple Test</h5>', $updatedPage->html);
+    }
 }