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
function postProcess(code) {
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
result.find('a:not([href^="#"]):not([target])').attr('target', '_blank');
//update continue line numbers
@ -576,11 +569,11 @@ function autoLinkify(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++) {
var id = $(headers[i]).attr('id');
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++) {
if (duplicatedHeaders[j] != headers[i]) {
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 */ ) {
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) {
var token = tokens[idx];
var langClass = '';

View File

@ -2132,10 +2132,15 @@ var lastResult = null;
function updateViewInner() {
if (currentMode == modeType.edit || !isDirty) return;
var value = editor.getValue();
var lastMeta = md.meta;
md.meta = {};
md.render(value); //only for get meta
parseMeta(md, ui.area.markdown, $('#toc'), $('#toc-affix'));
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);
var result = postProcess(rendered).children().toArray();
partialUpdate(result, lastResult, ui.area.markdown.children().toArray());

View File

@ -1,9 +1,14 @@
var markdown = $(".markdown-body");
var text = $('<textarea/>').html(markdown.html()).text();
var lastMeta = md.meta;
md.meta = {};
md.render(text); //only for get meta
parseMeta(md, markdown, $('#toc'), $('#toc-affix'));
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);
var result = postProcess(rendered);
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) {
var startline = tokens[idx].lines[0] + 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) {