Skip to content

Commit ec8b17b

Browse files
committed
bump 2.3.0
1 parent bf559b4 commit ec8b17b

File tree

11 files changed

+186
-69
lines changed

11 files changed

+186
-69
lines changed

docs/_coverpage.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
![logo](_media/icon.svg)
22

3-
# docsify <small>2.2.1</small>
3+
# docsify <small>2.3.0</small>
44

55
> A magical documentation site generator.
66

lib/docsify.js

+147-45
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ function emojify (text) {
176176
}
177177

178178

179-
180179
var utils = Object.freeze({
181180
load: load,
182181
genTree: genTree,
@@ -208,7 +207,9 @@ function scrollActiveSidebar () {
208207

209208
for (var i = 0, len = lis.length; i < len; i += 1) {
210209
var li = lis[i];
211-
var href = li.querySelector('a').getAttribute('href');
210+
var a = li.querySelector('a');
211+
if (!a) { continue }
212+
var href = a.getAttribute('href');
212213

213214
if (href !== '/') {
214215
var match = href.match('#([^#]+)$');
@@ -322,10 +323,12 @@ function bindToggle (dom) {
322323
}
323324
}
324325

326+
var scrollingElement = document.scrollingElement || document.documentElement;
327+
325328
function scroll2Top (offset) {
326329
if ( offset === void 0 ) offset = 0;
327330

328-
document.body.scrollTop = offset === true ? 0 : Number(offset);
331+
scrollingElement.scrollTop = offset === true ? 0 : Number(offset);
329332
}
330333

331334
function sticky () {
@@ -2543,6 +2546,7 @@ function cssVars () {
25432546
var markdown = marked;
25442547
var toc = [];
25452548
var CACHE = {};
2549+
var originTitle = document.title;
25462550

25472551
var renderTo = function (dom, content) {
25482552
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
@@ -2579,10 +2583,9 @@ function init () {
25792583
return ("<pre v-pre data-lang=\"" + lang + "\"><code class=\"lang-" + lang + "\">" + hl + "</code></pre>")
25802584
};
25812585
renderer.link = function (href, title, text) {
2582-
if (!/:/.test(href)) {
2586+
if (!/:|(\/{2})/.test(href)) {
25832587
href = ("#/" + href).replace(/\/+/g, '/');
25842588
}
2585-
25862589
return ("<a href=\"" + href + "\" title=\"" + (title || '') + "\">" + text + "</a>")
25872590
};
25882591
renderer.paragraph = function (text) {
@@ -2593,11 +2596,19 @@ function init () {
25932596
}
25942597
return ("<p>" + text + "</p>")
25952598
};
2599+
renderer.image = function (href, title, text) {
2600+
var url = /:|(\/{2})/.test(href) ? href : ($docsify.basePath + href).replace(/\/+/g, '/');
2601+
var titleHTML = title ? (" title=\"" + title + "\"") : '';
2602+
2603+
return ("<img src=\"" + url + "\" alt=\"" + text + "\"" + titleHTML + " />")
2604+
};
25962605

25972606
if (typeof $docsify.markdown === 'function') {
2598-
markdown.setOptions({ renderer: renderer });
2599-
markdown = $docsify.markdown.call(this, markdown);
2607+
markdown = $docsify.markdown.call(this, markdown, renderer);
26002608
} else {
2609+
if ($docsify.markdown && $docsify.markdown.renderer) {
2610+
$docsify.markdown.renderer = merge(renderer, $docsify.markdown.renderer);
2611+
}
26012612
markdown.setOptions(merge({ renderer: renderer }, $docsify.markdown));
26022613
}
26032614

@@ -2653,25 +2664,34 @@ function renderApp (dom, replace) {
26532664
* article
26542665
*/
26552666
function renderArticle (content) {
2656-
renderTo('article', content ? markdown(content) : 'not found');
2657-
if (!$docsify.loadSidebar) { renderSidebar(); }
2658-
2659-
if (content && typeof Vue !== 'undefined') {
2660-
CACHE.vm && CACHE.vm.$destroy();
2661-
2662-
var script = [].slice.call(
2663-
document.body.querySelectorAll('article>script'))
2664-
.filter(function (script) { return !/template/.test(script.type); }
2665-
)[0];
2666-
var code = script ? script.innerText.trim() : null;
2667-
2668-
script && script.remove();
2669-
CACHE.vm = code
2670-
? new Function(("return " + code))()
2671-
: new Vue({ el: 'main' }); // eslint-disable-line
2672-
CACHE.vm && CACHE.vm.$nextTick(function (_) { return scrollActiveSidebar(); });
2673-
}
2674-
if ($docsify.auto2top) { setTimeout(function () { return scroll2Top($docsify.auto2top); }, 0); }
2667+
var hook = window.Docsify.hook;
2668+
var renderFn = function (data) {
2669+
renderTo('article', data);
2670+
if (!$docsify.loadSidebar) { renderSidebar(); }
2671+
2672+
if (data && typeof Vue !== 'undefined') {
2673+
CACHE.vm && CACHE.vm.$destroy();
2674+
2675+
var script = [].slice.call(
2676+
document.body.querySelectorAll('article>script'))
2677+
.filter(function (script) { return !/template/.test(script.type); }
2678+
)[0];
2679+
var code = script ? script.innerText.trim() : null;
2680+
2681+
script && script.remove();
2682+
CACHE.vm = code
2683+
? new Function(("return " + code))()
2684+
: new Vue({ el: 'main' }); // eslint-disable-line
2685+
CACHE.vm && CACHE.vm.$nextTick(function (_) { return scrollActiveSidebar(); });
2686+
}
2687+
if ($docsify.auto2top) { setTimeout(function () { return scroll2Top($docsify.auto2top); }, 0); }
2688+
};
2689+
2690+
hook.emit('before', content, function (result) {
2691+
var html = result ? markdown(result) : '';
2692+
2693+
hook.emit('after', html, function (result) { return renderFn(result || 'not found'); });
2694+
});
26752695
}
26762696

26772697
/**
@@ -2700,15 +2720,21 @@ function renderSidebar (content) {
27002720
}
27012721

27022722
renderTo('.sidebar-nav', html);
2723+
2724+
if (toc[0] && toc[0].level === 1) {
2725+
document.title = "" + (toc[0].title) + (originTitle ? ' - ' + originTitle : '');
2726+
}
2727+
27032728
var target = activeLink('.sidebar-nav', true);
27042729
if (target) { renderSubSidebar(target); }
2705-
toc = [];
27062730

2731+
toc = [];
27072732
scrollActiveSidebar();
27082733
}
27092734

27102735
function renderSubSidebar (target) {
27112736
if (!$docsify.subMaxLevel) { return }
2737+
toc[0] && toc[0].level === 1 && toc.shift();
27122738
target.parentNode.innerHTML += tree(genTree(toc, $docsify.subMaxLevel), '<ul>');
27132739
}
27142740

@@ -2787,6 +2813,57 @@ function renderLoading (ref) {
27872813
}
27882814
}
27892815

2816+
var Hook = function Hook () {
2817+
this.beforeHooks = [];
2818+
this.afterHooks = [];
2819+
this.initHooks = [];
2820+
this.readyHooks = [];
2821+
};
2822+
2823+
Hook.prototype.beforeEach = function beforeEach (fn) {
2824+
this.beforeHooks.push(fn);
2825+
};
2826+
2827+
Hook.prototype.afterEach = function afterEach (fn) {
2828+
this.afterHooks.push(fn);
2829+
};
2830+
2831+
Hook.prototype.init = function init (fn) {
2832+
this.initHooks.push(fn);
2833+
};
2834+
2835+
Hook.prototype.ready = function ready (fn) {
2836+
this.readyHooks.push(fn);
2837+
};
2838+
2839+
Hook.prototype.emit = function emit (name, data, next) {
2840+
var newData = data;
2841+
var queue = this[name + 'Hooks'];
2842+
var step = function (index) {
2843+
var hook = queue[index];
2844+
if (index >= queue.length) {
2845+
next && next(newData);
2846+
} else {
2847+
if (typeof hook === 'function') {
2848+
if (hook.length === 2) {
2849+
hook(data, function (result) {
2850+
newData = result;
2851+
step(index + 1);
2852+
});
2853+
} else {
2854+
var result = hook(data);
2855+
newData = result !== undefined ? result : newData;
2856+
step(index + 1);
2857+
}
2858+
} else {
2859+
step(index + 1);
2860+
}
2861+
}
2862+
};
2863+
2864+
step(0);
2865+
};
2866+
27902867
var OPTIONS = merge({
27912868
el: '#app',
27922869
repo: '',
@@ -2818,11 +2895,14 @@ if (script) {
28182895
if (OPTIONS.name === true) { OPTIONS.name = ''; }
28192896
}
28202897

2898+
var hook = new Hook();
2899+
28212900
// utils
28222901
window.$docsify = OPTIONS;
28232902
window.Docsify = {
28242903
installed: true,
2825-
utils: merge({}, utils)
2904+
utils: merge({}, utils),
2905+
hook: hook
28262906
};
28272907

28282908
// load options
@@ -2831,22 +2911,39 @@ init();
28312911
var cacheRoute = null;
28322912
var cacheXhr = null;
28332913

2914+
var getAlias = function (route) {
2915+
if (OPTIONS.alias[route]) {
2916+
return getAlias(OPTIONS.alias[route])
2917+
} else {
2918+
return route
2919+
}
2920+
};
2921+
28342922
var mainRender = function (cb) {
2835-
var route = OPTIONS.basePath + getRoute();
2923+
var page;
2924+
var route = getRoute();
28362925
if (cacheRoute === route) { return cb() }
28372926

2838-
var basePath = cacheRoute = route;
2927+
var basePath = cacheRoute = OPTIONS.basePath + route;
28392928

28402929
if (!/\//.test(basePath)) {
28412930
basePath = '';
28422931
} else if (basePath && !/\/$/.test(basePath)) {
28432932
basePath = basePath.match(/(\S*\/)[^\/]+$/)[1];
28442933
}
28452934

2846-
var page;
2935+
// replace route
2936+
route = '/' + route;
2937+
if (OPTIONS.alias && OPTIONS.alias[route]) {
2938+
route = getAlias(route);
2939+
} else {
2940+
route = (OPTIONS.basePath + route).replace(/\/+/, '/');
2941+
}
28472942

28482943
if (!route) {
28492944
page = OPTIONS.homepage || 'README.md';
2945+
} else if (/\.md$/.test(route)) {
2946+
page = route;
28502947
} else if (/\/$/.test(route)) {
28512948
page = route + "README.md";
28522949
} else {
@@ -2884,21 +2981,26 @@ var mainRender = function (cb) {
28842981
};
28852982

28862983
var Docsify = function () {
2887-
var dom = document.querySelector(OPTIONS.el) || document.body;
2888-
var replace = dom !== document.body;
2889-
var main = function () {
2890-
mainRender(function (_) {
2891-
scrollIntoView();
2892-
activeLink('nav')
2893-
;[].concat(window.$docsify.plugins).forEach(function (fn) { return fn && fn(); });
2894-
});
2895-
};
2984+
setTimeout(function (_) {
2985+
[].concat(OPTIONS.plugins).forEach(function (fn) { return typeof fn === 'function' && fn(hook); });
2986+
window.Docsify.hook.emit('init');
2987+
2988+
var dom = document.querySelector(OPTIONS.el) || document.body;
2989+
var replace = dom !== document.body;
2990+
var main = function () {
2991+
mainRender(function (_) {
2992+
scrollIntoView();
2993+
activeLink('nav');
2994+
});
2995+
};
28962996

2897-
// Render app
2898-
renderApp(dom, replace);
2899-
main();
2900-
if (!/^#\//.test(window.location.hash)) { window.location.hash = '#/'; }
2901-
window.addEventListener('hashchange', main);
2997+
// Render app
2998+
renderApp(dom, replace);
2999+
main();
3000+
if (!/^#\//.test(window.location.hash)) { window.location.hash = '#/'; }
3001+
window.addEventListener('hashchange', main);
3002+
window.Docsify.hook.emit('ready');
3003+
}, 0);
29023004
};
29033005

29043006
var index = Docsify();

lib/docsify.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/plugins/ga.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ function collect () {
2929
}
3030

3131
var install = function () {
32-
if (!window.Docsify || !window.Docsify.installed) {
33-
console.error('[Docsify] Please load docsify.js first.');
34-
return
35-
}
32+
if (install.installed) { return }
33+
install.installed = true;
3634

3735
if (!window.$docsify.ga) {
3836
console.error('[Docsify] ga is required.');
3937
return
4038
}
4139

42-
collect();
43-
window.$docsify.plugins = [].concat(window.$docsify.plugins, collect);
40+
window.$docsify.plugins = [].concat(function (hook) {
41+
hook.init(collect);
42+
hook.beforeEach(collect);
43+
}, window.$docsify.plugins);
4444
};
4545

4646
var ga = install();

lib/plugins/ga.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)