diff --git a/src/resources/hoedown.js b/src/resources/hoedown.js index 3089a5a6..8d7e8249 100644 --- a/src/resources/hoedown.js +++ b/src/resources/hoedown.js @@ -14,6 +14,8 @@ marked.setOptions({ }); var updateHtml = function(html) { + startFreshRender(); + // There is at least one async job for MathJax. asyncJobsCount = 1; diff --git a/src/resources/markdown-it.js b/src/resources/markdown-it.js index e0e87e4d..675c2333 100644 --- a/src/resources/markdown-it.js +++ b/src/resources/markdown-it.js @@ -133,6 +133,8 @@ var updateText = function(text) { text = "[TOC]\n\n" + text; } + startFreshRender(); + // There is at least one async job for MathJax. asyncJobsCount = 1; metaDataText = null; diff --git a/src/resources/markdown_template.js b/src/resources/markdown_template.js index 8d07f24f..6a54dc5a 100644 --- a/src/resources/markdown_template.js +++ b/src/resources/markdown_template.js @@ -265,13 +265,22 @@ window.onwheel = function(e) { } } +var skipScrollCheckRange = null; + window.onscroll = function() { if (g_muteScroll) { return; } - currentHeaderIdx = -1; var scrollTop = document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset; + if (skipScrollCheckRange + && skipScrollCheckRange.start <= scrollTop + && skipScrollCheckRange.end > scrollTop) { + return; + } + + currentHeaderIdx = -1; + skipScrollCheckRange = null; var eles = document.querySelectorAll("h1, h2, h3, h4, h5, h6"); if (eles.length == 0) { @@ -279,7 +288,8 @@ window.onscroll = function() { return; } - var biaScrollTop = scrollTop + 50; + var bias = 50; + var biaScrollTop = scrollTop + bias; for (var i = 0; i < eles.length; ++i) { if (biaScrollTop >= eles[i].offsetTop) { currentHeaderIdx = i; @@ -291,6 +301,17 @@ window.onscroll = function() { var curHeader = null; if (currentHeaderIdx != -1) { curHeader = eles[currentHeaderIdx].getAttribute("id"); + + // Update the range which can be skipped to check. + var endOffset; + if (currentHeaderIdx < eles.length - 1) { + endOffset = eles[currentHeaderIdx + 1].offsetTop - bias; + } else { + endOffset = document.documentElement.scrollHeight; + } + + skipScrollCheckRange = { start: eles[currentHeaderIdx].offsetTop - bias, + end: endOffset }; } content.setHeader(curHeader ? curHeader : ""); @@ -1827,3 +1848,8 @@ var clearMarkRectDivs = function() { delete n; } }; + +// Clean up before a fresh render. +var startFreshRender = function() { + skipScrollCheckRange = null; +}; diff --git a/src/resources/marked.js b/src/resources/marked.js index 37d409bd..32747bf6 100644 --- a/src/resources/marked.js +++ b/src/resources/marked.js @@ -50,6 +50,8 @@ var updateText = function(text) { text = "[TOC]\n\n" + text; } + startFreshRender(); + // There is at least one async job for MathJax. asyncJobsCount = 1; diff --git a/src/resources/showdown.js b/src/resources/showdown.js index e40cabba..dd4fbb14 100644 --- a/src/resources/showdown.js +++ b/src/resources/showdown.js @@ -89,6 +89,8 @@ var updateText = function(text) { text = "[TOC]\n\n" + text; } + startFreshRender(); + // There is at least one async job for MathJax. asyncJobsCount = 1;