Fixed code snippets being added as single line

TinyMCE was adding attributes to <br> 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
This commit is contained in:
Dan Brown 2022-06-21 12:01:06 +01:00
parent 0c6f598d91
commit 9dd69b04b8
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9

View file

@ -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(/<br\s*[\/]?>/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);