#461 Remove md tags by using temporary div

This commit is contained in:
Thiago Veronezi 2016-11-16 12:58:44 -05:00
parent 40d02812b1
commit 293911ccde

View File

@ -1001,23 +1001,32 @@ function _toggleBlock(editor, type, start_chars, end_chars) {
function _cleanBlock(cm) { function _cleanBlock(cm) {
if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className)) if(/editor-preview-active/.test(cm.getWrapperElement().lastChild.className))
return; return;
// split the selection in lines
var startPoint = cm.getCursor("start"); var selections = cm.getSelection().split("\n");
var endPoint = cm.getCursor("end"); var removeTags = function(selection) {
var text; var html = marked(selection);
// create a div...
for(var line = startPoint.line; line <= endPoint.line; line++) { var tmp = document.createElement("DIV");
text = cm.getLine(line); // .. with the new generated html code...
text = text.replace(/^[ ]*([# ]+|\*|\-|[> ]+|[0-9]+(.|\)))[ ]*/, ""); tmp.innerHTML = html;
// ... now read the text of the generated code.
cm.replaceRange(text, { // This way the browser does the job of removing the tags.
line: line, var result = selection;
ch: 0 if(tmp.textContent) {
}, { result = tmp.textContent;
line: line, } else if(tmp.innerText) {
ch: 99999999999999 result = tmp.innerText;
});
} }
// removing trailing "new line"
return result.split("\n").join("");
};
var result = [];
for(var i = 0; i < selections.length; i++) {
result.push(removeTags(selections[i]));
}
// Add removed "new lines" back to the resulting string.
// Replace the selection with the new clean selection.
cm.replaceSelection(result.join("\n"));
} }
// Merge the properties of one object into another. // Merge the properties of one object into another.