diff --git a/docs/_coverpage.md b/docs/_coverpage.md index 0a84e863..74b8f328 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,6 +1,6 @@ ![logo](_media/icon.svg) -# docsify 2.2.1 +# docsify 2.3.0 > A magical documentation site generator. diff --git a/lib/docsify.js b/lib/docsify.js index 74e094ee..cacdf375 100644 --- a/lib/docsify.js +++ b/lib/docsify.js @@ -176,7 +176,6 @@ function emojify (text) { } - var utils = Object.freeze({ load: load, genTree: genTree, @@ -208,7 +207,9 @@ function scrollActiveSidebar () { for (var i = 0, len = lis.length; i < len; i += 1) { var li = lis[i]; - var href = li.querySelector('a').getAttribute('href'); + var a = li.querySelector('a'); + if (!a) { continue } + var href = a.getAttribute('href'); if (href !== '/') { var match = href.match('#([^#]+)$'); @@ -322,10 +323,12 @@ function bindToggle (dom) { } } +var scrollingElement = document.scrollingElement || document.documentElement; + function scroll2Top (offset) { if ( offset === void 0 ) offset = 0; - document.body.scrollTop = offset === true ? 0 : Number(offset); + scrollingElement.scrollTop = offset === true ? 0 : Number(offset); } function sticky () { @@ -2543,6 +2546,7 @@ function cssVars () { var markdown = marked; var toc = []; var CACHE = {}; +var originTitle = document.title; var renderTo = function (dom, content) { dom = typeof dom === 'object' ? dom : document.querySelector(dom); @@ -2579,10 +2583,9 @@ function init () { return ("
" + hl + "
") }; renderer.link = function (href, title, text) { - if (!/:/.test(href)) { + if (!/:|(\/{2})/.test(href)) { href = ("#/" + href).replace(/\/+/g, '/'); } - return ("" + text + "") }; renderer.paragraph = function (text) { @@ -2593,11 +2596,19 @@ function init () { } return ("

" + text + "

") }; + renderer.image = function (href, title, text) { + var url = /:|(\/{2})/.test(href) ? href : ($docsify.basePath + href).replace(/\/+/g, '/'); + var titleHTML = title ? (" title=\"" + title + "\"") : ''; + + return ("\""") + }; if (typeof $docsify.markdown === 'function') { - markdown.setOptions({ renderer: renderer }); - markdown = $docsify.markdown.call(this, markdown); + markdown = $docsify.markdown.call(this, markdown, renderer); } else { + if ($docsify.markdown && $docsify.markdown.renderer) { + $docsify.markdown.renderer = merge(renderer, $docsify.markdown.renderer); + } markdown.setOptions(merge({ renderer: renderer }, $docsify.markdown)); } @@ -2653,25 +2664,34 @@ function renderApp (dom, replace) { * article */ function renderArticle (content) { - renderTo('article', content ? markdown(content) : 'not found'); - if (!$docsify.loadSidebar) { renderSidebar(); } + var hook = window.Docsify.hook; + var renderFn = function (data) { + renderTo('article', data); + if (!$docsify.loadSidebar) { renderSidebar(); } - if (content && typeof Vue !== 'undefined') { - CACHE.vm && CACHE.vm.$destroy(); + if (data && typeof Vue !== 'undefined') { + CACHE.vm && CACHE.vm.$destroy(); - var script = [].slice.call( - document.body.querySelectorAll('article>script')) - .filter(function (script) { return !/template/.test(script.type); } - )[0]; - var code = script ? script.innerText.trim() : null; + var script = [].slice.call( + document.body.querySelectorAll('article>script')) + .filter(function (script) { return !/template/.test(script.type); } + )[0]; + var code = script ? script.innerText.trim() : null; - script && script.remove(); - CACHE.vm = code - ? new Function(("return " + code))() - : new Vue({ el: 'main' }); // eslint-disable-line - CACHE.vm && CACHE.vm.$nextTick(function (_) { return scrollActiveSidebar(); }); - } - if ($docsify.auto2top) { setTimeout(function () { return scroll2Top($docsify.auto2top); }, 0); } + script && script.remove(); + CACHE.vm = code + ? new Function(("return " + code))() + : new Vue({ el: 'main' }); // eslint-disable-line + CACHE.vm && CACHE.vm.$nextTick(function (_) { return scrollActiveSidebar(); }); + } + if ($docsify.auto2top) { setTimeout(function () { return scroll2Top($docsify.auto2top); }, 0); } + }; + + hook.emit('before', content, function (result) { + var html = result ? markdown(result) : ''; + + hook.emit('after', html, function (result) { return renderFn(result || 'not found'); }); + }); } /** @@ -2700,15 +2720,21 @@ function renderSidebar (content) { } renderTo('.sidebar-nav', html); + + if (toc[0] && toc[0].level === 1) { + document.title = "" + (toc[0].title) + (originTitle ? ' - ' + originTitle : ''); + } + var target = activeLink('.sidebar-nav', true); if (target) { renderSubSidebar(target); } - toc = []; + toc = []; scrollActiveSidebar(); } function renderSubSidebar (target) { if (!$docsify.subMaxLevel) { return } + toc[0] && toc[0].level === 1 && toc.shift(); target.parentNode.innerHTML += tree(genTree(toc, $docsify.subMaxLevel), '