diff --git a/lib/docsify.js b/lib/docsify.js index d28abf5f..b6cac80f 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -130,7 +130,10 @@ function scrollActiveSidebar () { var li = lis[i]; var href = li.querySelector('a').getAttribute('href'); - if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); } + if (href !== '/') { + var match = href.match('#([^#]+)$'); + if (match && match.length) { href = match[0].slice(1); } + } nav[decodeURIComponent(href)] = li; } @@ -174,19 +177,23 @@ function activeLink (dom, activeParent) { var host = window.location.href; dom = typeof dom === 'object' ? dom : document.querySelector(dom); - if (!dom) { return + if (!dom) { return } + var target;[].slice.call(dom.querySelectorAll('a')) + .sort(function (a, b) { return b.href.length - a.href.length; }) + .forEach(function (node) { + if (host.indexOf(node.href) === 0 && !target) { + activeParent + ? node.parentNode.setAttribute('class', 'active') + : node.setAttribute('class', 'active'); + target = node; + } else { + activeParent + ? node.parentNode.removeAttribute('class') + : node.removeAttribute('class'); + } + }); - ; }[].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'); - } - }); + return target } /** @@ -2348,7 +2355,7 @@ function cover () { var SL = ', 100%, 85%'; var bgc = "linear-gradient(to left bottom, hsl(" + (Math.floor(Math.random() * 255) + SL) + ") 0%, hsl(" + (Math.floor(Math.random() * 255) + SL) + ") 100%)"; - return ("
\n
\n
") + return ("
\n
\n
\n
") } function toggle () { @@ -2452,12 +2459,12 @@ function renderApp (dom, replace) { */ function renderArticle (content) { renderTo('article', content ? marked(content) : 'not found'); - if (!renderSidebar.rendered) { renderSidebar(null, OPTIONS$1); } - if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS$1); } - renderSidebar.rendered = false; - renderNavbar.rendered = false; + if (!OPTIONS$1.sidebar && !OPTIONS$1.loadSidebar) { renderSidebar(); } - if (content && typeof Vue !== 'undefined' && typeof Vuep !== 'undefined') { new Vue({ el: 'main' }); } // eslint-disable-line + if (content && typeof Vue !== 'undefined' && typeof Vuep !== 'undefined') { + var vm = new Vue({ el: 'main' }); // eslint-disable-line + vm.$nextTick(function (_) { return scrollActiveSidebar(); }); + } if (OPTIONS$1.auto2top) { scroll2Top(); } } @@ -2467,7 +2474,6 @@ function renderArticle (content) { function renderNavbar (content) { if (CACHE.navbar && CACHE.navbar === content) { return } CACHE.navbar = content; - renderNavbar.rendered = true; if (content) { renderTo('nav', marked(content)); } activeLink('nav'); @@ -2477,23 +2483,27 @@ function renderNavbar (content) { * sidebar */ function renderSidebar (content) { - var isToc = false; + var html; if (content) { - content = marked(content); + html = marked(content); } else if (OPTIONS$1.sidebar) { - content = tree(OPTIONS$1.sidebar, '