-
-
Notifications
You must be signed in to change notification settings - Fork 700
fix(core): preserve whitespace edge cases but collapse html formatting newlines (BLO-1065) #2551
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
YousefED
wants to merge
3
commits into
main
Choose a base branch
from
copy-paste-from-ms-word-is-broken-blo-1065
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.
+285
−9
Open
Changes from all commits
Commits
Show all changes
3 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
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
87 changes: 87 additions & 0 deletions
87
packages/core/src/api/parsers/html/util/normalizeWhitespace.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,87 @@ | ||
| /** | ||
| * Checks if the given HTML element contains markers indicating it was | ||
| * generated by Notion. Notion uses `\n` in text nodes to represent hard | ||
| * breaks, which is non-standard but intentional. | ||
| * | ||
| * Detected by the `<!-- notionvc: UUID -->` comment that Notion places | ||
| * on the clipboard. | ||
| */ | ||
| function isNotionHTML(element: HTMLElement): boolean { | ||
| const walker = element.ownerDocument.createTreeWalker( | ||
| element, | ||
| // NodeFilter.SHOW_COMMENT | ||
| 128, | ||
| ); | ||
|
|
||
| let node: Node | null; | ||
| while ((node = walker.nextNode())) { | ||
| if (/^\s*notionvc:/.test(node.nodeValue || "")) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes whitespace in text nodes by collapsing runs of whitespace | ||
| * (including newlines) to single spaces, matching CSS white-space:normal | ||
| * behavior. | ||
| * | ||
| * This is needed because ProseMirror's DOMParser, when `linebreakReplacement` | ||
| * is set in the schema (as BlockNote does for hard breaks), converts `\n` | ||
| * characters in text nodes to hard break nodes instead of collapsing them. | ||
| * This causes HTML source line wrapping (e.g. from MS Word) to create | ||
| * visible line breaks in the editor. | ||
| * | ||
| * Skipped for sources like Notion that intentionally use `\n` in text nodes | ||
| * to represent hard breaks instead of `<br>` tags. | ||
| * | ||
| * Skips `<pre>` and `<code>` elements where whitespace should be preserved. | ||
| */ | ||
| function normalizeTextNodeWhitespace(element: HTMLElement) { | ||
| const preserveWSTags = new Set(["PRE", "CODE"]); | ||
| const walker = element.ownerDocument.createTreeWalker( | ||
| element, | ||
| // NodeFilter.SHOW_TEXT | ||
| 4, | ||
| { | ||
| acceptNode(node) { | ||
| // Skip text nodes inside pre/code elements | ||
| let parent = node.parentElement; | ||
| while (parent && parent !== element) { | ||
| if (preserveWSTags.has(parent.tagName)) { | ||
| // NodeFilter.FILTER_REJECT | ||
| return 2; | ||
| } | ||
| parent = parent.parentElement; | ||
| } | ||
| // NodeFilter.FILTER_ACCEPT | ||
| return 1; | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| const textNodes: Text[] = []; | ||
| let node: Node | null; | ||
| while ((node = walker.nextNode())) { | ||
| textNodes.push(node as Text); | ||
| } | ||
|
|
||
| for (const textNode of textNodes) { | ||
| if (textNode.nodeValue && /[\r\n]/.test(textNode.nodeValue)) { | ||
| textNode.nodeValue = textNode.nodeValue.replace(/[ \t\r\n\f]+/g, " "); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes whitespace in HTML text nodes to match standard CSS | ||
| * white-space:normal behavior. Skipped for Notion HTML which intentionally | ||
| * uses `\n` for hard breaks. | ||
| */ | ||
| export function preprocessHTMLWhitespace(element: HTMLElement) { | ||
| if (!isNotionHTML(element)) { | ||
| normalizeTextNodeWhitespace(element); | ||
| } | ||
| } |
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
126 changes: 126 additions & 0 deletions
126
tests/src/unit/core/formatConversion/parse/__snapshots__/html/msWordPaste.json
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,126 @@ | ||
| [ | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": { | ||
| "bold": true, | ||
| "underline": true, | ||
| }, | ||
| "text": "Que se passe-t-il si je réponds tard à un message chat et que l'utilisateur n'est plus en ligne :", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "1", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": "Lorsque vous envoyez un message à un utilisateur dans une conversation chat, et qu'il est encore en ligne, il recevra le message sur sa bulle chatbot.", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "2", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": "Cependant S'il n'est plus en ligne, votre message sera envoyé par email si :", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "3", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": ". l'utilisateur n'a pas lu votre réponse après 2 minutes", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "4", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": ". l'utilisateur n'est plus présent sur votre site web", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "5", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": " ", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "6", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| { | ||
| "children": [], | ||
| "content": [ | ||
| { | ||
| "styles": {}, | ||
| "text": "Cela se fait automatiquement donc, lorsque nous répondons par chat, si l'utilisateur n'est plus là, Crisp renvoie le message alors par email et le canal de discussion se transforme en canal de discussion email. | ||
|
|
||
| Il est possible aussi de créer une conversation email directement le profil de l'utilisateur (bouton bleu en haut à droite de la conversation)", | ||
| "type": "text", | ||
| }, | ||
| ], | ||
| "id": "7", | ||
| "props": { | ||
| "backgroundColor": "default", | ||
| "textAlignment": "left", | ||
| "textColor": "default", | ||
| }, | ||
| "type": "paragraph", | ||
| }, | ||
| ] |
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.
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.
Why here & not as part of nestesListToBlockNoteStructure?