bump 0.4.0

This commit is contained in:
qingwei.li 2016-11-27 18:56:25 +08:00
commit ea03583d3d
2 changed files with 28 additions and 15 deletions

View file

@ -2141,7 +2141,7 @@ var buildHeadlinesTree = function (tree, tpl) {
if (!tree || !tree.length) { return '' } if (!tree || !tree.length) { return '' }
tree.forEach(function (node) { tree.forEach(function (node) {
tpl += "<li><a class=\"section-link\" href=\"#" + (node.slug) + "\">" + (node.title) + "</a></li>"; tpl += "<li><a class=\"section-link\" href=\"" + (node.slug) + "\">" + (node.title) + "</a></li>";
if (node.children) { if (node.children) {
tpl += "<li><ul class=\"children\">" + (buildHeadlinesTree(node.children)) + "</li></ul>"; tpl += "<li><ul class=\"children\">" + (buildHeadlinesTree(node.children)) + "</li></ul>";
} }
@ -2150,8 +2150,11 @@ var buildHeadlinesTree = function (tree, tpl) {
return tpl return tpl
}; };
var genToc = function (toc, maxLevel) { var genToc = function (toc, opts) {
var tree = tocToTree(toc, maxLevel); var tree = Array.isArray(opts.sidebar)
? opts.sidebar
: tocToTree(toc, opts['max-level']);
return buildHeadlinesTree(tree, '<ul>') return buildHeadlinesTree(tree, '<ul>')
}; };
@ -2166,13 +2169,15 @@ var renderer = new marked.Renderer();
renderer.heading = function (text, level) { renderer.heading = function (text, level) {
var slug = text.replace(/<(?:.|\n)*?>/gm, '').toLowerCase().replace(/[\s\n\t]+/g, '-'); var slug = text.replace(/<(?:.|\n)*?>/gm, '').toLowerCase().replace(/[\s\n\t]+/g, '-');
toc.push({ level: level, slug: slug, title: text }); toc.push({ level: level, slug: '#' + slug, title: text });
return ("<h" + level + " id=\"" + slug + "\"><a href=\"#" + slug + "\" class=\"anchor\"></a>" + text + "</h" + level + ">") return ("<h" + level + " id=\"" + slug + "\"><a href=\"#" + slug + "\" class=\"anchor\"></a>" + text + "</h" + level + ">")
}; };
// highlight code // highlight code
renderer.code = function (code, lang) { renderer.code = function (code, lang) {
if ( lang === void 0 ) lang = '';
var hl = prism.highlight(code, prism.languages[lang] || prism.languages.markup); var hl = prism.highlight(code, prism.languages[lang] || prism.languages.markup);
return ("<pre data-lang=\"" + lang + "\"><code class=\"lang-" + lang + "\">" + hl + "</code></pre>") return ("<pre data-lang=\"" + lang + "\"><code class=\"lang-" + lang + "\">" + hl + "</code></pre>")
@ -2190,13 +2195,13 @@ var render = function (content, opts) {
} }
var section = "<section class=\"content\">\n <article class=\"markdown-section\">" + (marked(content)) + "</article>\n </section>"; var section = "<section class=\"content\">\n <article class=\"markdown-section\">" + (marked(content)) + "</article>\n </section>";
var sidebar = "<aside class=\"sidebar\">" + (genToc(toc, opts['max-level'])) + "</aside>"; var sidebar = "<aside class=\"sidebar\">" + (genToc(toc, opts)) + "</aside>";
return (corner + "<main>" + sidebar + section + "</main>") return (corner + "<main>" + sidebar + section + "</main>")
}; };
function scrollActiveSidebar () { function scrollActiveSidebar (isCustom) {
if (/mobile/i.test(navigator.userAgent)) { return } if (/mobile/i.test(navigator.userAgent) || isCustom) { return }
var anchors = document.querySelectorAll('.anchor'); var anchors = document.querySelectorAll('.anchor');
var nav = {}; var nav = {};
@ -2245,14 +2250,15 @@ function scrollActiveSidebar () {
scrollIntoView(); scrollIntoView();
} }
var bindEvent = function () { var bindEvent = function (isCustom) {
scrollActiveSidebar(); scrollActiveSidebar(isCustom);
}; };
var DEFAULT_OPTS = { var DEFAULT_OPTS = {
el: '#app', el: '#app',
repo: '', repo: '',
'max-level': 6 'max-level': 6,
sidebar: ''
}; };
var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop(); var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop();
@ -2267,15 +2273,17 @@ var Docsify = function Docsify (opts) {
Docsify.installed = true; Docsify.installed = true;
this.opts = Object.assign({}, opts, DEFAULT_OPTS); this.opts = Object.assign({}, opts, DEFAULT_OPTS);
this.replace = true; this.replace = true;
this.dom = document.querySelector(this.opts.el); this.dom = document.querySelector(this.opts.el);
if (!this.dom) { if (!this.dom) {
this.dom = document.body; this.dom = document.body;
this.replace = false; this.replace = false;
} }
if (this.opts.sidebar) { this.opts.sidebar = window[this.opts.sidebar]; }
this.loc = document.location.pathname; this.loc = document.location.pathname;
if (/\/$/.test(this.loc)) { this.loc += 'README'; } if (/\/$/.test(this.loc)) { this.loc += 'README'; }
this.load(); this.load();
var nav = document.querySelector('nav'); var nav = document.querySelector('nav');
@ -2291,7 +2299,10 @@ Docsify.prototype.load = function load () {
this$1.render('not found'); this$1.render('not found');
} else { } else {
this$1.render(res.target.response); this$1.render(res.target.response);
bindEvent(); bindEvent(!!this$1.opts.sidebar);
if (this$1.opts.sidebar) {
this$1.activeNav(document.querySelector('aside.sidebar'), true);
}
} }
}); });
}; };
@ -2300,10 +2311,12 @@ Docsify.prototype.render = function render$1 (content) {
this.dom[this.replace ? 'outerHTML' : 'innerHTML'] = render(content, this.opts); this.dom[this.replace ? 'outerHTML' : 'innerHTML'] = render(content, this.opts);
}; };
Docsify.prototype.activeNav = function activeNav (elm) { Docsify.prototype.activeNav = function activeNav (elm, activeParentNode) {
var host = document.location.origin + document.location.pathname;[].slice.call(elm.querySelectorAll('a')).forEach(function (node) { var host = document.location.origin + document.location.pathname;[].slice.call(elm.querySelectorAll('a')).forEach(function (node) {
if (node.href === host) { if (node.href === host) {
node.setAttribute('class', 'active'); activeParentNode
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active');
} }
}); });
}; };

2
lib/docsify.min.js vendored

File diff suppressed because one or more lines are too long