-
-
Notifications
You must be signed in to change notification settings - Fork 742
feat: math block #2857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthewlipski
wants to merge
13
commits into
main
Choose a base branch
from
code-block-previews
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: math block #2857
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e3f182a
Added `renderPreview` field to code block options and added LaTeX pre…
matthewlipski 6ccc955
Math block overhaul
matthewlipski ae49edc
Small fix
matthewlipski 9687742
Implemented minor CodeRabbit feedback
matthewlipski 9626d7e
Updated test snapshot
matthewlipski de7b1df
Big update to math block
matthewlipski 719fd7a
Merge branch 'main' into code-block-previews
matthewlipski ed8c681
Major changes
matthewlipski c8388cc
Fixed build issue
matthewlipski f2bfe62
Reverted `inert`
matthewlipski 57c459a
Implemented PR feedback
matthewlipski 3a1e4ca
Fixed test editor having unsupported default language for code block
matthewlipski ed8e6c1
Fixed export error
matthewlipski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "playground": true, | ||
| "docs": true, | ||
| "author": "matthewlipski", | ||
| "tags": [ | ||
| "Intermediate", | ||
| "Blocks", | ||
| "Custom Schemas", | ||
| "Suggestion Menus", | ||
| "Slash Menu" | ||
| ], | ||
| "dependencies": { | ||
| "@blocknote/code-block": "latest", | ||
| "@blocknote/math-block": "latest", | ||
| "react-icons": "^5.5.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Math Block | ||
|
|
||
| In this example, we register the `@blocknote/math-block` block in a custom schema. The math block renders LaTeX as MathML (using Temml) for the browser to display natively, and reveals an editable LaTeX source popup when selected. Exporting to HTML produces a MathML `<math>` element, and pasting MathML back in is converted to LaTeX. | ||
|
|
||
| **Try it out:** Click a formula to edit its LaTeX! | ||
|
|
||
| **Relevant Docs:** | ||
|
|
||
| - [Custom Blocks](/docs/features/custom-schemas/custom-blocks) | ||
| - [Editor Setup](/docs/getting-started/editor-setup) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <html lang="en"> | ||
|
matthewlipski marked this conversation as resolved.
|
||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Math Block</title> | ||
| <script> | ||
| <!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY --> | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="./main.tsx"></script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY | ||
| import React from "react"; | ||
| import { createRoot } from "react-dom/client"; | ||
| import App from "./src/App.jsx"; | ||
|
matthewlipski marked this conversation as resolved.
|
||
|
|
||
| const root = createRoot(document.getElementById("root")!); | ||
| root.render( | ||
| <React.StrictMode> | ||
| <App /> | ||
| </React.StrictMode>, | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| { | ||
| "name": "@blocknote/example-custom-schema-math-block", | ||
| "description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY", | ||
| "type": "module", | ||
| "private": true, | ||
| "version": "0.12.4", | ||
| "scripts": { | ||
| "start": "vp dev", | ||
| "dev": "vp dev", | ||
| "build:prod": "tsc && vp build", | ||
| "preview": "vp preview" | ||
| }, | ||
| "dependencies": { | ||
| "@blocknote/ariakit": "latest", | ||
| "@blocknote/core": "latest", | ||
| "@blocknote/mantine": "latest", | ||
| "@blocknote/react": "latest", | ||
| "@blocknote/shadcn": "latest", | ||
| "@mantine/core": "^9.0.2", | ||
| "@mantine/hooks": "^9.0.2", | ||
| "react": "^19.2.3", | ||
| "react-dom": "^19.2.3", | ||
| "@blocknote/code-block": "latest", | ||
| "@blocknote/math-block": "latest", | ||
| "react-icons": "^5.5.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/react": "^19.2.3", | ||
| "@types/react-dom": "^19.2.3", | ||
| "@vitejs/plugin-react": "^6.0.1", | ||
| "vite-plus": "catalog:" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import "@blocknote/core/fonts/inter.css"; | ||
| import { BlockNoteSchema } from "@blocknote/core"; | ||
| import { | ||
| filterSuggestionItems, | ||
| insertOrUpdateBlockForSlashMenu, | ||
| } from "@blocknote/core/extensions"; | ||
| import { createHighlighter } from "@blocknote/code-block"; | ||
| import { createMathBlockSpec } from "@blocknote/math-block"; | ||
| import { BlockNoteView } from "@blocknote/mantine"; | ||
| import "@blocknote/mantine/style.css"; | ||
| import { | ||
| getDefaultReactSlashMenuItems, | ||
| SuggestionMenuController, | ||
| useCreateBlockNote, | ||
| } from "@blocknote/react"; | ||
| import { TbMathFunction } from "react-icons/tb"; | ||
|
|
||
| // Our schema with block specs, which contain the configs and implementations for blocks | ||
| // that we want our editor to use. | ||
| const schema = BlockNoteSchema.create().extend({ | ||
| blockSpecs: { | ||
| // Creates an instance of the Math block and adds it to the schema. | ||
| math: createMathBlockSpec(), | ||
| }, | ||
| }); | ||
|
|
||
| // Slash menu item to insert a Math block. | ||
| const insertMath = (editor: typeof schema.BlockNoteEditor) => ({ | ||
| title: "Math", | ||
| subtext: "Insert a LaTeX math formula", | ||
| onItemClick: () => | ||
| insertOrUpdateBlockForSlashMenu(editor, { | ||
| type: "math", | ||
| }), | ||
| aliases: ["math", "latex", "formula", "equation"], | ||
| group: "Basic blocks", | ||
| icon: <TbMathFunction />, | ||
| }); | ||
|
|
||
| export default function App() { | ||
| const editor = useCreateBlockNote({ | ||
| // Configures the syntax highlighting extension to always use LaTeX syntax highlighting in the | ||
| // Math block. | ||
| syntaxHighlighting: { | ||
| createHighlighter, | ||
| highlightBlock: (block) => | ||
| block.type === "math" ? "latex" : block.props.language, | ||
| }, | ||
| schema, | ||
| initialContent: [ | ||
| { | ||
| type: "paragraph", | ||
| content: "Click a formula to edit its LaTeX source:", | ||
| }, | ||
| { | ||
| type: "math", | ||
| content: "a^2 = \\sqrt{b^2 + c^2}", | ||
| }, | ||
| { | ||
| type: "math", | ||
| content: "\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}", | ||
| }, | ||
| { | ||
| type: "paragraph", | ||
| content: "Press the '/' key to open the Slash Menu and add another", | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // Renders the editor instance using a React component. | ||
| return ( | ||
| <BlockNoteView editor={editor} slashMenu={false}> | ||
| {/* Replaces the default Slash Menu. */} | ||
| <SuggestionMenuController | ||
| triggerCharacter={"/"} | ||
| getItems={async (query) => { | ||
| // Gets all default slash menu items. | ||
| const defaultItems = getDefaultReactSlashMenuItems(editor); | ||
| // Finds index of last item in "Basic blocks" group. | ||
| const lastBasicBlockIndex = defaultItems.findLastIndex( | ||
| (item) => item.group === "Basic blocks", | ||
| ); | ||
| // Inserts the Math item as the last item in the "Basic blocks" group. | ||
| defaultItems.splice(lastBasicBlockIndex + 1, 0, insertMath(editor)); | ||
|
|
||
| // Returns filtered items based on the query. | ||
| return filterSuggestionItems(defaultItems, query); | ||
| }} | ||
| /> | ||
| </BlockNoteView> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "__comment": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY", | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "useDefineForClassFields": true, | ||
| "lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
| "allowJs": false, | ||
| "skipLibCheck": true, | ||
| "allowSyntheticDefaultImports": true, | ||
| "strict": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "noEmit": true, | ||
| "jsx": "react-jsx", | ||
| "composite": true | ||
| }, | ||
| "include": ["."], | ||
| "__ADD_FOR_LOCAL_DEV_references": [ | ||
| { | ||
| "path": "../../../packages/core/" | ||
| }, | ||
| { | ||
| "path": "../../../packages/react/" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /// <reference types="vite-plus/client" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY | ||
| import react from "@vitejs/plugin-react"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import { defineConfig } from "vite-plus"; | ||
| // https://vitejs.dev/config/ | ||
| export default defineConfig(((conf: { command: string }) => ({ | ||
| plugins: [react()], | ||
| optimizeDeps: {}, | ||
| build: { | ||
| sourcemap: true, | ||
| }, | ||
| resolve: { | ||
| alias: | ||
| conf.command === "build" || | ||
| !fs.existsSync(path.resolve(__dirname, "../../packages/core/src")) | ||
| ? {} | ||
| : ({ | ||
| // Comment out the lines below to load a built version of blocknote | ||
| // or, keep as is to load live from sources with live reload working | ||
| "@blocknote/core": path.resolve( | ||
| __dirname, | ||
| "../../packages/core/src/", | ||
| ), | ||
| "@blocknote/react": path.resolve( | ||
| __dirname, | ||
| "../../packages/react/src/", | ||
|
matthewlipski marked this conversation as resolved.
|
||
| ), | ||
| } as any), | ||
| }, | ||
| })) as Parameters<typeof defineConfig>[0]); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious if we can find a better way to do this. This will make it difficult to create easily installable block-plugins later I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think syntaxHighlighting should be a top-level option like this. But as a single extension, maybe yea.
What's annoying is that it wants to be a singleton, but multiple places need to configure it. I'll think on it.