markdown_preview: Support anchor link for headings#53184
markdown_preview: Support anchor link for headings#53184smitbarmase merged 17 commits intozed-industries:mainfrom
Conversation
smitbarmase
left a comment
There was a problem hiding this comment.
Thanks, I added a few suggestions.
Move generate_heading_slug(), slug deduplication, and heading slug storage from MarkdownElementBuilder (render-time) to the parser (parse-time). Slugs are now stored as HashMap<SharedString, usize> on ParsedMarkdownData for O(1) lookup instead of Vec with O(N) scan.
When clicking a cross-file anchor link like [text](./file.md#heading), the editor cursor now moves to the heading position in addition to the preview scrolling. Uses a pending slug that resolves once the target file's parse completes.
Keep both pending_heading_scroll (ours) and search_highlights (upstream).
|
Thanks, taking a look. |
smitbarmase
left a comment
There was a problem hiding this comment.
Thanks, I made a few fixes on top of your commits before merging:
Have you tested it? I don't think the cross-file anchor scroll (./file.md#heading) was ever working. It was trying to scroll the current preview while opening a target editor, so the heading would never resolve. There was a bunch of dead code around this that just added complexity, so I cleaned it up. We can introduce cross-file scroll later.
I also removed the scroll logic from render. We should not handle state mutations there, it's unreliable and wrong. Render should purely be used for rendering. I moved it to a window.defer call in handle_url_click instead, which is a more deterministic way to handle it.
For separation of concern, I removed the # fragment handling from MarkdownElement. Any URL can have a # in it, so stripping it and treating it as an anchor at the shared markdown level is a bug. I fixed that handling and moved into markdown preview itself.
We now also do parsing for heading slug only for markdown preview.
Thanks again for working on this!
) ## What does this PR did - Generate [GitHub-flavored heading slugs](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#section-links) for markdown headings - Handle `[label](#heading)` same-document anchor links that scroll the preview and editor to the target heading - Handle `[label](./file.md#heading)` cross-file anchor links that open the file, scroll the preview, and move the editor cursor to the heading https://github.com/user-attachments/assets/ecc468bf-bed0-4543-a988-703025a61bf8 ## What to test - [ ] Create a markdown file with `[Go to section](#section-name)` links, verify clicking scrolls preview and editor - [ ] Create two markdown files with cross-file links like `[See other](./other.md#heading)`, verify file opens and preview scrolls to heading - [ ] Verify duplicate headings produce correct slugs (`heading`, `heading-1`) - [ ] Verify external URLs (`https://...`) are unaffected Self-Review Checklist: - [x] I've reviewed my own diff for quality, security, and reliability - [ ] Unsafe blocks (if any) have justifying comments - [x] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [ ] Performance impact has been considered and is acceptable Closes zed-industries#18699 Release Notes: - Added support for anchor links for headings in Markdown Preview. --------- Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
|
Thanks @dongdong867 |
What does this PR did
[label](#heading)same-document anchor links that scroll the preview and editor to the target heading[label](./file.md#heading)cross-file anchor links that open the file, scroll the preview, and move the editor cursor to the headingmarkdown_heading_linking.mov
What to test
[Go to section](#section-name)links, verify clicking scrolls preview and editor[See other](./other.md#heading), verify file opens and preview scrolls to headingheading,heading-1)https://...) are unaffectedSelf-Review Checklist:
Closes #18699
Release Notes: