Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: preserve newlines in pre block during migration

thecodrrcc2550753138
diff --git a/packages/core/migrations.js b/packages/core/migrations.js
index 18cd371ed..171dc9dbb 100644
--- a/packages/core/migrations.js
+++ b/packages/core/migrations.js
@@ -93,9 +93,19 @@ function decodeWrappedTableHtml(html) {
});
}
+const NEWLINE_TOKEN = "[[%NN_NEWLINE_IN_PRE%]]";
+const NEWLINE_TOKEN_REGEX = /\[\[%NN_NEWLINE_IN_PRE%\]\]/gm;
+const NEWLINE_REPLACEMENT_REGEX = /\n|<br>|<br\/>/gm;
+const PREBLOCK_REGEX = /<pre.*<\/pre>/gm;
+
export function tinyToTiptap(html) {
if (typeof html !== "string") return html;
+ // Preserve newlines in pre blocks
+ html = html.replace(PREBLOCK_REGEX, (pre) => {
+ return pre.replace(NEWLINE_REPLACEMENT_REGEX, NEWLINE_TOKEN);
+ });
+
const document = parseHTML(html);
const tables = document.querySelectorAll("table");
@@ -124,6 +134,11 @@ export function tinyToTiptap(html) {
if (!p.childNodes.length) p.remove();
}
+ const codeblocks = document.querySelectorAll("pre");
+ for (const pre of codeblocks) {
+ pre.innerHTML = pre.innerText.replace(NEWLINE_TOKEN_REGEX, "<br/>");
+ }
+
const bogus = document.querySelectorAll("[data-mce-bogus]");
for (const element of bogus) {
element.remove();