-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
[2.x] Custom Markdown heading renderer #2047
Conversation
Cursor (Claude) analysis of the three approaches for implementing heading permalinks:1. Original CommonMark Extension Implementation protected function configurePermalinksExtension(): void
{
$this->addExtension(HeadingPermalinkExtension::class);
'fragment_prefix' => '',
$this->config = array_merge([
'heading_permalink' => [
'id_prefix' => '',
'fragment_prefix' => '',
'symbol' => '',
'insert' => 'after',
'min_heading_level' => 2,
'aria_hidden' => false,
],
], $this->config);
} Benefits:
Drawbacks:
2. Full Blade Component Replacement@props(['level' => 1, 'id' => null])
@php
$tag = 'h' . $level;
$id = $id ?? \Illuminate\Support\Str::slug($slot);
@endphp
<{{ $tag }} {{ $attributes->merge(['id' => $id]) }}>
{{ $slot }}
@if(config('markdown.features.permalinks', true))
<a href="#{{ $id }}" class="heading-permalink" aria-label="Permalink for this section"></a>
@endif
</{{ $tag }}> Benefits:
Drawbacks:
3. Permalink-Only Component InjectionExample implementation: // components/heading-permalink.blade.php
@props(['id'])
<a href="#{{ $id }}"
class="heading-permalink"
aria-label="Permalink for this section">
</a> Benefits:
Drawbacks:
RecommendationI recommend using the full Blade component replacement approach (#2) because:
.prose h1, .prose h2, .prose h3, .prose h4, .prose h5, .prose h6 {
@apply w-fit;
}
.prose :is(h1,h2,h3,h4,h5,h6):hover .heading-permalink,
.prose :is(h1,h2,h3,h4,h5,h6):focus .heading-permalink {
@apply opacity-75 grayscale transition-opacity duration-100 ease-out;
}
.heading-permalink {
@apply opacity-0 ml-1 transition-opacity duration-300 ease-linear px-1 scroll-m-4;
}
.heading-permalink::before {
@apply content-['#'];
}
.heading-permalink:hover, .heading-permalink:focus {
@apply opacity-100 grayscale-0;
} The slight performance overhead is negligible for most sites, and the benefits of having full control over the heading structure outweigh the drawbacks. It also makes it easier to add features like custom icons, positioning options, and accessibility improvements in the future. The permalink-only component approach might seem simpler at first, but it introduces complexity in positioning and coordination with existing heading elements. The full component replacement gives us a clean slate to work with while maintaining all the functionality we need. |
61386bd
to
e1ccda8
Compare
packages/framework/resources/views/components/markdown-heading.blade.php
Show resolved
Hide resolved
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## new-asset-system #2047 +/- ##
====================================================
Coverage 100.00% 100.00%
- Complexity 1925 1931 +6
====================================================
Files 195 196 +1
Lines 5122 5138 +16
====================================================
+ Hits 5122 5138 +16 ☔ View full report in Codecov by Sentry. |
Edge case we need to fix: If there are multiple headings of the same name, we need offset. (Done) |
…links [2.x] Semantic Markdown heading permalinks
Was wondering if we should have a config option for the hash symbol. Until users request it I think I'm going to hold off on that. Claude's (via Cursor) explanation below is very in line with my thoughts. Based on the context and the nature of this customization, I recommend requiring users to publish the blade component rather than adding a configuration option. Here's why:
If someone wants to customize the permalink icon, they can:
This approach:
The documentation already shows this is the recommended approach for UI customizations, as seen in: ### Customizing the Blade templates
To make it really easy to customize asset loading, the styles and scripts are loaded in dedicated Blade components.
- Styles are loaded in `hyde::layouts.styles`
- Scripts are loaded in `hyde::layouts.scripts`
To customize them, run the following command:
```bash
php hyde publish:views layouts
```
Then edit the files found in `resources/views/vendor/hyde/layouts` directory of your project. |
4e378fd
to
8282ccd
Compare
8282ccd
to
3feddd3
Compare
b9ae134
to
9948afd
Compare
dcd2b03
to
c4e67ff
Compare
Just for absolute fun, not that I thought this would in any way be a performance sink, but the code that generates unique identifiers takes only |
cfac7e7
to
3df7aa2
Compare
Abstract
Changes
HeadingPermalinkExtension
, meaning user added custom options provided to that extension will be ignored. If you for some reason require that extension's custom features, either implement those to the Blade component after publishing it, or disable the Hyde permalink feature from the configuration and add the extension yourself.MarkdownService
withPermalinks
andcanEnablePermalinks
methods, see comment