Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Custom Markdown heading renderer #2047

Merged
merged 100 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
aff4388
Create HeadingRenderer.php
caendesilva Dec 1, 2024
ed7f281
Create markdown-heading.blade.php
caendesilva Dec 1, 2024
7e45297
Render slot literally
caendesilva Dec 1, 2024
5607c6d
Indent code
caendesilva Dec 1, 2024
ec6b2c6
Add an attribute for controlling permalink state
caendesilva Dec 1, 2024
98ddab1
Set state from class
caendesilva Dec 1, 2024
e1ccda8
Fix recursion issue
caendesilva Dec 1, 2024
24e459f
Support extra attributes
caendesilva Dec 1, 2024
2c3e07b
Forward node attributes
caendesilva Dec 1, 2024
6631d86
Format props
caendesilva Dec 1, 2024
07d5e97
Update added config setting
caendesilva Dec 1, 2024
c430b9b
Apply fixes from StyleCI
StyleCIBot Dec 1, 2024
b0a4ef9
Register the custom heading renderer
caendesilva Dec 1, 2024
87f03f7
Extract helper method
caendesilva Dec 1, 2024
585764d
Create heading permalinks configuration
caendesilva Dec 1, 2024
bfc93e3
Better option name
caendesilva Dec 1, 2024
cb6c70e
Construct with page class
caendesilva Dec 1, 2024
94cada0
Add permalinks only for configured pages
caendesilva Dec 1, 2024
bcf5cdd
Format long line
caendesilva Dec 1, 2024
22d30c5
Nullable class string
caendesilva Dec 1, 2024
6fbb656
Remove auto-configuration for HeadingPermalinkExtension
caendesilva Dec 1, 2024
b7f0509
Change component to match markup made by extension
caendesilva Dec 1, 2024
539a8eb
Use more explicit assertions
caendesilva Dec 1, 2024
53b158f
Make the permalink headings level configurable
caendesilva Dec 1, 2024
bb9fd0a
Default to minimum level of 2
caendesilva Dec 1, 2024
d8896ad
Introduce local variable
caendesilva Dec 1, 2024
2c53963
Post process to normalize result to CommonMark implementation
caendesilva Dec 1, 2024
358c799
Replace multiple replacements with single Regex
caendesilva Dec 1, 2024
cc8cc6d
Add a test for when heading permalinks are disabled
caendesilva Dec 1, 2024
2475010
Sync configuration files
caendesilva Dec 1, 2024
30469f4
Mock the View factory
caendesilva Dec 1, 2024
a6bcaa1
Fix mock bindings
caendesilva Dec 1, 2024
567b505
Granular mocks
caendesilva Dec 1, 2024
22a7745
Update test to test the automatic permalinks feature
caendesilva Dec 1, 2024
1100719
Remove the `MarkdownService::withPermalinks` method
caendesilva Dec 1, 2024
d03dbf3
Add todo
caendesilva Dec 1, 2024
9311bd1
Create HeadingRendererUnitTest.php
caendesilva Dec 1, 2024
80ed9d4
Create MarkdownHeadingRendererTest.php
caendesilva Dec 1, 2024
5e243e7
Create a minimal view environment
caendesilva Dec 1, 2024
c7eb6bb
Register finder path
caendesilva Dec 1, 2024
bad9eb3
Create and configure the engine resolver
caendesilva Dec 1, 2024
fd6df97
Extract helper method
caendesilva Dec 1, 2024
4995016
Basic constructor tests
caendesilva Dec 1, 2024
a13f1e8
Cleanup helper method code
caendesilva Dec 1, 2024
0d08cc5
Helper to mock child node renderer
caendesilva Dec 1, 2024
6dc0095
Improve formatting
caendesilva Dec 1, 2024
a6b26be
Set default value
caendesilva Dec 1, 2024
598d9c9
Implement initial unit test
caendesilva Dec 1, 2024
5b00e79
Test can add permalink based on configuration
caendesilva Dec 1, 2024
bc876e8
Refactor to data provider attribute
caendesilva Dec 1, 2024
10e905a
Clean up after test
caendesilva Dec 1, 2024
703e70c
Automatically reset to cached config default
caendesilva Dec 1, 2024
039c11a
More extensive range testing
caendesilva Dec 1, 2024
3a709dd
Revert "More extensive range testing"
caendesilva Dec 1, 2024
496260c
Make protected helper methods public internal
caendesilva Dec 1, 2024
90ff929
Remove tests for later refactor
caendesilva Dec 1, 2024
0382947
Cleaner and extended testing
caendesilva Dec 1, 2024
240e3dd
Test more code paths
caendesilva Dec 2, 2024
bbd5a0b
Clarify test name
caendesilva Dec 2, 2024
1195704
Apply fixes from StyleCI
StyleCIBot Dec 2, 2024
65cc1ff
Unit test the post processing
caendesilva Dec 2, 2024
2dd5c32
Clarify test name to specify the reason behind it
caendesilva Dec 2, 2024
b1b6266
Add extra test case
caendesilva Dec 2, 2024
2f31b9b
Apply fixes from StyleCI
StyleCIBot Dec 2, 2024
3c3661a
Clean up test code to remove focus from implementation details
caendesilva Dec 2, 2024
b00ea06
Implement the high level feature test
caendesilva Dec 2, 2024
a7b52d6
Add some more assertions
caendesilva Dec 2, 2024
e67a640
Assert on the full output when relevant
caendesilva Dec 2, 2024
6fe5cb8
Expand feature testing
caendesilva Dec 2, 2024
3c285a7
Add todo
caendesilva Dec 2, 2024
43d82a4
Test escaping
caendesilva Dec 2, 2024
4e80297
Document custom Markdown heading renderer
caendesilva Dec 3, 2024
f53eb4d
Remove the enabled key from the permalinks configuration
caendesilva Dec 3, 2024
ce21d66
Remove the unused `canEnablePermalinks` from `MarkdownService`
caendesilva Dec 3, 2024
3d3ed48
Improve the test
caendesilva Dec 3, 2024
7f79bee
Update RELEASE_NOTES.md
caendesilva Dec 4, 2024
0710e95
More semantic Markdown heading permalinks
caendesilva Dec 4, 2024
8737a52
Conditionally add element identifier
caendesilva Dec 4, 2024
04aa325
Remove support for custom identifier when it breaks permalinks
caendesilva Dec 4, 2024
e7be152
Expect semantic heading identifiers
caendesilva Dec 4, 2024
3c66d44
Refactor to use Tailwind permalink styles
caendesilva Dec 4, 2024
5f90cbc
Tweak styles to match original behaviour
caendesilva Dec 4, 2024
af9f4fa
Update tests for semantic Markdown permalink headers
caendesilva Dec 4, 2024
1caa631
Add scroll margin to header
caendesilva Dec 5, 2024
9c49d46
Format long line
caendesilva Dec 5, 2024
7ebd17f
Merge pull request #2052 from hydephp/semantic-markdown-heading-perma…
caendesilva Dec 5, 2024
418044e
Merge attributes dynamically
caendesilva Dec 5, 2024
4fb8e57
Unwrap unnecessary unwrapping
caendesilva Dec 5, 2024
f9f9755
Trim empty class directives
caendesilva Dec 5, 2024
a36760b
Allow side effect of extra space for edge case
caendesilva Dec 5, 2024
c78ddeb
Inline local variable override
caendesilva Dec 5, 2024
3feddd3
Revert "Inline local variable override"
caendesilva Dec 5, 2024
0d9f3e8
Remove todo as it can be handled in Blade
caendesilva Dec 5, 2024
99e2d1e
Add internal heading registry
caendesilva Dec 5, 2024
7f40d01
Start duplicate suffixes at two
caendesilva Dec 5, 2024
c4e67ff
Add suffix to heading identifiers of the same name
caendesilva Dec 5, 2024
f0adee1
Update RELEASE_NOTES.md
caendesilva Dec 5, 2024
155c456
Add more test values to ensure proper count
caendesilva Dec 5, 2024
b959d2d
Cleanup and refactor code
caendesilva Dec 5, 2024
3df7aa2
Add array types
caendesilva Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@props(['level' => 1, 'id' => null, 'addPermalink' => config('markdown.features.permalinks', true)])

@php
$tag = 'h' . $level;
$id = $id ?? \Illuminate\Support\Str::slug($slot);
@endphp

<{{ $tag }} {{ $attributes->merge(['id' => $id]) }}>
{!! $slot !!}
caendesilva marked this conversation as resolved.
Show resolved Hide resolved
@if($addPermalink === true)
<a href="#{{ $id }}" class="heading-permalink" aria-label="Permalink for this section"></a>
@endif
</{{ $tag }}>
33 changes: 33 additions & 0 deletions packages/framework/src/Markdown/Processing/HeadingRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Hyde\Markdown\Processing;

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
{
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string

Check warning on line 19 in packages/framework/src/Markdown/Processing/HeadingRenderer.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Markdown/Processing/HeadingRenderer.php#L19

Added line #L19 was not covered by tests
{
if (! ($node instanceof Heading)) {
throw new \InvalidArgumentException('Incompatible node type: '.get_class($node));

Check warning on line 22 in packages/framework/src/Markdown/Processing/HeadingRenderer.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Markdown/Processing/HeadingRenderer.php#L21-L22

Added lines #L21 - L22 were not covered by tests
}

$content = $childRenderer->renderNodes($node->children());

Check warning on line 25 in packages/framework/src/Markdown/Processing/HeadingRenderer.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Markdown/Processing/HeadingRenderer.php#L25

Added line #L25 was not covered by tests

return view('hyde::components.markdown-heading', [
'level' => $node->getLevel(),
'slot' => $content,
'addPermalink' => config('markdown.features.permalinks', true) && ! str_contains($content, 'class="heading-permalink"'),
])->render();

Check warning on line 31 in packages/framework/src/Markdown/Processing/HeadingRenderer.php

View check run for this annotation

Codecov / codecov/patch

packages/framework/src/Markdown/Processing/HeadingRenderer.php#L27-L31

Added lines #L27 - L31 were not covered by tests
}
}