Repository navigation
[COMMS-1057] Enable math formula support in BlockNote on documents #26049
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
ihordubas99
wants to merge
2
commits into
dev
from
feature/comms-1057-enable-math-formula-support-in-blocknote-on-documents
+417
−14
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,63 @@ | ||
| //-- copyright | ||
| // OpenProject is an open source project management software. | ||
| // Copyright (C) the OpenProject GmbH | ||
| // | ||
| // This program is free software; you can redistribute it and/or | ||
| // modify it under the terms of the GNU General Public License version 3. | ||
| // | ||
| // OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
| // Copyright (C) 2006-2013 Jean-Philippe Lang | ||
| // Copyright (C) 2010-2013 the ChiliProject Team | ||
| // | ||
| // This program is free software; you can redistribute it and/or | ||
| // modify it under the terms of the GNU General Public License | ||
| // as published by the Free Software Foundation; either version 2 | ||
| // of the License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| // | ||
| // See COPYRIGHT and LICENSE files for more details. | ||
| //++ | ||
|
|
||
| import { createBlockSpec, createInlineContentSpec, plainContentToString } from "@blocknote/core"; | ||
|
|
||
| // `@blocknote/math-block` cannot be loaded in Node (it imports KaTeX's CSS), and with React's development | ||
| // build its rendering crashes the process after an export, so these mirror its specs. The math tests | ||
| // guard that they stay in sync with the package. The markdown exporter reads the LaTeX from the | ||
| // `application/x-tex` annotation. | ||
| function mathElement(latex:string, display:"block" | "inline") { | ||
| const namespace = "http://www.w3.org/1998/Math/MathML"; | ||
| const math = document.createElementNS(namespace, "math"); | ||
| const semantics = document.createElementNS(namespace, "semantics"); | ||
| const annotation = document.createElementNS(namespace, "annotation"); | ||
|
|
||
| math.setAttribute("display", display); | ||
| annotation.setAttribute("encoding", "application/x-tex"); | ||
| annotation.textContent = latex; | ||
| semantics.append(annotation); | ||
| math.append(semantics); | ||
|
|
||
| return math as unknown as HTMLElement; | ||
| } | ||
|
|
||
| export const mathBlockStaticSpec = createBlockSpec( | ||
| { type: "mathBlock", propSchema: {}, content: "plain" }, | ||
| { | ||
| meta: { code: true, defining: true, isolating: false }, | ||
| render: (block) => ({ dom: mathElement(plainContentToString(block.content), "block") }), | ||
| }, | ||
| ); | ||
|
|
||
| export const mathInlineStaticSpec = createInlineContentSpec( | ||
| { type: "math", propSchema: {}, content: "plain" }, | ||
| { | ||
| meta: { code: true }, | ||
| render: (inlineContent) => ({ dom: mathElement(plainContentToString(inlineContent.content), "inline") }), | ||
| }, | ||
| ); | ||
79 changes: 79 additions & 0 deletions
79
extensions/op-blocknote-hocuspocus/test/extensions/math.test.ts
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,79 @@ | ||
| //-- copyright | ||
| // OpenProject is an open source project management software. | ||
| // Copyright (C) the OpenProject GmbH | ||
| // | ||
| // This program is free software; you can redistribute it and/or | ||
| // modify it under the terms of the GNU General Public License version 3. | ||
| // | ||
| // OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
| // Copyright (C) 2006-2013 Jean-Philippe Lang | ||
| // Copyright (C) 2010-2013 the ChiliProject Team | ||
| // | ||
| // This program is free software; you can redistribute it and/or | ||
| // modify it under the terms of the GNU General Public License | ||
| // as published by the Free Software Foundation; either version 2 | ||
| // of the License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| // | ||
| // See COPYRIGHT and LICENSE files for more details. | ||
| //++ | ||
|
|
||
| import { | ||
| createMathBlockConfig, | ||
| createReactInlineMathSpec, | ||
| createReactMathBlockSpec, | ||
| mathInlineContentConfig, | ||
| } from "@blocknote/math-block"; | ||
| import { describe, expect, test } from "vitest"; | ||
| import * as Y from "yjs"; | ||
| import { createEditor } from "../../src/extensions/openProjectApi"; | ||
| import { mathBlockStaticSpec, mathInlineStaticSpec } from "../../src/mathSpecs"; | ||
|
|
||
| async function storedMarkdown(blocks:unknown[]) { | ||
| const editor = createEditor(); | ||
| const fragment = new Y.Doc().getXmlFragment("document-store"); | ||
| // @ts-expect-error BlockNote types are complicated | ||
| editor.blocksToYXmlFragment(blocks, fragment); | ||
| return editor.blocksToMarkdownLossy(editor.yXmlFragmentToBlocks(fragment)); | ||
| } | ||
|
|
||
| describe("math", () => { | ||
| test("stores a math block as a $$ block", async () => { | ||
| const markdown = await storedMarkdown([ | ||
| { type: "mathBlock", content: "a^2 = \\sqrt{b^2 + c^2}" }, | ||
| ]); | ||
|
|
||
| expect(markdown).toBe("$$\na^2 = \\sqrt{b^2 + c^2}\n$$\n"); | ||
| }); | ||
|
|
||
| test("stores inline math as a $ span", async () => { | ||
| const markdown = await storedMarkdown([ | ||
| { type: "paragraph", content: ["Euler: ", { type: "math", content: "e^{i\\pi} + 1 = 0" }, "."] }, | ||
| ]); | ||
|
|
||
| expect(markdown).toBe("Euler: $e^{i\\pi} + 1 = 0$.\n"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("math specs", () => { | ||
| test("match the math block of @blocknote/math-block", () => { | ||
| const { code, defining, isolating } = createReactMathBlockSpec().implementation.meta ?? {}; | ||
|
|
||
| expect(mathBlockStaticSpec().config).toEqual(createMathBlockConfig()); | ||
| expect(mathBlockStaticSpec().implementation.meta).toEqual({ code, defining, isolating }); | ||
| }); | ||
|
|
||
| test("match the inline math of @blocknote/math-block", () => { | ||
| const { code } = createReactInlineMathSpec().implementation.meta ?? {}; | ||
|
|
||
| expect(mathInlineStaticSpec.config).toEqual(mathInlineContentConfig); | ||
| expect(mathInlineStaticSpec.implementation.meta).toEqual({ code }); | ||
| }); | ||
| }); |
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
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.
Please add a drift guard to the hocuspocus tests:
To ensure that we don't run into inconsistencies on future updates of BlockNote.
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.
Done.
@blocknote/math-blockis now a hocuspocus devDependency, and the math tests check that the static specs match its configs andcode/defining/isolatingmeta. They fail as soon as the specs differ. I'll add the same guard to the diagrams PR (#26157).