# Markdown preview visible find matches \(\#62280\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/9bb4787949ee555142487f75f73c73379e520d1b)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 9bb4787949ee555142487f75f73c73379e520d1b

Requested commit: 9bb4787949ee555142487f75f73c73379e520d1b

Commit: 9bb4787949ee555142487f75f73c73379e520d1b

Tree: e2a29da20723edc28d37dc8757275bc219d92f14

Author: Warya Wayne

Committer: GitHub

## Message

```
Markdown preview visible find matches (#62280)

# 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>
```

## Parents

- [fa00dccc42311f8dc71c533105488b0dbd518138](https://git.cafe/gitcafe/zed/commit/fa00dccc42311f8dc71c533105488b0dbd518138?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/9bb4787949ee555142487f75f73c73379e520d1b?format=markdown)
