From 8db142554fbe1c85aa8ffcdf0900b2855645939b Mon Sep 17 00:00:00 2001 From: Cheng-Han, Wu Date: Thu, 11 Feb 2016 14:35:25 -0600 Subject: Updated rendering operations to improve performance --- public/js/extra.js | 14 +++++--------- public/js/index.js | 9 +++++++-- public/js/pretty.js | 9 +++++++-- public/js/syncscroll.js | 4 ++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/public/js/extra.js b/public/js/extra.js index a2ae83f6..28a91fbc 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -338,13 +338,6 @@ function finishView(view) { //only static transform should be here function postProcess(code) { var result = $('
' + code + '
'); - //prevent XSS - result.find("script").replaceWith(function () { - return "" - }); - result.find("iframe").replaceWith(function () { - return "" + $(this).html() + "" - }); //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 '
\n'; }; +md.renderer.rules.heading_open = function (tokens, idx) { + return ''; +}; md.renderer.rules.fence = function (tokens, idx, options, env, self) { var token = tokens[idx]; var langClass = ''; diff --git a/public/js/index.js b/public/js/index.js index 2e797ac3..3507e016 100644 --- a/public/js/index.js +++ b/public/js/index.js @@ -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()); diff --git a/public/js/pretty.js b/public/js/pretty.js index a38dc244..40d90cf7 100644 --- a/public/js/pretty.js +++ b/public/js/pretty.js @@ -1,9 +1,14 @@ var markdown = $(".markdown-body"); var text = $('