Skip to content

Commit

Permalink
Skip empty identifiers to allow just child items
Browse files Browse the repository at this point in the history
Required if headings don't follow a sensible order
  • Loading branch information
caendesilva committed Dec 10, 2024
1 parent 59fc714 commit 7092df6
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
<ul @class([$isChild ? 'pl-2' : 'table-of-contents pb-3'])>
@foreach($items as $item)
<li class="my-0.5">
<a href="#{{ $item['identifier'] }}" class="-ml-8 pl-8 opacity-80 hover:opacity-100 hover:bg-gray-200/20 transition-all duration-300">
<span class="text-[75%] opacity-50 hover:opacity-100 transition-opacity duration-300">#</span>
{{ $item['title'] }}
</a>
@if(isset($item['identifier']))
<a href="#{{ $item['identifier'] }}" class="-ml-8 pl-8 opacity-80 hover:opacity-100 hover:bg-gray-200/20 transition-all duration-300">
<span class="text-[75%] opacity-50 hover:opacity-100 transition-opacity duration-300">#</span>
{{ $item['title'] }}
</a>
@endif

@if(! empty($item['children']))
<x-hyde::docs.table-of-contents :items="$item['children']" :isChild="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<ul class="table-of-contents">
<li>
<ul>
<li>
<a href="#level-3">
<span>#</span>
Level 3
</a>
<ul>
<li>
<a href="#level-4">
<span>#</span>
Level 4
</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a href="#level-2">
<span>#</span>
Level 2
</a>
</li>
<li>
<ul>
<li>
<a href="#level-3b">
<span>#</span>
Level 3B
</a>
</li>
</ul>
</li>
</ul>
HTML, $result
);
}

public function testNonHeadingMarkdownIsRemoved()
{
$expected = <<<'MARKDOWN'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 7092df6

Please sign in to comment.