add MDX heading permalinks for tutorial pages#1403
Conversation
| @@ -1,4 +1,4 @@ | |||
| @import "/styles/variables.scss"; | |||
| @use "/styles/variables" as *; | |||
There was a problem hiding this comment.
Was this to fix anything in particular? Ideally this change should not need to affect how we import other files, or update the package lock.
| type HeadingProps = HTMLAttributes<HTMLHeadingElement> & { | ||
| as: HeadingTag; | ||
| children?: ComponentChildren; | ||
| id?: string; |
There was a problem hiding this comment.
For my understanding, what passes this in? Since it seems to be implicit, we may want to add a comment to explain it.
There was a problem hiding this comment.
MDX passes these props automatically when rendering headings. Astro already injects things like id, children, etc. into the heading components, so this just forwards them into our custom component.
| aria-label="Link to this section" | ||
| > | ||
| <span class="heading-permalink__text">{children}</span> | ||
| <span class="heading-permalink__icon">#</span> |
There was a problem hiding this comment.
Not huge, but possibly this could be done with just one <a> tag as before without <span> children if we add the # via CSS? something like
.heading-permalink::after: {
content: '#';
display: inline;
/* ...etc */
}There was a problem hiding this comment.
yeah I thought about that, but since we’re making the whole heading a link, if the heading text contains inline links it could create nested anchors. that’s why I split it out earlier.
but yeah using ::after for # still makes sense.
| img: FreeRatioImage, | ||
| pre: PreProxy, | ||
| a: LinkWrapper, | ||
| h1: HeadingPermalinkH1, |
There was a problem hiding this comment.
While the tutorial layout is the main thing I had in mind when raising the issue, are there other pages that would benefit from this too? e.g. probably pages with the contributor docs should also have it. Put another way, should this be the default, maybe with other pages opting out if need be? @ksen0 let me know if you have thoughts there!
There was a problem hiding this comment.
I scoped it to tutorials first since that’s what the issue mentioned, but this could definitely be useful for other MDX pages like contributor docs too.
I wasn’t sure if it should be global by default or opt-in per layout, so I kept it limited for now. happy to extend it or move it to a shared layout if that’s preferred.
|
Thanks for working on this, your screen recording looks like what I was imagining for this feature! Left some comments, let me know what you think. |
|
Thanks for working on this! Please link the issue in the PR description |
Summary
Adds MDX heading permalinks for tutorial pages using component overrides in
TutorialLayout.astro.Closes #1398
Changes
HeadingPermalinkcomponent (Preact)h1–h6in tutorial MDX rendering only#fragment)#indicator appears on hover/focus (left side, subtle color).tutorials .rendered-markdownDemo
Short demo showing heading hover, permalink visibility, and fragment navigation:
https://github.com/user-attachments/assets/ab1f6c54-a9a5-423d-860e-360aec349cd9
Testing
npm run checkpasses (no errors)npm run buildsucceeds#indicator#fragmentnavigation scrolls correctlyNotes