From 41be8176a63d15c170d165c4669133bd42ae483a Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Thu, 8 Dec 2016 21:12:33 +0800 Subject: [PATCH] bump 1.0.0 --- lib/docsify.js | 143 +++++++++++++++++++++++++++++++------------ lib/docsify.min.js | 3 +- lib/themes/buble.css | 2 +- lib/themes/pure.css | 2 +- lib/themes/vue.css | 2 +- 5 files changed, 108 insertions(+), 44 deletions(-) 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 ("" + text + "") + toc.push({ level: level, slug: (route + "#" + slug), title: text }); + + return ("" + text + "") }; // highlight code renderer.code = function (code, lang) { @@ -2333,15 +2368,22 @@ renderer.code = function (code, lang) { 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, '