-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2047 from hydephp/custom-markdown-heading-renderer
[2.x] Custom Markdown heading renderer
- Loading branch information
Showing
17 changed files
with
842 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/framework/resources/views/components/markdown-heading.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@props([ | ||
'level' => 1, | ||
'id' => null, | ||
'extraAttributes' => [], | ||
'addPermalink' => config('markdown.permalinks.enabled', true), | ||
]) | ||
|
||
@php | ||
$tag = 'h' . $level; | ||
$id = $id ?? \Illuminate\Support\Str::slug($slot); | ||
$extraAttributes = array_merge($extraAttributes, [ | ||
'id' => $addPermalink ? $id : ($extraAttributes['id'] ?? null), | ||
'class' => trim(($extraAttributes['class'] ?? '') . ($addPermalink ? ' group w-fit scroll-mt-2' : '')), | ||
]); | ||
@endphp | ||
|
||
<{{ $tag }} {{ $attributes->merge($extraAttributes) }}> | ||
{!! $slot !!} | ||
@if($addPermalink === true) | ||
<a href="#{{ $id }}" class="heading-permalink opacity-0 ml-1 transition-opacity duration-300 ease-linear px-1 group-hover:opacity-100 focus:opacity-100 group-hover:grayscale-0 focus:grayscale-0" title="Permalink"> | ||
# | ||
</a> | ||
@endif | ||
</{{ $tag }}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/framework/src/Markdown/Processing/HeadingRenderer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
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; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
|
||
/** | ||
* Renders a heading node, and supports built-in permalink generation. | ||
* | ||
* @see \League\CommonMark\Extension\CommonMark\Renderer\Block\HeadingRenderer | ||
*/ | ||
class HeadingRenderer implements NodeRendererInterface | ||
{ | ||
/** @var ?class-string<\Hyde\Pages\Concerns\HydePage> */ | ||
protected ?string $pageClass = null; | ||
|
||
/** @var array<string> */ | ||
protected array $headingRegistry = []; | ||
|
||
/** @param ?class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */ | ||
public function __construct(string $pageClass = null, array &$headingRegistry = []) | ||
{ | ||
$this->pageClass = $pageClass; | ||
$this->headingRegistry = &$headingRegistry; | ||
} | ||
|
||
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string | ||
{ | ||
if (! ($node instanceof Heading)) { | ||
throw new \InvalidArgumentException('Incompatible node type: '.get_class($node)); | ||
} | ||
|
||
$content = $childRenderer->renderNodes($node->children()); | ||
|
||
$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(); | ||
|
||
return $this->postProcess($rendered); | ||
} | ||
|
||
/** @internal */ | ||
public function canAddPermalink(string $content, int $level): bool | ||
{ | ||
return config('markdown.permalinks.enabled', true) | ||
&& $level >= config('markdown.permalinks.min_level', 2) | ||
&& $level <= config('markdown.permalinks.max_level', 6) | ||
&& ! str_contains($content, 'class="heading-permalink"') | ||
&& in_array($this->pageClass, config('markdown.permalinks.pages', [DocumentationPage::class])); | ||
} | ||
|
||
/** @internal */ | ||
public function postProcess(string $html): string | ||
{ | ||
$html = str_replace('class=""', '', $html); | ||
$html = preg_replace('/<h([1-6]) >/', '<h$1>', $html); | ||
|
||
return implode('', array_map('trim', explode("\n", $html))); | ||
} | ||
|
||
protected function makeHeadingId(string $contents): string | ||
{ | ||
$identifier = $this->ensureIdentifierIsUnique(Str::slug($contents)); | ||
|
||
$this->headingRegistry[] = $identifier; | ||
|
||
return $identifier; | ||
} | ||
|
||
protected function ensureIdentifierIsUnique(string $slug): string | ||
{ | ||
$identifier = $slug; | ||
$suffix = 2; | ||
|
||
while (in_array($identifier, $this->headingRegistry)) { | ||
$identifier = $slug.'-'.$suffix++; | ||
} | ||
|
||
return $identifier; | ||
} | ||
} |
Oops, something went wrong.