From 35f375d7562f0ab36edc6e179e3c902c96cf0fee Mon Sep 17 00:00:00 2001
From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com>
Date: Thu, 27 Jul 2023 03:52:41 -0700
Subject: [PATCH] Desktop: Fixes #6055: Preserve empty newlines created by
pressing Enter repeatedly in the rich text editor (#8549)
---
.../html_to_md/paragraph_with_nonbreaking_space.html | 3 +++
.../tests/html_to_md/paragraph_with_nonbreaking_space.md | 5 +++++
.../gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.tsx | 9 +++++++--
packages/turndown/src/commonmark-rules.js | 8 +++++++-
packages/turndown/src/utilities.js | 2 +-
5 files changed, 23 insertions(+), 4 deletions(-)
create mode 100644 packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.html
create mode 100644 packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.md
diff --git a/packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.html b/packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.html
new file mode 100644
index 00000000000..184862f853e
--- /dev/null
+++ b/packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.html
@@ -0,0 +1,3 @@
+
Paragraphs with a single nonbreaking space should be preserved:
+
+
\ No newline at end of file
diff --git a/packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.md b/packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.md
new file mode 100644
index 00000000000..c1baaecabbb
--- /dev/null
+++ b/packages/app-cli/tests/html_to_md/paragraph_with_nonbreaking_space.md
@@ -0,0 +1,5 @@
+Paragraphs with a single nonbreaking space should be preserved:
+
+
+
+
\ No newline at end of file
diff --git a/packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.tsx b/packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.tsx
index 40ea310ac0e..7258bffffc7 100644
--- a/packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.tsx
+++ b/packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.tsx
@@ -91,7 +91,7 @@ let dispatchDidUpdateIID_: any = null;
let changeId_ = 1;
const TinyMCE = (props: NoteBodyEditorProps, ref: any) => {
- const [editor, setEditor] = useState(null);
+ const [editor, setEditor] = useState(null);
const [scriptLoaded, setScriptLoaded] = useState(false);
const [editorReady, setEditorReady] = useState(false);
const [draggingStarted, setDraggingStarted] = useState(false);
@@ -578,7 +578,12 @@ const TinyMCE = (props: NoteBodyEditorProps, ref: any) => {
icons_url: 'gui/NoteEditor/NoteBody/TinyMCE/icons.js',
plugins: 'noneditable link joplinLists hr searchreplace codesample table',
noneditable_noneditable_class: 'joplin-editable', // Can be a regex too
- valid_elements: '*[*]', // We already filter in sanitize_html
+
+ // #p: Pad empty paragraphs with to prevent them from being removed.
+ // *[*]: Allow all elements and attributes -- we already filter in sanitize_html
+ // See https://www.tiny.cloud/docs/configure/content-filtering/#controlcharacters
+ valid_elements: '#p,*[*]',
+
menubar: false,
relative_urls: false,
branding: false,
diff --git a/packages/turndown/src/commonmark-rules.js b/packages/turndown/src/commonmark-rules.js
index 5a60c64d816..66a39896ba4 100644
--- a/packages/turndown/src/commonmark-rules.js
+++ b/packages/turndown/src/commonmark-rules.js
@@ -1,4 +1,4 @@
-import { repeat, isCodeBlockSpecialCase1, isCodeBlockSpecialCase2, isCodeBlock, getStyleProp, htmlEscapeLeadingNonbreakingSpace } from './utilities'
+import { repeat, isCodeBlockSpecialCase1, isCodeBlockSpecialCase2, isCodeBlock, getStyleProp } from './utilities'
const Entities = require('html-entities').AllHtmlEntities;
const htmlentities = (new Entities()).encode;
@@ -32,6 +32,12 @@ rules.paragraph = {
const leadingNonbreakingSpace = /^\u{00A0}/ug;
content = content.replace(leadingNonbreakingSpace, ' ');
+ // Paragraphs that are truly empty (not even containing nonbreaking spaces)
+ // take up by default no space. Output nothing.
+ if (content === '') {
+ return '';
+ }
+
return '\n\n' + content + '\n\n'
}
}
diff --git a/packages/turndown/src/utilities.js b/packages/turndown/src/utilities.js
index 6a1afb33ad1..04090cb6d57 100644
--- a/packages/turndown/src/utilities.js
+++ b/packages/turndown/src/utilities.js
@@ -53,7 +53,7 @@ export function hasVoid (node) {
var meaningfulWhenBlankElements = [
'A', 'TABLE', 'THEAD', 'TBODY', 'TFOOT', 'TH', 'TD', 'IFRAME', 'SCRIPT',
- 'AUDIO', 'VIDEO'
+ 'AUDIO', 'VIDEO', 'P'
]
export function isMeaningfulWhenBlank (node) {