Updated rendering operations to improve performance

This commit is contained in:
Cheng-Han, Wu 2016-02-11 14:35:25 -06:00
parent 2a774064af
commit 8db142554f
4 changed files with 21 additions and 15 deletions

View file

@ -338,13 +338,6 @@ function finishView(view) {
//only static transform should be here //only static transform should be here
function postProcess(code) { function postProcess(code) {
var result = $('<div>' + code + '</div>'); var result = $('<div>' + code + '</div>');
//prevent XSS
result.find("script").replaceWith(function () {
return "<noscript>" + $(this).html() + "</noscript>"
});
result.find("iframe").replaceWith(function () {
return "<noiframe>" + $(this).html() + "</noiframe>"
});
//link should open in new window or tab //link should open in new window or tab
result.find('a:not([href^="#"]):not([target])').attr('target', '_blank'); result.find('a:not([href^="#"]):not([target])').attr('target', '_blank');
//update continue line numbers //update continue line numbers
@ -576,11 +569,11 @@ function autoLinkify(view) {
} }
function deduplicatedHeaderId(view) { function deduplicatedHeaderId(view) {
var headers = view.find(':header').toArray(); var headers = view.find(':header.raw').removeClass('raw').toArray();
for (var i = 0; i < headers.length; i++) { for (var i = 0; i < headers.length; i++) {
var id = $(headers[i]).attr('id'); var id = $(headers[i]).attr('id');
if (!id) continue; if (!id) continue;
var duplicatedHeaders = view.find(':header[id=' + id + ']').toArray(); var duplicatedHeaders = view.find(':header[id="' + id + '"]').toArray();
for (var j = 0; j < duplicatedHeaders.length; j++) { for (var j = 0; j < duplicatedHeaders.length; j++) {
if (duplicatedHeaders[j] != headers[i]) { if (duplicatedHeaders[j] != headers[i]) {
var newId = id + j; var newId = id + j;
@ -685,6 +678,9 @@ md.renderer.rules.list_item_open = function ( /* tokens, idx, options, env */ )
md.renderer.rules.blockquote_open = function (tokens, idx /*, options, env */ ) { md.renderer.rules.blockquote_open = function (tokens, idx /*, options, env */ ) {
return '<blockquote class="raw">\n'; return '<blockquote class="raw">\n';
}; };
md.renderer.rules.heading_open = function (tokens, idx) {
return '<h' + tokens[idx].hLevel + ' class="raw">';
};
md.renderer.rules.fence = function (tokens, idx, options, env, self) { md.renderer.rules.fence = function (tokens, idx, options, env, self) {
var token = tokens[idx]; var token = tokens[idx];
var langClass = ''; var langClass = '';

View file

@ -2132,10 +2132,15 @@ var lastResult = null;
function updateViewInner() { function updateViewInner() {
if (currentMode == modeType.edit || !isDirty) return; if (currentMode == modeType.edit || !isDirty) return;
var value = editor.getValue(); var value = editor.getValue();
var lastMeta = md.meta;
md.meta = {}; md.meta = {};
md.render(value); //only for get meta
parseMeta(md, ui.area.markdown, $('#toc'), $('#toc-affix'));
var rendered = md.render(value); var rendered = md.render(value);
// only render again when meta changed
if (JSON.stringify(md.meta) != JSON.stringify(lastMeta)) {
parseMeta(md, ui.area.markdown, $('#toc'), $('#toc-affix'));
rendered = md.render(value);
}
// prevent XSS
rendered = preventXSS(rendered); rendered = preventXSS(rendered);
var result = postProcess(rendered).children().toArray(); var result = postProcess(rendered).children().toArray();
partialUpdate(result, lastResult, ui.area.markdown.children().toArray()); partialUpdate(result, lastResult, ui.area.markdown.children().toArray());

View file

@ -1,9 +1,14 @@
var markdown = $(".markdown-body"); var markdown = $(".markdown-body");
var text = $('<textarea/>').html(markdown.html()).text(); var text = $('<textarea/>').html(markdown.html()).text();
var lastMeta = md.meta;
md.meta = {}; md.meta = {};
md.render(text); //only for get meta
parseMeta(md, markdown, $('#toc'), $('#toc-affix'));
var rendered = md.render(text); var rendered = md.render(text);
// only render again when meta changed
if (JSON.stringify(md.meta) != JSON.stringify(lastMeta)) {
parseMeta(md, markdown, $('#toc'), $('#toc-affix'));
rendered = md.render(text);
}
// prevent XSS
rendered = preventXSS(rendered); rendered = preventXSS(rendered);
var result = postProcess(rendered); var result = postProcess(rendered);
markdown.html(result.html()); markdown.html(result.html());

View file

@ -73,9 +73,9 @@ md.renderer.rules.heading_open = function (tokens, idx) {
if (tokens[idx].lines && tokens[idx].level === 0) { if (tokens[idx].lines && tokens[idx].level === 0) {
var startline = tokens[idx].lines[0] + 1; var startline = tokens[idx].lines[0] + 1;
var endline = tokens[idx].lines[1]; var endline = tokens[idx].lines[1];
return '<h' + tokens[idx].hLevel + ' class="part" data-startline="' + startline + '" data-endline="' + endline + '">'; return '<h' + tokens[idx].hLevel + ' class="part raw" data-startline="' + startline + '" data-endline="' + endline + '">';
} }
return '<h' + tokens[idx].hLevel + '>'; return '<h' + tokens[idx].hLevel + ' class="raw">';
}; };
md.renderer.rules.fence = function (tokens, idx, options, env, self) { md.renderer.rules.fence = function (tokens, idx, options, env, self) {