diff --git a/lib/docsify.js b/lib/docsify.js
index 33fd3a7..f842df9 100644
--- a/lib/docsify.js
+++ b/lib/docsify.js
@@ -99,6 +99,98 @@ function getRoute () {
return route
}
+/**
+ * Active sidebar when scroll
+ * @link https://buble.surge.sh/
+ */
+function scrollActiveSidebar () {
+ if (/mobile/i.test(navigator.userAgent)) { return }
+
+ var anchors = document.querySelectorAll('.anchor');
+ var nav = {};
+ var lis = document.querySelectorAll('.sidebar li');
+ var active = null;
+
+ for (var i = 0, len = lis.length; i < len; i += 1) {
+ var li = lis[i];
+ var href = li.querySelector('a').getAttribute('href');
+
+ if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); }
+
+ nav[href] = li;
+ }
+
+ function highlight () {
+ for (var i = 0, len = anchors.length; i < len; i += 1) {
+ var node = anchors[i].parentNode;
+ var bcr = node.getBoundingClientRect();
+
+ if (bcr.top < 10 && bcr.bottom > 10) {
+ var li = nav[node.id];
+
+ if (!li || li === active) { return }
+ if (active) { active.setAttribute('class', ''); }
+
+ li.setAttribute('class', 'active');
+ active = li;
+
+ return
+ }
+ }
+ }
+
+ var dom = document.querySelector('main .content');
+ dom.removeEventListener('scroll', highlight);
+ dom.addEventListener('scroll', highlight);
+ highlight();
+}
+
+function scrollIntoView () {
+ var id = window.location.hash.match(/#[^#\/]+$/g);
+ if (!id || !id.length) { return }
+ var section = document.querySelector(id[0]);
+
+ if (section) { section.scrollIntoView(); }
+}
+
+/**
+ * Acitve link
+ */
+function activeLink (dom, activeParent) {
+ var host = window.location.href;
+
+ dom = typeof dom === 'object' ? dom : document.querySelector(dom);
+ if (!dom) { return
+
+ ; }[].slice.call(dom.querySelectorAll('a')).forEach(function (node) {
+ if (node.href === host) {
+ activeParent
+ ? node.parentNode.setAttribute('class', 'active')
+ : node.setAttribute('class', 'active');
+ } else {
+ activeParent
+ ? node.parentNode.removeAttribute('class')
+ : node.removeAttribute('class');
+ }
+ });
+}
+
+/**
+ * sidebar toggle
+ */
+function bindToggle (dom) {
+ dom = typeof dom === 'object' ? dom : document.querySelector(dom);
+ if (!dom) { return }
+ var body = document.body;
+
+ dom.addEventListener('click', function () { return body.classList.toggle('close'); });
+
+ if (!/mobile/i.test(navigator.userAgent)) { return }
+ document.querySelector('aside').addEventListener('click', function (event) {
+ body.classList.toggle('close');
+ });
+}
+
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -2240,100 +2332,8 @@ function tree (toc, tpl) {
return tpl
}
-/**
- * Active sidebar when scroll
- * @link https://buble.surge.sh/
- */
-function scrollActiveSidebar () {
- if (/mobile/i.test(navigator.userAgent)) { return }
-
- var anchors = document.querySelectorAll('.anchor');
- var nav = {};
- var lis = document.querySelectorAll('.sidebar li');
- var active = null;
-
- for (var i = 0, len = lis.length; i < len; i += 1) {
- var li = lis[i];
- var href = li.querySelector('a').getAttribute('href');
-
- if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); }
-
- nav[href] = li;
- }
-
- function highlight () {
- for (var i = 0, len = anchors.length; i < len; i += 1) {
- var node = anchors[i].parentNode;
- var bcr = node.getBoundingClientRect();
-
- if (bcr.top < 10 && bcr.bottom > 10) {
- var li = nav[node.id];
-
- if (!li || li === active) { return }
- if (active) { active.setAttribute('class', ''); }
-
- li.setAttribute('class', 'active');
- active = li;
-
- return
- }
- }
- }
-
- document.querySelector('main .content').addEventListener('scroll', highlight);
- highlight();
-
- function scrollIntoView () {
- var id = window.location.hash.match(/#[^#\/]+$/g);
- if (!id || !id.length) { return }
- var section = document.querySelector(id[0]);
-
- if (section) { section.scrollIntoView(); }
- }
-
- window.addEventListener('hashchange', scrollIntoView);
- scrollIntoView();
-}
-
-/**
- * Acitve link
- */
-function activeLink (dom, activeParent) {
- var host = window.location.href;
-
- dom = typeof dom === 'object' ? dom : document.querySelector(dom);
- if (!dom) { return
-
- ; }[].slice.call(dom.querySelectorAll('a')).forEach(function (node) {
- if (node.href === host) {
- activeParent
- ? node.parentNode.setAttribute('class', 'active')
- : node.setAttribute('class', 'active');
- } else {
- activeParent
- ? node.parentNode.removeAttribute('class')
- : node.removeAttribute('class');
- }
- });
-}
-
-/**
- * sidebar toggle
- */
-function bindToggle (dom) {
- dom = typeof dom === 'object' ? dom : document.querySelector(dom);
- if (!dom) { return }
- var body = document.body;
-
- dom.addEventListener('click', function () { return body.classList.toggle('close'); });
-
- if (!/mobile/i.test(navigator.userAgent)) { return }
- document.querySelector('aside').addEventListener('click', function (event) {
- body.classList.toggle('close');
- });
-}
-
var OPTIONS$1 = {};
+var CACHE = {};
var renderTo = function (dom, content) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
@@ -2405,6 +2405,8 @@ function renderArticle (content) {
* navbar
*/
function renderNavbar (content) {
+ if (CACHE['navbar'] === content) { return }
+ CACHE['navbar'] = content;
renderNavbar.rendered = true;
if (content) { renderTo('nav', marked(content)); }
@@ -2415,6 +2417,8 @@ function renderNavbar (content) {
* sidebar
*/
function renderSidebar (content) {
+ if (CACHE['sidebar'] === content) { return }
+ CACHE['sidebar'] = content;
renderSidebar.rendered = true;
var isToc = false;
@@ -2429,7 +2433,7 @@ function renderSidebar (content) {
}
renderTo('aside.sidebar', content);
- isToc ? scrollActiveSidebar() : activeLink('aside.sidebar', true);
+ if (isToc) { scrollActiveSidebar(); }
toc = [];
}
@@ -2465,25 +2469,37 @@ config(OPTIONS);
var cacheRoute = null;
-var mainRender = function () {
+var mainRender = function (cb) {
var route = getRoute();
- if (cacheRoute === route) { return }
-
+ if (cacheRoute === route) { return cb() }
+ var wait;
var basePath = cacheRoute = route;
if (!/\//.test(basePath)) {
basePath = '';
} else if (basePath && !/\/$/.test(basePath)) {
- basePath = basePath.match(/(\S+\/)[^\/]+$/)[1];
+ basePath = basePath.match(/(\S*\/)[^\/]+$/)[1];
}
// Render markdown file
load((!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md"))
- .then(renderArticle, function (_) { return renderArticle(null); });
+ .then(function (result) {
+ renderArticle(result);
+ if (OPTIONS.loadSidebar) {
+ if (wait === false) { cb(); }
+ wait = false;
+ } else {
+ cb();
+ }
+ }, function (_) { return renderArticle(null); });
// Render sidebar
if (OPTIONS.loadSidebar) {
- load(basePath + OPTIONS.loadSidebar).then(renderSidebar);
+ load(basePath + OPTIONS.loadSidebar).then(function (result) {
+ renderSidebar(result);
+ if (wait === false) { cb(); }
+ wait = false;
+ });
}
// Render navbar
@@ -2495,13 +2511,19 @@ var mainRender = function () {
var Docsify = function () {
var dom = document.querySelector(OPTIONS.el) || document.body;
var replace = dom !== document.body;
+ var main = function () {
+ mainRender(function (_) {
+ activeLink('aside.sidebar', true);
+ scrollIntoView();
+ });
+ };
// Render app
renderApp(dom, replace);
- mainRender();
+ main();
if (OPTIONS.router) {
if (!/^#\//.test(window.location.hash)) { window.location.hash = '#/'; }
- window.addEventListener('hashchange', mainRender);
+ window.addEventListener('hashchange', main);
}
};
diff --git a/lib/docsify.min.js b/lib/docsify.min.js
index 4cf70a0..161c160 100644
--- a/lib/docsify.min.js
+++ b/lib/docsify.min.js
@@ -1,2 +1,2 @@
-var Docsify=function(){"use strict";function e(e,t){void 0===t&&(t="get");var n=new XMLHttpRequest;return n.open(t,e),n.send(),{then:function(e,t){void 0===t&&(t=function(){}),n.addEventListener("error",t),n.addEventListener("load",function(n){var r=n.target;r.status>=400?t(r):e(r.response)})}}}function t(e,t){var n=[],r={};return e.forEach(function(e){var i=e.level||1,a=i-1;i>t||(r[a]?(r[a].children=r[a].children||[],r[a].children.push(e)):n.push(e),r[i]=e)}),n}function n(e){return e.replace(/([A-Z])/g,function(e){return"-"+e.toLowerCase()})}function r(e){return null===e||void 0===e}function i(){var e=window.location;if(k===e.hash&&!r(y))return y;var t=e.hash.match(/^#\/([^#]+)/);return t=t&&2===t.length?t[1]:/^#\//.test(e.hash)?"":e.pathname,y=t,k=e.hash,t}function a(e,t){return t={exports:{}},e(t,t.exports),t.exports}function s(e){return e?(/\/\//.test(e)||(e="https://github.com/"+e),e=e.replace(/^git\+/,""),'\n \n \n '):""}function o(){return'
"+s(e.message+"",!0)+"";throw e}}var h={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:u,hr:/^( *[-*_]){3,} *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,nptable:u,lheading:/^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,blockquote:/^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,list:/^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:/^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,def:/^ *\[([^\]]+)\]: *([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,table:u,paragraph:/^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,text:/^[^\n]+/};h.bullet=/(?:[*+-]|\d+\.)/,h.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,h.item=l(h.item,"gm")(/bull/g,h.bullet)(),h.list=l(h.list)(/bull/g,h.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+h.def.source+")")(),h.blockquote=l(h.blockquote)("def",h.def)(),h._tag="(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b",h.html=l(h.html)("comment",//)("closed",/<(tag)[\s\S]+?<\/\1>/)("closing",/
1&&o.length>1||(e=a.slice(c+1).join("\n")+e,c=p-1)),i=r||/\n\n(?!\s*$)/.test(l),c!==p-1&&(r="\n"===l.charAt(l.length-1),i||(i=r)),g.tokens.push({type:i?"loose_item_start":"list_item_start"}),g.token(l,!1,n),g.tokens.push({type:"list_item_end"});g.tokens.push({type:"list_end"})}else if(a=g.rules.html.exec(e))e=e.substring(a[0].length),g.tokens.push({type:g.options.sanitize?"paragraph":"html",pre:!g.options.sanitizer&&("pre"===a[1]||"script"===a[1]||"style"===a[1]),text:a[0]});else if(!n&&t&&(a=g.rules.def.exec(e)))e=e.substring(a[0].length),g.tokens.links[a[1].toLowerCase()]={href:a[2],title:a[3]};else if(t&&(a=g.rules.table.exec(e))){for(e=e.substring(a[0].length),l={type:"table",header:a[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:a[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:a[3].replace(/(?: *\| *)?\n$/,"").split("\n")},c=0;c "+e+"
\n":"'+(n?e:s(e,!0))+"\n
"},i.prototype.blockquote=function(e){return""+(n?e:s(e,!0))+"\n\n"+e+"
\n"},i.prototype.html=function(e){return e},i.prototype.heading=function(e,t,n){return"
\n":"
\n"},i.prototype.list=function(e,t){var n=t?"ol":"ul";return"<"+n+">\n"+e+""+n+">\n"},i.prototype.listitem=function(e){return"\n\n"+e+"\n\n"+t+"\n
\n"},i.prototype.tablerow=function(e){return"\n"+e+" \n"},i.prototype.tablecell=function(e,t){var n=t.header?"th":"td",r=t.align?"<"+n+' style="text-align:'+t.align+'">':"<"+n+">";return r+e+""+n+">\n"},i.prototype.strong=function(e){return""+e+""},i.prototype.em=function(e){return""+e+""},i.prototype.codespan=function(e){return""+e+""},i.prototype.br=function(){return this.options.xhtml?"
":"
"},i.prototype.del=function(e){return""+e+""},i.prototype.link=function(e,t,n){if(this.options.sanitize){try{var r=decodeURIComponent(o(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(e){return""}if(0===r.indexOf("javascript:")||0===r.indexOf("vbscript:"))return""}var i='"+n+""},i.prototype.image=function(e,t,n){var r='":">"},i.prototype.text=function(e){return e},a.parse=function(e,t,n){var r=new a(t,n);return r.parse(e)},a.prototype.parse=function(e){var t=this;this.inline=new r(e.links,this.options,this.renderer),this.tokens=e.reverse();for(var n="";this.next();)n+=t.tok();return n},a.prototype.next=function(){return this.token=this.tokens.pop()},a.prototype.peek=function(){return this.tokens[this.tokens.length-1]||0},a.prototype.parseText=function(){for(var e=this,t=this.token.text;"text"===this.peek().type;)t+="\n"+e.next().text;return this.inline.output(t)},a.prototype.tok=function(){var e=this;switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.inline.output(this.token.text),this.token.depth,this.token.text);case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var t,n,r,i,a,s="",o="";for(r="",t=0;t