ExamplesExtensionsSearch and Replace

Search and Replace

SearchAndReplaceExtension adds search, match navigation, and replacement to your editor. It is opt-in and is not registered in the default editor. UI is deliberately left to consumers: the only implemented UI is the Mantine component in this example, which you can copy or adapt to your preferences.

Try it out: Click Find or press Cmd/Ctrl+F inside the editor, search for fox, and expand Replace to change one match or all matches.

Usage

Install the extension when creating your editor, then use its methods to drive your own controls:

import { BlockNoteEditor } from "@blocknote/core";
import { SearchAndReplaceExtension } from "@blocknote/core/extensions";

const editor = BlockNoteEditor.create({
  extensions: [SearchAndReplaceExtension()],
});
const search = editor.getExtension(SearchAndReplaceExtension)!;

search.setQuery({ searchTerm: "fox" });
search.nextMatch();
search.replace("wolf");
search.replaceAll("wolf");
search.clearSearch();

For the example UI, copy SearchAndReplace.tsx and style.css, then wrap your BlockNoteView with <SearchAndReplace editor={editor}> as shown in App.tsx.

Relevant Docs:

import "@blocknote/core/fonts/inter.css";import { SearchAndReplaceExtension } from "@blocknote/core/extensions";import { BlockNoteView } from "@blocknote/mantine";import "@blocknote/mantine/style.css";import { useCreateBlockNote } from "@blocknote/react";import { SearchAndReplace } from "./SearchAndReplace";import "./style.css";export default function App() {  const editor = useCreateBlockNote({    extensions: [SearchAndReplaceExtension()],    initialContent: [      { type: "heading", content: "The fox field guide" },      {        type: "paragraph",        content: [          { type: "text", text: "A curious ", styles: {} },          { type: "text", text: "f", styles: { bold: true } },          { type: "text", text: "ox", styles: { italic: true } },          {            type: "text",            text: " crosses a formatting boundary. Find it just like any other fox.",            styles: {},          },        ],      },      {        type: "bulletListItem",        content: "Fox sightings",        children: [          { type: "bulletListItem", content: "A red fox near the river." },          { type: "bulletListItem", content: "An arctic FOX in the snow." },        ],      },      {        type: "table",        content: {          type: "tableContent",          rows: [            { cells: ["Animal", "Habitat"] },            { cells: ["Fennec fox", "Desert"] },            { cells: ["Gray fox", "Woodland"] },          ],        },      },      {        type: "paragraph",        content:          "Try Match case, use Enter / Shift+Enter to navigate, or replace fox with wolf. Undo works as usual.",      },    ],  });  return (    <SearchAndReplace editor={editor}>      <BlockNoteView editor={editor} className="search-example-editor" />    </SearchAndReplace>  );}