From: Dan Brown Date: Tue, 21 Jun 2022 11:01:06 +0000 (+0100) Subject: Fixed code snippets being added as single line X-Git-Tag: v22.06~1^2~6 X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/9dd69b04b8bcb32abe9db60627a52690629bcb52?ds=inline Fixed code snippets being added as single line TinyMCE was adding attributes to
elements within code blocks which would then not be converted to newlines by our code regex match. This changes the conversion to use dom querying instead. Fixes #3507 --- diff --git a/resources/js/wysiwyg/plugin-codeeditor.js b/resources/js/wysiwyg/plugin-codeeditor.js index 82ab7d3c8..4123fb783 100644 --- a/resources/js/wysiwyg/plugin-codeeditor.js +++ b/resources/js/wysiwyg/plugin-codeeditor.js @@ -86,7 +86,13 @@ function defineCodeBlockCustomElement(editor) { getContent() { const code = this.querySelector('code') || this.querySelector('pre'); const tempEl = document.createElement('pre'); - tempEl.innerHTML = code.innerHTML.replace().replace(//gi ,'\n').replace(/\ufeff/g, ''); + tempEl.innerHTML = code.innerHTML.replace(/\ufeff/g, ''); + + const brs = tempEl.querySelectorAll('br'); + for (const br of brs) { + br.replaceWith('\n'); + } + return tempEl.textContent; } @@ -104,6 +110,7 @@ function defineCodeBlockCustomElement(editor) { const container = this.shadowRoot.querySelector('.CodeMirrorContainer'); const renderCodeMirror = (Code) => { + console.log({content}); this.cm = Code.wysiwygView(container, content, this.getLanguage()); Code.updateLayout(this.cm); setTimeout(() => { @@ -159,6 +166,7 @@ function register(editor, url) { showPopup(editor, textContent, '', (newCode, newLang) => { const pre = doc.createElement('pre'); const code = doc.createElement('code'); + console.log(newCode); code.classList.add(`language-${newLang}`); code.innerText = newCode; pre.append(code);