11import "@blocknote/core/fonts/inter.css" ;
22import { BlockNoteSchema } from "@blocknote/core" ;
3+ import {
4+ filterSuggestionItems ,
5+ insertOrUpdateBlockForSlashMenu ,
6+ } from "@blocknote/core/extensions" ;
37import { createHighlighter } from "@blocknote/code-block" ;
48import { createMathBlockSpec } from "@blocknote/math-block" ;
59import { BlockNoteView } from "@blocknote/mantine" ;
610import "@blocknote/mantine/style.css" ;
7- import { useCreateBlockNote } from "@blocknote/react" ;
11+ import {
12+ getDefaultReactSlashMenuItems ,
13+ SuggestionMenuController ,
14+ useCreateBlockNote ,
15+ } from "@blocknote/react" ;
16+ import { TbMathFunction } from "react-icons/tb" ;
17+
18+ // Our schema with block specs, which contain the configs and implementations for blocks
19+ // that we want our editor to use.
20+ const schema = BlockNoteSchema . create ( ) . extend ( {
21+ blockSpecs : {
22+ // Creates an instance of the Math block and adds it to the schema.
23+ math : createMathBlockSpec ( ) ,
24+ } ,
25+ } ) ;
26+
27+ // Slash menu item to insert a Math block.
28+ const insertMath = ( editor : typeof schema . BlockNoteEditor ) => ( {
29+ title : "Math" ,
30+ subtext : "Insert a LaTeX math formula" ,
31+ onItemClick : ( ) =>
32+ insertOrUpdateBlockForSlashMenu ( editor , {
33+ type : "math" ,
34+ } ) ,
35+ aliases : [ "math" , "latex" , "formula" , "equation" ] ,
36+ group : "Basic blocks" ,
37+ icon : < TbMathFunction /> ,
38+ } ) ;
839
940export default function App ( ) {
10- // The math block isn't a default block, so we register it in a custom schema.
1141 const editor = useCreateBlockNote ( {
12- // The Shiki highlighter (from @blocknote/code-block) syntax-highlights the
13- // math block's editable LaTeX source popup. `highlightBlock` enables it for
14- // the math block and highlights it as LaTeX.
42+ // Configures the syntax highlighting extension to always use LaTeX syntax highlighting in the
43+ // Math block.
1544 syntaxHighlighting : {
1645 createHighlighter,
1746 highlightBlock : ( block ) =>
1847 block . type === "math" ? "latex" : block . props . language ,
1948 } ,
20- schema : BlockNoteSchema . create ( ) . extend ( {
21- blockSpecs : {
22- math : createMathBlockSpec ( ) ,
23- } ,
24- } ) ,
49+ schema,
2550 initialContent : [
2651 {
2752 type : "paragraph" ,
@@ -37,10 +62,31 @@ export default function App() {
3762 } ,
3863 {
3964 type : "paragraph" ,
65+ content : "Press the '/' key to open the Slash Menu and add another" ,
4066 } ,
4167 ] ,
4268 } ) ;
4369
4470 // Renders the editor instance using a React component.
45- return < BlockNoteView editor = { editor } /> ;
71+ return (
72+ < BlockNoteView editor = { editor } slashMenu = { false } >
73+ { /* Replaces the default Slash Menu. */ }
74+ < SuggestionMenuController
75+ triggerCharacter = { "/" }
76+ getItems = { async ( query ) => {
77+ // Gets all default slash menu items.
78+ const defaultItems = getDefaultReactSlashMenuItems ( editor ) ;
79+ // Finds index of last item in "Basic blocks" group.
80+ const lastBasicBlockIndex = defaultItems . findLastIndex (
81+ ( item ) => item . group === "Basic blocks" ,
82+ ) ;
83+ // Inserts the Math item as the last item in the "Basic blocks" group.
84+ defaultItems . splice ( lastBasicBlockIndex + 1 , 0 , insertMath ( editor ) ) ;
85+
86+ // Returns filtered items based on the query.
87+ return filterSuggestionItems ( defaultItems , query ) ;
88+ } }
89+ />
90+ </ BlockNoteView >
91+ ) ;
4692}
0 commit comments