Skip to content

Markdown preview visible find matches - #62280

Merged
SomeoneToIgnore merged 4 commits into
zed-industries:mainfrom
WaryaWayne:markdown-preview-visible-find-matches
Aug 19, 2026
Merged

SomeoneToIgnore merged 4 commits into
zed-industries:mainfrom
WaryaWayne:markdown-preview-visible-find-matches

Conversation

@WaryaWayne

@WaryaWayne WaryaWayne commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Objective

Search in the markdown preview matches against the raw markdown source, including text that never renders. Searching a README for a common word reports more matches than are visible: the count says 2 when only 1 is on screen, and navigating to the phantom match appears to do nothing. Badge-heavy READMEs make this worse, since every badge URL contributes searchable words (github, build, main, svg) that the reader never sees.

Solution

Track which source ranges produce no on-screen text and filter matches against them:

  • Link destinations ([label](url), where only label renders)
  • Images, including alt text and destination, since an image renders as an image
  • Link reference definitions, which pulldown-cmark consumes and never emits events for

Ranges are returned sorted and disjoint so the preview can binary search them. Search results also derive from the parsed markdown, which lags the source during a background parse, so MatchesInvalidated now fires once the parse lands.

Testing

cargo test -p markdown -p markdown_preview. Eight new tests cover link destinations, reference definitions, standalone images, linked images, images between link text, parse-in-flight, and match-order independence.

Note: follow_preview_serialized_path_updates_when_followed_editor_changes fails on main independently of this change (sqlite FOREIGN KEY constraint failed in save_preview, passes in isolation).

Self-Review Checklist:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content adheres to Zed's UI standards (UX/UI and icon guidelines)
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Showcase

Markdown preview search on the same document, installed Zed versus this branch:

Before:
warya_wayne_zed_fix_miscounted_search_in_Markdown_preview_current

After:
warya_wayne_zed_fix_miscounted_search_in_Markdown_preview_new


Release Notes:

  • Fixed markdown preview search matching text that isn't rendered, such as link and image destinations

Extend the non-rendered source ranges beyond link destinations so search
matches only text the reader can actually see:

- Images render as an image, so neither the alt text nor the destination
  is on screen. Skipping their events also keeps alt text from carving a
  hole out of an enclosing link's non-rendered span.
- Link reference definitions are consumed by the parser and never appear
  in any event range, so collect their spans from pulldown-cmark.

Search results are derived from the parsed markdown, which lags the
source while a background parse is in flight. Track that and emit
MatchesInvalidated once it lands, so stale matches are not left behind.

The returned ranges are now sorted and disjoint, which lets the preview
binary search them instead of carrying a cursor across matches. That
drops an undocumented dependency on SearchQuery::search_str returning
matches in ascending order.
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Aug 6, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b579dd120

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +258 to +260
.reference_definitions()
.iter()
.map(|(_, definition)| definition.span.clone())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve every duplicate reference-definition span

When a document contains multiple definitions with the same normalized label (for example, two [foo]: ... lines, or labels differing only by case), reference_definitions() exposes the selected definitions keyed by label, so iterating this map retains only one span. CommonMark still consumes every duplicate definition without rendering it, leaving the omitted definitions searchable as phantom matches. Collect the consumed definition spans independently of the deduplicated lookup map.

Useful? React with 👍 / 👎.

@smitbarmase smitbarmase added the area:preview/markdown Feedback for Zed's Markdown preview label Aug 7, 2026
@SomeoneToIgnore SomeoneToIgnore self-assigned this Aug 19, 2026

@SomeoneToIgnore SomeoneToIgnore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@SomeoneToIgnore

Copy link
Copy Markdown
Contributor

/autofix

@SomeoneToIgnore
SomeoneToIgnore added this pull request to the merge queue Aug 19, 2026
Merged via the queue into zed-industries:main with commit 9bb4787 Aug 19, 2026
36 checks passed
@WaryaWayne
WaryaWayne deleted the markdown-preview-visible-find-matches branch August 19, 2026 22:08
playdohface pushed a commit to playdohface/zed that referenced this pull request Aug 29, 2026
# Objective

Search in the markdown preview matches against the raw markdown source,
including text that never renders. Searching a README for a common word
reports more matches than are visible: the count says 2 when only 1 is
on screen, and navigating to the phantom match appears to do nothing.
Badge-heavy READMEs make this worse, since every badge URL contributes
searchable words (`github`, `build`, `main`, `svg`) that the reader
never sees.

## Solution

Track which source ranges produce no on-screen text and filter matches
against them:

- Link destinations (`[label](url)`, where only `label` renders)
- Images, including alt text and destination, since an image renders as
an image
- Link reference definitions, which `pulldown-cmark` consumes and never
emits events for

Ranges are returned sorted and disjoint so the preview can binary search
them. Search results also derive from the parsed markdown, which lags
the source during a background parse, so `MatchesInvalidated` now fires
once the parse lands.

## Testing

`cargo test -p markdown -p markdown_preview`. Eight new tests cover link
destinations, reference definitions, standalone images, linked images,
images between link text, parse-in-flight, and match-order independence.

Note:
`follow_preview_serialized_path_updates_when_followed_editor_changes`
fails on `main` independently of this change (sqlite `FOREIGN KEY
constraint failed` in `save_preview`, passes in isolation).

## 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 adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

## Showcase

Markdown preview search on the same document, installed Zed versus this
branch:

Before:
<img width="1090" height="1235"
alt="warya_wayne_zed_fix_miscounted_search_in_Markdown_preview_current"
src="https://github.com/user-attachments/assets/d2e59e12-b43e-4fea-ac9c-12631845653b"
/>

After:
<img width="1090" height="1235"
alt="warya_wayne_zed_fix_miscounted_search_in_Markdown_preview_new"
src="https://github.com/user-attachments/assets/9f681fa4-f141-41c9-af9f-675cf3a2f0bd"
/>

---

Release Notes:

- Fixed markdown preview search matching text that isn't rendered, such
as link and image destinations

---------

Co-authored-by: zed-zippy[bot] <234243425+zed-zippy[bot]@users.noreply.github.com>
sergiooroman pushed a commit to sergiooroman/zed that referenced this pull request Sep 8, 2026
# Objective

Search in the markdown preview matches against the raw markdown source,
including text that never renders. Searching a README for a common word
reports more matches than are visible: the count says 2 when only 1 is
on screen, and navigating to the phantom match appears to do nothing.
Badge-heavy READMEs make this worse, since every badge URL contributes
searchable words (`github`, `build`, `main`, `svg`) that the reader
never sees.

## Solution

Track which source ranges produce no on-screen text and filter matches
against them:

- Link destinations (`[label](url)`, where only `label` renders)
- Images, including alt text and destination, since an image renders as
an image
- Link reference definitions, which `pulldown-cmark` consumes and never
emits events for

Ranges are returned sorted and disjoint so the preview can binary search
them. Search results also derive from the parsed markdown, which lags
the source during a background parse, so `MatchesInvalidated` now fires
once the parse lands.

## Testing

`cargo test -p markdown -p markdown_preview`. Eight new tests cover link
destinations, reference definitions, standalone images, linked images,
images between link text, parse-in-flight, and match-order independence.

Note:
`follow_preview_serialized_path_updates_when_followed_editor_changes`
fails on `main` independently of this change (sqlite `FOREIGN KEY
constraint failed` in `save_preview`, passes in isolation).

## 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 adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

## Showcase

Markdown preview search on the same document, installed Zed versus this
branch:

Before:
<img width="1090" height="1235"
alt="warya_wayne_zed_fix_miscounted_search_in_Markdown_preview_current"
src="https://github.com/user-attachments/assets/d2e59e12-b43e-4fea-ac9c-12631845653b"
/>

After:
<img width="1090" height="1235"
alt="warya_wayne_zed_fix_miscounted_search_in_Markdown_preview_new"
src="https://github.com/user-attachments/assets/9f681fa4-f141-41c9-af9f-675cf3a2f0bd"
/>

---

Release Notes:

- Fixed markdown preview search matching text that isn't rendered, such
as link and image destinations

---------

Co-authored-by: zed-zippy[bot] <234243425+zed-zippy[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:preview/markdown Feedback for Zed's Markdown preview cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants