Merge branch 'assign_ids_to_nested_headers' of https://github.com/Julesdevops/BookStack into Julesdevops-assign_ids_to_nested_headers

This commit is contained in:
Dan Brown 2021-11-22 16:34:28 +00:00
commit 4ddbc9556b
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 29 additions and 0 deletions

View 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) {

View 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);
}
}