mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-08-26 04:05:57 +00:00
Fixed code block language parsing issue
Language parsing of code blocks could falter on pasted code blocks due to the lanuage being parsed with a space which would throw an error when used as a css class. This adds more extensive language parsing to be safer. Fixes #3133
This commit is contained in:
parent
dba506a20e
commit
819ec55b1b
@ -211,9 +211,9 @@ function wysiwygView(elem) {
|
|||||||
const doc = elem.ownerDocument;
|
const doc = elem.ownerDocument;
|
||||||
const codeElem = elem.querySelector('code');
|
const codeElem = elem.querySelector('code');
|
||||||
|
|
||||||
let lang = (elem.className || '').replace('language-', '');
|
let lang = getLanguageFromCssClasses(elem.className || '');
|
||||||
if (lang === '' && codeElem) {
|
if (!lang && codeElem) {
|
||||||
lang = (codeElem.className || '').replace('language-', '')
|
lang = getLanguageFromCssClasses(codeElem.className || '');
|
||||||
}
|
}
|
||||||
|
|
||||||
elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
|
elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
|
||||||
@ -228,7 +228,7 @@ function wysiwygView(elem) {
|
|||||||
elem.parentNode.replaceChild(newWrap, elem);
|
elem.parentNode.replaceChild(newWrap, elem);
|
||||||
|
|
||||||
newWrap.appendChild(newTextArea);
|
newWrap.appendChild(newTextArea);
|
||||||
newWrap.contentEditable = false;
|
newWrap.contentEditable = 'false';
|
||||||
newTextArea.textContent = content;
|
newTextArea.textContent = content;
|
||||||
|
|
||||||
let cm = CodeMirror(function(elt) {
|
let cm = CodeMirror(function(elt) {
|
||||||
@ -245,6 +245,16 @@ function wysiwygView(elem) {
|
|||||||
return {wrap: newWrap, editor: cm};
|
return {wrap: newWrap, editor: cm};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the code language from the given css classes.
|
||||||
|
* @param {String} classes
|
||||||
|
* @return {String}
|
||||||
|
*/
|
||||||
|
function getLanguageFromCssClasses(classes) {
|
||||||
|
const langClasses = classes.split(' ').filter(cssClass => cssClass.startsWith('language-'));
|
||||||
|
return (langClasses[0] || '').replace('language-', '');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a CodeMirror instance to show in the WYSIWYG pop-up editor
|
* Create a CodeMirror instance to show in the WYSIWYG pop-up editor
|
||||||
* @param {HTMLElement} elem
|
* @param {HTMLElement} elem
|
||||||
|
Loading…
Reference in New Issue
Block a user