feat(markdown): add copiable anchor links to headings#167
feat(markdown): add copiable anchor links to headings#167adrianschmidt-bot wants to merge 3 commits intojgroth:mainfrom
Conversation
Adds clickable anchor links (#) that appear on hover next to all headings (h1-h6) in markdown content. Clicking the link: - Updates the URL with the anchor - Copies the full URL to clipboard This makes it easy to share direct links to specific sections in documentation. Closes jgroth#145
📝 WalkthroughWalkthroughAdds copiable anchor links to rendered markdown headings (h1–h6). After render the component inserts anchor elements for headings with IDs; clicking an anchor updates history with a route-aware hash and attempts to copy the full URL to the clipboard. Styling reveals anchors on heading hover/focus. Changes
Sequence DiagramsequenceDiagram
participant User
participant Component as Markdown Component
participant DOM
participant URLState as URL/History
participant Clipboard
Component->>DOM: render markdown HTML
Component->>Component: addAnchorLinks() — scan headings, append anchors
User->>DOM: click anchor link
DOM->>Component: handleAnchorClick(event, id)
Component->>Component: getAnchorHref(id)
Component->>URLState: history.pushState(...) with route-aware `#id`
Component->>Clipboard: try navigator.clipboard.writeText(fullUrl)
URLState->>User: browser URL updates (hash)
Clipboard->>User: copy success/failure (best-effort)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/components/markdown/markdown.tsx`:
- Around line 106-118: The handleAnchorClick function builds fullUrl without
window.location.search and calls navigator.clipboard.writeText without
feature-detection; update handleAnchorClick (and its use of getAnchorHref) to
append window.location.search when constructing the fullUrl (and when updating
history if you want the URL in the address bar to keep query params), and guard
clipboard access by checking if navigator.clipboard and
navigator.clipboard.writeText exist before calling them (fall back to no-op or a
safe alternative if unavailable) so that no exception is thrown before the
.catch() can run.
preserve query params and guard clipboard access
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/components/markdown/markdown.tsx`:
- Around line 99-104: getAnchorHref currently builds fragments like "##id" when
getRoute() returns an empty string; update the getAnchorHref(id: string)
implementation to check the route (obtained via getRoute()) and if
routeWithoutAnchor is empty return `#${id}`, otherwise return
`#${routeWithoutAnchor}#${id}` so you don't produce double-hash fragments;
modify the getAnchorHref method accordingly.
avoid ##id when route is empty
Summary
Adds clickable anchor links that appear on hover next to all headings (h1-h6) in markdown content.
Behavior
When hovering over a heading with an ID, a
#link appears. Clicking it:This makes it easy to share direct links to specific sections in documentation.
Implementation
rehype-slugplugin (from PR Add support for cross-page anchor links #160) which already adds IDs to headingsaddAnchorLinks()appends anchor elements to each headingStyling
--kompendium-color-turquoise)Closes #145
Summary by CodeRabbit