diff --git a/packages/framework/resources/views/components/docs/table-of-contents.blade.php b/packages/framework/resources/views/components/docs/table-of-contents.blade.php
index 3165353ea02..ba9bdeb1741 100644
--- a/packages/framework/resources/views/components/docs/table-of-contents.blade.php
+++ b/packages/framework/resources/views/components/docs/table-of-contents.blade.php
@@ -4,10 +4,12 @@
@foreach($items as $item)
-
-
- #
- {{ $item['title'] }}
-
+ @if(isset($item['identifier']))
+
+ #
+ {{ $item['title'] }}
+
+ @endif
@if(! empty($item['children']))
diff --git a/packages/framework/tests/Feature/Views/SidebarTableOfContentsViewTest.php b/packages/framework/tests/Feature/Views/SidebarTableOfContentsViewTest.php
index 5f1e09c5833..5db1fcb4b90 100644
--- a/packages/framework/tests/Feature/Views/SidebarTableOfContentsViewTest.php
+++ b/packages/framework/tests/Feature/Views/SidebarTableOfContentsViewTest.php
@@ -90,6 +90,62 @@ public function testCanGenerateTableOfContentsForDocumentUsingSetextHeaders()
);
}
+ public function testCanGenerateTableOfContentsWithNonLogicalHeadingOrder()
+ {
+ $markdown = <<<'MARKDOWN'
+ # Level 1
+ ### Level 3
+ #### Level 4
+ ## Level 2
+ # Level 1B
+ ### Level 3B
+ MARKDOWN;
+
+ $result = $this->render($markdown);
+
+ $this->assertIsString($result);
+
+ $this->assertHtmlStructure(<<<'HTML'
+
+ -
+
+
+ -
+
+ #
+ Level 2
+
+
+ -
+
+
+
+ HTML, $result
+ );
+ }
+
public function testNonHeadingMarkdownIsRemoved()
{
$expected = <<<'MARKDOWN'
diff --git a/packages/framework/tests/Unit/GeneratesSidebarTableOfContentsTest.php b/packages/framework/tests/Unit/GeneratesSidebarTableOfContentsTest.php
index 32952775d32..e340e1bf142 100644
--- a/packages/framework/tests/Unit/GeneratesSidebarTableOfContentsTest.php
+++ b/packages/framework/tests/Unit/GeneratesSidebarTableOfContentsTest.php
@@ -107,6 +107,30 @@ public function testCanGenerateTableOfContentsForDocumentUsingSetextHeaders()
);
}
+ public function testCanGenerateTableOfContentsWithNonLogicalHeadingOrder()
+ {
+ $markdown = "# Level 1\n### Level 3\n#### Level 4\n";
+ $result = (new GeneratesTableOfContents($markdown))->execute();
+
+ $this->assertSame([
+ [
+ 'children' => [
+ [
+ 'title' => 'Level 3',
+ 'identifier' => 'level-3',
+ 'children' => [
+ [
+ 'title' => 'Level 4',
+ 'identifier' => 'level-4',
+ 'children' => [],
+ ],
+ ],
+ ],
+ ],
+ ],
+ ], $result);
+ }
+
public function testNonHeadingMarkdownIsIgnored()
{
$expected = <<<'MARKDOWN'