bump 1.4.0
This commit is contained in:
parent
b287f57c86
commit
3069ad3928
4 changed files with 75 additions and 55 deletions
122
lib/docsify.js
122
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 ("<section class=\"cover\" style=\"background: " + bgc + "\">\n <div class=\"cover-main\"></div>\n </section>")
|
||||
return ("<section class=\"cover\" style=\"background: " + bgc + "\">\n <div class=\"cover-main\"></div>\n <div class=\"mask\"></div>\n </section>")
|
||||
}
|
||||
|
||||
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, '<ul>');
|
||||
html = tree(OPTIONS$1.sidebar, '<ul>');
|
||||
} else {
|
||||
content = tree(genTree(toc, OPTIONS$1.maxLevel), '<ul>');
|
||||
isToc = true;
|
||||
html = tree(genTree(toc, OPTIONS$1.maxLevel), '<ul>');
|
||||
}
|
||||
|
||||
renderSidebar.rendered = true;
|
||||
if (CACHE.sidebar && CACHE.sidebar === content) { return }
|
||||
CACHE.sidebar = content;
|
||||
renderTo('aside.sidebar', content);
|
||||
if (isToc) { scrollActiveSidebar(); }
|
||||
renderTo('aside.sidebar', html);
|
||||
var target = activeLink('aside.sidebar', true);
|
||||
if (content) { renderSubSidebar(target); }
|
||||
toc = [];
|
||||
|
||||
scrollActiveSidebar();
|
||||
}
|
||||
|
||||
function renderSubSidebar (target) {
|
||||
if (!OPTIONS$1.subMaxLevel) { return }
|
||||
target.parentNode.innerHTML += tree(genTree(toc, OPTIONS$1.subMaxLevel), '<ul>');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -2502,12 +2512,31 @@ function renderSidebar (content) {
|
|||
function renderCover (content) {
|
||||
renderCover.dom = renderCover.dom || document.querySelector('section.cover');
|
||||
if (!content) {
|
||||
renderCover.dom.classList.add('hidden');
|
||||
} else {
|
||||
renderCover.dom.classList.remove('hidden');
|
||||
!renderCover.rendered && renderTo('.cover-main', marked(content));
|
||||
renderCover.rendered = true;
|
||||
renderCover.dom.classList.remove('show');
|
||||
return
|
||||
}
|
||||
renderCover.dom.classList.add('show');
|
||||
if (renderCover.rendered) { return }
|
||||
|
||||
// render cover
|
||||
var html = marked(content);
|
||||
var match = html.trim().match('<p><img[^s]+src="(.*?)"[^a]+alt="(.*?)"></p>$');
|
||||
|
||||
// render background
|
||||
if (match) {
|
||||
var coverEl = document.querySelector('section.cover');
|
||||
|
||||
if (match[2] === 'color') {
|
||||
coverEl.style.background = match[1];
|
||||
} else {
|
||||
coverEl.classList.add('has-mask');
|
||||
coverEl.style.backgroundImage = "url(" + (match[1]) + ")";
|
||||
}
|
||||
html = html.replace(match[0], '');
|
||||
}
|
||||
|
||||
renderTo('.cover-main', html);
|
||||
renderCover.rendered = true;
|
||||
|
||||
sticky();
|
||||
}
|
||||
|
|
@ -2561,6 +2590,7 @@ var OPTIONS = {
|
|||
el: '#app',
|
||||
repo: '',
|
||||
maxLevel: 6,
|
||||
subMaxLevel: 0,
|
||||
sidebar: '',
|
||||
sidebarToggle: false,
|
||||
loadSidebar: null,
|
||||
|
|
@ -2595,7 +2625,6 @@ var mainRender = function (cb) {
|
|||
var route = OPTIONS.basePath + getRoute();
|
||||
if (cacheRoute === route) { return cb() }
|
||||
|
||||
var wait;
|
||||
var basePath = cacheRoute = route;
|
||||
|
||||
if (!/\//.test(basePath)) {
|
||||
|
|
@ -2627,22 +2656,16 @@ var mainRender = function (cb) {
|
|||
if (OPTIONS.coverpage && page !== OPTIONS.homepage) { renderCover(); }
|
||||
// render sidebar
|
||||
if (OPTIONS.loadSidebar) {
|
||||
if (wait === false) { cb(); }
|
||||
wait = false;
|
||||
load(basePath + OPTIONS.loadSidebar)
|
||||
.then(function (result) {
|
||||
renderSidebar(result);
|
||||
cb();
|
||||
});
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
}, function (_) { return renderArticle(null); });
|
||||
|
||||
// Render sidebar
|
||||
if (OPTIONS.loadSidebar) {
|
||||
load(basePath + OPTIONS.loadSidebar).then(function (result) {
|
||||
renderSidebar(result);
|
||||
if (wait === false) { cb(); }
|
||||
wait = false;
|
||||
});
|
||||
}
|
||||
|
||||
// Render navbar
|
||||
if (OPTIONS.loadNavbar) {
|
||||
load(basePath + OPTIONS.loadNavbar).then(renderNavbar);
|
||||
|
|
@ -2653,10 +2676,7 @@ 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();
|
||||
});
|
||||
mainRender(function (_) { return scrollIntoView(); });
|
||||
};
|
||||
|
||||
// Render app
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue