diff --git a/lib/docsify.js b/lib/docsify.js
index 216bed94..33fd3a73 100644
--- a/lib/docsify.js
+++ b/lib/docsify.js
@@ -76,6 +76,29 @@ function isNil (o) {
return o === null || o === undefined
}
+var cacheRoute$1 = null;
+var cacheHash = null;
+
+/**
+ * hash route
+ */
+function getRoute () {
+ var loc = window.location;
+ if (cacheHash === loc.hash && !isNil(cacheRoute$1)) { return cacheRoute$1 }
+
+ var route = loc.hash.match(/^#\/([^#]+)/);
+
+ if (route && route.length === 2) {
+ route = route[1];
+ } else {
+ route = /^#\//.test(loc.hash) ? '' : loc.pathname;
+ }
+ cacheRoute$1 = route;
+ cacheHash = loc.hash;
+
+ return route
+}
+
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -2231,9 +2254,11 @@ function scrollActiveSidebar () {
for (var i = 0, len = lis.length; i < len; i += 1) {
var li = lis[i];
- var a = li.querySelector('a');
+ var href = li.querySelector('a').getAttribute('href');
- nav[a.getAttribute('href').slice(1)] = li;
+ if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); }
+
+ nav[href] = li;
}
function highlight () {
@@ -2244,8 +2269,7 @@ function scrollActiveSidebar () {
if (bcr.top < 10 && bcr.bottom > 10) {
var li = nav[node.id];
- if (!li) { return }
- if (li === active) { return }
+ if (!li || li === active) { return }
if (active) { active.setAttribute('class', ''); }
li.setAttribute('class', 'active');
@@ -2260,9 +2284,9 @@ function scrollActiveSidebar () {
highlight();
function scrollIntoView () {
- var id = window.location.hash.slice(1);
- if (!id) { return }
- var section = document.querySelector('#' + id);
+ var id = window.location.hash.match(/#[^#\/]+$/g);
+ if (!id || !id.length) { return }
+ var section = document.querySelector(id[0]);
if (section) { section.scrollIntoView(); }
}
@@ -2275,7 +2299,7 @@ function scrollActiveSidebar () {
* Acitve link
*/
function activeLink (dom, activeParent) {
- var host = document.location.origin + document.location.pathname;
+ var host = window.location.href;
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
if (!dom) { return
@@ -2285,6 +2309,10 @@ function activeLink (dom, activeParent) {
activeParent
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active');
+ } else {
+ activeParent
+ ? node.parentNode.removeAttribute('class')
+ : node.removeAttribute('class');
}
});
}
@@ -2305,6 +2333,8 @@ function bindToggle (dom) {
});
}
+var OPTIONS$1 = {};
+
var renderTo = function (dom, content) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
dom.innerHTML = content;
@@ -2319,11 +2349,16 @@ var renderer = new marked.Renderer();
* @link https://github.com/chjj/marked#overriding-renderer-methods
*/
renderer.heading = function (text, level) {
- var slug = text.toLowerCase().replace(/<(?:.|\n)*?>/gm, '').replace(/[\s\n\t]+/g, '-');
+ var slug = text.toLowerCase().replace(/<(?:.|\n)*?>/gm, '').replace(/[^\w|\u4e00-\u9fa5]+/g, '-');
+ var route = '';
- toc.push({ level: level, slug: '#' + slug, title: text });
+ if (OPTIONS$1.router) {
+ route = "#/" + (getRoute());
+ }
- return ("
" + hl + "")
};
+renderer.link = function (href, title, text) {
+ if (OPTIONS$1.router && !/^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/.test(href)) {
+ href = !/^\/#/.test(href) ? ("#" + href) : href;
+ }
+
+ return ("" + text + "")
+};
marked.setOptions({ renderer: renderer });
/**
* App
*/
-function renderApp (dom, replace, opts) {
+function renderApp (dom, replace) {
var nav = document.querySelector('nav') || document.createElement('nav');
- dom[replace ? 'outerHTML' : 'innerHTML'] = toggle(opts.sidebarToggle) + corner(opts.repo) + main();
+ dom[replace ? 'outerHTML' : 'innerHTML'] = toggle(OPTIONS$1.sidebarToggle) + corner(OPTIONS$1.repo) + main();
document.body.insertBefore(nav, document.body.children[0]);
// bind toggle
@@ -2351,18 +2393,18 @@ function renderApp (dom, replace, opts) {
/**
* article
*/
-function renderArticle (content, OPTIONS) {
+function renderArticle (content) {
renderTo('article', content ? marked(content) : 'not found');
- if (!renderSidebar.rendered) { renderSidebar(null, OPTIONS); }
- if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS); }
+ if (!renderSidebar.rendered) { renderSidebar(null, OPTIONS$1); }
+ if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS$1); }
+ renderSidebar.rendered = false;
+ renderNavbar.rendered = false;
}
/**
* navbar
*/
-function renderNavbar (content, OPTIONS) {
- if ( OPTIONS === void 0 ) OPTIONS = {};
-
+function renderNavbar (content) {
renderNavbar.rendered = true;
if (content) { renderTo('nav', marked(content)); }
@@ -2372,24 +2414,27 @@ function renderNavbar (content, OPTIONS) {
/**
* sidebar
*/
-function renderSidebar (content, OPTIONS) {
- if ( OPTIONS === void 0 ) OPTIONS = {};
-
+function renderSidebar (content) {
renderSidebar.rendered = true;
var isToc = false;
if (content) {
content = marked(content);
- } else if (OPTIONS.sidebar) {
- content = tree(OPTIONS.sidebar, '"+s(e.message+"",!0)+"";throw e}}var g={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]+/};g.bullet=/(?:[*+-]|\d+\.)/,g.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,g.item=l(g.item,"gm")(/bull/g,g.bullet)(),g.list=l(g.list)(/bull/g,g.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+g.def.source+")")(),g.blockquote=l(g.blockquote)("def",g.def)(),g._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",g.html=l(g.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)),h.tokens.push({type:i?"loose_item_start":"list_item_start"}),h.token(l,!1,n),h.tokens.push({type:"list_item_end"});h.tokens.push({type:"list_end"})}else if(a=h.rules.html.exec(e))e=e.substring(a[0].length),h.tokens.push({type:h.options.sanitize?"paragraph":"html",pre:!h.options.sanitizer&&("pre"===a[1]||"script"===a[1]||"style"===a[1]),text:a[0]});else if(!n&&t&&(a=h.rules.def.exec(e)))e=e.substring(a[0].length),h.tokens.links[a[1].toLowerCase()]={href:a[2],title:a[3]};else if(t&&(a=h.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