Skip to content

Search and replace

The search plugin finds and replaces text in the active editor. It has no built-in UI: build one with useEditorSearch() in a component inside the editor, such as in the toolbar.

<script lang="ts">
import { useEditorSearch } from '@the-sumeet/sveltedown';
const search = useEditorSearch();
</script>
<input value={search.search} oninput={(event) => search.setSearch(event.currentTarget.value)} />
<span>{search.cursor} / {search.total}</span>
<button onclick={search.prev}>Previous</button>
<button onclick={search.next}>Next</button>
<button onclick={() => search.replaceAll('replacement')}>Replace all</button>

Terms are case-insensitive regular expressions. replace and replaceAll insert the replacement literally, and replaceAll is a single undo step. The search must be open (openSearch()) to find matches. Its properties, like total and currentRange, are reactive.

Matches are highlighted with the CSS Custom Highlight API. Style them with ::highlight(MdxSearch) and ::highlight(MdxFocusSearch).