mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
bump 0.4.0
This commit is contained in:
parent
37e7984e7e
commit
ea03583d3d
@ -2141,7 +2141,7 @@ var buildHeadlinesTree = function (tree, tpl) {
|
||||
if (!tree || !tree.length) { return '' }
|
||||
|
||||
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) {
|
||||
tpl += "<li><ul class=\"children\">" + (buildHeadlinesTree(node.children)) + "</li></ul>";
|
||||
}
|
||||
@ -2150,8 +2150,11 @@ var buildHeadlinesTree = function (tree, tpl) {
|
||||
return tpl
|
||||
};
|
||||
|
||||
var genToc = function (toc, maxLevel) {
|
||||
var tree = tocToTree(toc, maxLevel);
|
||||
var genToc = function (toc, opts) {
|
||||
var tree = Array.isArray(opts.sidebar)
|
||||
? opts.sidebar
|
||||
: tocToTree(toc, opts['max-level']);
|
||||
|
||||
return buildHeadlinesTree(tree, '<ul>')
|
||||
};
|
||||
|
||||
@ -2166,13 +2169,15 @@ var renderer = new marked.Renderer();
|
||||
renderer.heading = function (text, level) {
|
||||
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 + ">")
|
||||
};
|
||||
|
||||
// highlight code
|
||||
renderer.code = function (code, lang) {
|
||||
if ( lang === void 0 ) lang = '';
|
||||
|
||||
var hl = prism.highlight(code, prism.languages[lang] || prism.languages.markup);
|
||||
|
||||
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 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>")
|
||||
};
|
||||
|
||||
function scrollActiveSidebar () {
|
||||
if (/mobile/i.test(navigator.userAgent)) { return }
|
||||
function scrollActiveSidebar (isCustom) {
|
||||
if (/mobile/i.test(navigator.userAgent) || isCustom) { return }
|
||||
|
||||
var anchors = document.querySelectorAll('.anchor');
|
||||
var nav = {};
|
||||
@ -2245,14 +2250,15 @@ function scrollActiveSidebar () {
|
||||
scrollIntoView();
|
||||
}
|
||||
|
||||
var bindEvent = function () {
|
||||
scrollActiveSidebar();
|
||||
var bindEvent = function (isCustom) {
|
||||
scrollActiveSidebar(isCustom);
|
||||
};
|
||||
|
||||
var DEFAULT_OPTS = {
|
||||
el: '#app',
|
||||
repo: '',
|
||||
'max-level': 6
|
||||
'max-level': 6,
|
||||
sidebar: ''
|
||||
};
|
||||
|
||||
var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop();
|
||||
@ -2267,15 +2273,17 @@ var Docsify = function Docsify (opts) {
|
||||
Docsify.installed = true;
|
||||
|
||||
this.opts = Object.assign({}, opts, DEFAULT_OPTS);
|
||||
|
||||
this.replace = true;
|
||||
this.dom = document.querySelector(this.opts.el);
|
||||
if (!this.dom) {
|
||||
this.dom = document.body;
|
||||
this.replace = false;
|
||||
}
|
||||
if (this.opts.sidebar) { this.opts.sidebar = window[this.opts.sidebar]; }
|
||||
|
||||
this.loc = document.location.pathname;
|
||||
if (/\/$/.test(this.loc)) { this.loc += 'README'; }
|
||||
|
||||
this.load();
|
||||
|
||||
var nav = document.querySelector('nav');
|
||||
@ -2291,7 +2299,10 @@ Docsify.prototype.load = function load () {
|
||||
this$1.render('not found');
|
||||
} else {
|
||||
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);
|
||||
};
|
||||
|
||||
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) {
|
||||
if (node.href === host) {
|
||||
node.setAttribute('class', 'active');
|
||||
activeParentNode
|
||||
? node.parentNode.setAttribute('class', 'active')
|
||||
: node.setAttribute('class', 'active');
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
2
lib/docsify.min.js
vendored
2
lib/docsify.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user