-
-
Notifications
You must be signed in to change notification settings - Fork 751
fix: Plain content issues #2896
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
5
commits into
code-block-previews
Choose a base branch
from
plain-content-fixes
base: code-block-previews
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f596d8a
Fixed plain content issues
matthewlipski 9b1ca65
Added tests & additional fixes
matthewlipski 68c4177
Merge branch 'code-block-previews' into plain-content-fixes
matthewlipski d2b6e1b
Merge branch 'code-block-previews' into plain-content-fixes
matthewlipski b88c124
Updated tests
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
151 changes: 151 additions & 0 deletions
151
packages/core/src/schema/inlineContent/createSpec.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,151 @@ | ||
| import { DOMParser as PMDOMParser } from "@tiptap/pm/model"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { BlockNoteSchema } from "../../blocks/BlockNoteSchema.js"; | ||
| import { | ||
| defaultBlockSpecs, | ||
| defaultInlineContentSpecs, | ||
| } from "../../blocks/defaultBlocks.js"; | ||
| import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; | ||
| import { createInlineContentSpec } from "./createSpec.js"; | ||
|
|
||
| // A minimal "plain" inline content, matched from external HTML by a `parse` | ||
| // function (the `tag: "*"` parse rule). | ||
| const customPlainIC = createInlineContentSpec( | ||
| { | ||
| type: "customPlainIC", | ||
| propSchema: {}, | ||
| content: "plain", | ||
| }, | ||
| { | ||
| parse: (el) => (el.classList?.contains("custom-plain-ic") ? {} : undefined), | ||
| render: () => { | ||
| const dom = document.createElement("span"); | ||
| const contentDOM = document.createElement("span"); | ||
| dom.append(contentDOM); | ||
| return { dom, contentDOM }; | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| const createEditor = () => | ||
| BlockNoteEditor.create({ | ||
| schema: BlockNoteSchema.create({ | ||
| blockSpecs: defaultBlockSpecs, | ||
| inlineContentSpecs: { | ||
| ...defaultInlineContentSpecs, | ||
| customPlainIC, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| describe("plain inline content", () => { | ||
| it("holds its text as a string in the block model", () => { | ||
| const editor = createEditor(); | ||
|
|
||
| editor.replaceBlocks(editor.document, [ | ||
| { | ||
| type: "paragraph", | ||
| content: [ | ||
| { | ||
| type: "customPlainIC", | ||
| props: {}, | ||
| content: "hello world", | ||
| } as any, | ||
| ], | ||
| }, | ||
| ]); | ||
|
|
||
| const inlineContent = (editor.document[0] as any).content[0]; | ||
| expect(inlineContent.type).toBe("customPlainIC"); | ||
| expect(inlineContent.content).toBe("hello world"); | ||
|
|
||
| editor._tiptapEditor.destroy(); | ||
| }); | ||
|
|
||
| it("round-trips a newline in its string content", () => { | ||
| const editor = createEditor(); | ||
|
|
||
| // Plain content holds raw text, so newlines are kept as characters rather | ||
| // than being split into (disallowed) `hardBreak` nodes. | ||
| editor.replaceBlocks(editor.document, [ | ||
| { | ||
| type: "paragraph", | ||
| content: [ | ||
| { | ||
| type: "customPlainIC", | ||
| props: {}, | ||
| content: "first\nsecond", | ||
| } as any, | ||
| ], | ||
| }, | ||
| ]); | ||
|
|
||
| const inlineContent = (editor.document[0] as any).content[0]; | ||
| expect(inlineContent.type).toBe("customPlainIC"); | ||
| expect(inlineContent.content).toBe("first\nsecond"); | ||
|
|
||
| editor._tiptapEditor.destroy(); | ||
| }); | ||
|
|
||
| it("keeps text and drops formatting marks when parsed", () => { | ||
| const editor = createEditor(); | ||
|
|
||
| const blocks = editor.tryParseHTMLToBlocks( | ||
| `<p><span class="custom-plain-ic">hello <b>world</b></span></p>`, | ||
| ); | ||
|
|
||
| const inlineContent = (blocks[0] as any).content[0]; | ||
| expect(inlineContent.type).toBe("customPlainIC"); | ||
| expect(inlineContent.content).toBe("hello world"); | ||
|
|
||
| editor._tiptapEditor.destroy(); | ||
| }); | ||
|
|
||
| it("converts line breaks to newline characters when parsed", () => { | ||
| const editor = createEditor(); | ||
|
|
||
| const blocks = editor.tryParseHTMLToBlocks( | ||
| `<p><span class="custom-plain-ic">first<br>second</span></p>`, | ||
| ); | ||
|
|
||
| const inlineContent = (blocks[0] as any).content[0]; | ||
| expect(inlineContent.type).toBe("customPlainIC"); | ||
| expect(inlineContent.content).toBe("first\nsecond"); | ||
|
|
||
| editor._tiptapEditor.destroy(); | ||
| }); | ||
|
|
||
| it("disallows formatting marks but allows annotation marks", () => { | ||
| const editor = createEditor(); | ||
|
|
||
| // Checked at the ProseMirror level because the block model intentionally | ||
| // represents plain content as a bare string, without marks. | ||
| const container = document.createElement("div"); | ||
| container.innerHTML = `<p><span class="custom-plain-ic">hello <b>bold</b> <ins data-id="1">inserted</ins></span></p>`; | ||
| const parsed = PMDOMParser.fromSchema(editor.pmSchema).parse(container, { | ||
| topNode: editor.pmSchema.nodes["blockGroup"].create(), | ||
| }); | ||
|
|
||
| let plainIC: any; | ||
| parsed.descendants((node) => { | ||
| if (node.type.name === "customPlainIC") { | ||
| plainIC = node; | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
|
|
||
| expect(plainIC).toBeDefined(); | ||
| expect(plainIC.textContent).toBe("hello bold inserted"); | ||
|
|
||
| const markNames = new Set<string>(); | ||
| plainIC.forEach((child: any) => { | ||
| child.marks.forEach((mark: any) => markNames.add(mark.type.name)); | ||
| }); | ||
| expect(markNames.has("insertion")).toBe(true); | ||
| expect(markNames.has("bold")).toBe(false); | ||
|
|
||
| editor._tiptapEditor.destroy(); | ||
| }); | ||
| }); |
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.
should still be for code too no?