Skip to content

Commit

Permalink
Add internal heading registry
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 5, 2024
1 parent 0d9f3e8 commit 5eca58e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class MarkdownService
/** @var array<class-string<\Hyde\Markdown\Contracts\MarkdownPostProcessorContract>> */
protected array $postprocessors = [];

/** @var array<string> */
protected array $headingRegistry = [];

public function __construct(string $markdown, ?string $pageClass = null)
{
$this->pageClass = $pageClass;
Expand Down Expand Up @@ -243,6 +246,6 @@ protected static function findLineContentPositions(array $lines): array
protected function configureCustomHeadingRenderer(): void
{
$environment = $this->converter->getEnvironment();
$environment->addRenderer(Heading::class, new HeadingRenderer($this->pageClass));
$environment->addRenderer(Heading::class, new HeadingRenderer($this->pageClass, $this->headingRegistry));
}
}
24 changes: 23 additions & 1 deletion packages/framework/src/Markdown/Processing/HeadingRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Markdown\Processing;

use Hyde\Pages\DocumentationPage;
use Illuminate\Support\Str;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
Expand All @@ -20,10 +21,13 @@ class HeadingRenderer implements NodeRendererInterface
/** @var ?class-string<\Hyde\Pages\Concerns\HydePage> */
protected ?string $pageClass = null;

protected array $headingRegistry = [];

/** @param ?class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */
public function __construct(string $pageClass = null)
public function __construct(string $pageClass = null, array &$headingRegistry = [])
{
$this->pageClass = $pageClass;
$this->headingRegistry = &$headingRegistry;
}

public function render(Node $node, ChildNodeRendererInterface $childRenderer): string
Expand All @@ -37,6 +41,7 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer): s
$rendered = view('hyde::components.markdown-heading', [
'level' => $node->getLevel(),
'slot' => $content,
'id' => $this->makeHeadingId($content),
'addPermalink' => $this->canAddPermalink($content, $node->getLevel()),
'extraAttributes' => $node->data->get('attributes'),
])->render();
Expand All @@ -62,4 +67,21 @@ public function postProcess(string $html): string

return implode('', array_map('trim', explode("\n", $html)));
}

protected function makeHeadingId(string $contents): string
{
$identifier = Str::slug($contents);

// Check for duplicates in the tracker
$id = $identifier;
$suffix = 1;
while (in_array($id, $this->headingRegistry)) {
$id = $identifier . '-' . $suffix++;
}

// Record the ID in the tracker and return it
$this->headingRegistry[] = $id;

return $id;
}
}

0 comments on commit 5eca58e

Please sign in to comment.