bump 1.0.0

This commit is contained in:
qingwei.li
2016-12-08 21:12:33 +08:00
parent 864935bfc1
commit 41be8176a6
5 changed files with 108 additions and 44 deletions

View File

@@ -76,6 +76,29 @@ function isNil (o) {
return o === null || o === undefined
}
var cacheRoute$1 = null;
var cacheHash = null;
/**
* hash route
*/
function getRoute () {
var loc = window.location;
if (cacheHash === loc.hash && !isNil(cacheRoute$1)) { return cacheRoute$1 }
var route = loc.hash.match(/^#\/([^#]+)/);
if (route && route.length === 2) {
route = route[1];
} else {
route = /^#\//.test(loc.hash) ? '' : loc.pathname;
}
cacheRoute$1 = route;
cacheHash = loc.hash;
return route
}
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -2231,9 +2254,11 @@ function scrollActiveSidebar () {
for (var i = 0, len = lis.length; i < len; i += 1) {
var li = lis[i];
var a = li.querySelector('a');
var href = li.querySelector('a').getAttribute('href');
nav[a.getAttribute('href').slice(1)] = li;
if (href !== '/') { href = href.match(/#([^#]+)$/g)[0].slice(1); }
nav[href] = li;
}
function highlight () {
@@ -2244,8 +2269,7 @@ function scrollActiveSidebar () {
if (bcr.top < 10 && bcr.bottom > 10) {
var li = nav[node.id];
if (!li) { return }
if (li === active) { return }
if (!li || li === active) { return }
if (active) { active.setAttribute('class', ''); }
li.setAttribute('class', 'active');
@@ -2260,9 +2284,9 @@ function scrollActiveSidebar () {
highlight();
function scrollIntoView () {
var id = window.location.hash.slice(1);
if (!id) { return }
var section = document.querySelector('#' + id);
var id = window.location.hash.match(/#[^#\/]+$/g);
if (!id || !id.length) { return }
var section = document.querySelector(id[0]);
if (section) { section.scrollIntoView(); }
}
@@ -2275,7 +2299,7 @@ function scrollActiveSidebar () {
* Acitve link
*/
function activeLink (dom, activeParent) {
var host = document.location.origin + document.location.pathname;
var host = window.location.href;
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
if (!dom) { return
@@ -2285,6 +2309,10 @@ function activeLink (dom, activeParent) {
activeParent
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active');
} else {
activeParent
? node.parentNode.removeAttribute('class')
: node.removeAttribute('class');
}
});
}
@@ -2305,6 +2333,8 @@ function bindToggle (dom) {
});
}
var OPTIONS$1 = {};
var renderTo = function (dom, content) {
dom = typeof dom === 'object' ? dom : document.querySelector(dom);
dom.innerHTML = content;
@@ -2319,11 +2349,16 @@ var renderer = new marked.Renderer();
* @link https://github.com/chjj/marked#overriding-renderer-methods
*/
renderer.heading = function (text, level) {
var slug = text.toLowerCase().replace(/<(?:.|\n)*?>/gm, '').replace(/[\s\n\t]+/g, '-');
var slug = text.toLowerCase().replace(/<(?:.|\n)*?>/gm, '').replace(/[^\w|\u4e00-\u9fa5]+/g, '-');
var route = '';
toc.push({ level: level, slug: '#' + slug, title: text });
if (OPTIONS$1.router) {
route = "#/" + (getRoute());
}
return ("<h" + level + " id=\"" + slug + "\"><a href=\"#" + slug + "\" class=\"anchor\"></a>" + text + "</h" + level + ">")
toc.push({ level: level, slug: (route + "#" + slug), title: text });
return ("<h" + level + " id=\"" + slug + "\"><a href=\"" + route + "#" + slug + "\" class=\"anchor\"></a>" + text + "</h" + level + ">")
};
// highlight code
renderer.code = function (code, lang) {
@@ -2333,15 +2368,22 @@ renderer.code = function (code, lang) {
return ("<pre data-lang=\"" + lang + "\"><code class=\"lang-" + lang + "\">" + hl + "</code></pre>")
};
renderer.link = function (href, title, text) {
if (OPTIONS$1.router && !/^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/.test(href)) {
href = !/^\/#/.test(href) ? ("#" + href) : href;
}
return ("<a href=\"" + href + "\" title=\"" + (title || '') + "\">" + text + "</a>")
};
marked.setOptions({ renderer: renderer });
/**
* App
*/
function renderApp (dom, replace, opts) {
function renderApp (dom, replace) {
var nav = document.querySelector('nav') || document.createElement('nav');
dom[replace ? 'outerHTML' : 'innerHTML'] = toggle(opts.sidebarToggle) + corner(opts.repo) + main();
dom[replace ? 'outerHTML' : 'innerHTML'] = toggle(OPTIONS$1.sidebarToggle) + corner(OPTIONS$1.repo) + main();
document.body.insertBefore(nav, document.body.children[0]);
// bind toggle
@@ -2351,18 +2393,18 @@ function renderApp (dom, replace, opts) {
/**
* article
*/
function renderArticle (content, OPTIONS) {
function renderArticle (content) {
renderTo('article', content ? marked(content) : 'not found');
if (!renderSidebar.rendered) { renderSidebar(null, OPTIONS); }
if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS); }
if (!renderSidebar.rendered) { renderSidebar(null, OPTIONS$1); }
if (!renderNavbar.rendered) { renderNavbar(null, OPTIONS$1); }
renderSidebar.rendered = false;
renderNavbar.rendered = false;
}
/**
* navbar
*/
function renderNavbar (content, OPTIONS) {
if ( OPTIONS === void 0 ) OPTIONS = {};
function renderNavbar (content) {
renderNavbar.rendered = true;
if (content) { renderTo('nav', marked(content)); }
@@ -2372,24 +2414,27 @@ function renderNavbar (content, OPTIONS) {
/**
* sidebar
*/
function renderSidebar (content, OPTIONS) {
if ( OPTIONS === void 0 ) OPTIONS = {};
function renderSidebar (content) {
renderSidebar.rendered = true;
var isToc = false;
if (content) {
content = marked(content);
} else if (OPTIONS.sidebar) {
content = tree(OPTIONS.sidebar, '<ul>');
} else if (OPTIONS$1.sidebar) {
content = tree(OPTIONS$1.sidebar, '<ul>');
} else {
content = tree(genTree(toc, OPTIONS.maxLevel), '<ul>');
content = tree(genTree(toc, OPTIONS$1.maxLevel), '<ul>');
isToc = true;
}
renderTo('aside.sidebar', content);
isToc ? scrollActiveSidebar() : activeLink('aside.sidebar', true);
toc = [];
}
function config (options) {
OPTIONS$1 = options;
}
var OPTIONS = {
@@ -2399,7 +2444,8 @@ var OPTIONS = {
sidebar: '',
sidebarToggle: false,
loadSidebar: null,
loadNavbar: null
loadNavbar: null,
router: false
};
var script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop();
@@ -2414,31 +2460,48 @@ if (script) {
if (OPTIONS.sidebar) { OPTIONS.sidebar = window[OPTIONS.sidebar]; }
}
var Docsify = function () {
var dom = document.querySelector(OPTIONS.el) || document.body;
var replace = dom !== document.body;
var loc = document.location.pathname;
// load options
config(OPTIONS);
if (/\/$/.test(loc)) { loc += 'README'; }
var cacheRoute = null;
// Render app
renderApp(dom, replace, OPTIONS);
var mainRender = function () {
var route = getRoute();
if (cacheRoute === route) { return }
var basePath = cacheRoute = route;
if (!/\//.test(basePath)) {
basePath = '';
} else if (basePath && !/\/$/.test(basePath)) {
basePath = basePath.match(/(\S+\/)[^\/]+$/)[1];
}
// Render markdown file
load((loc + ".md"))
.then(function (content) { return renderArticle(content, OPTIONS); },
function (_) { return renderArticle(null, OPTIONS); });
load((!route || /\/$/.test(route)) ? (route + "README.md") : (route + ".md"))
.then(renderArticle, function (_) { return renderArticle(null); });
// Render sidebar
if (OPTIONS.loadSidebar) {
load(OPTIONS.loadSidebar)
.then(function (content) { return renderSidebar(content, OPTIONS); });
load(basePath + OPTIONS.loadSidebar).then(renderSidebar);
}
// Render navbar
if (OPTIONS.loadNavbar) {
load(OPTIONS.loadNavbar)
.then(function (content) { return renderNavbar(content, OPTIONS); });
load(basePath + OPTIONS.loadNavbar).then(renderNavbar);
}
};
var Docsify = function () {
var dom = document.querySelector(OPTIONS.el) || document.body;
var replace = dom !== document.body;
// Render app
renderApp(dom, replace);
mainRender();
if (OPTIONS.router) {
if (!/^#\//.test(window.location.hash)) { window.location.hash = '#/'; }
window.addEventListener('hashchange', mainRender);
}
};