2016-11-20 15:56:37 +08:00
|
|
|
(function (global, factory) {
|
|
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('marked'), require('prismjs')) :
|
|
|
|
typeof define === 'function' && define.amd ? define(['marked', 'prismjs'], factory) :
|
|
|
|
(global.Docsify = factory(global.marked,global.Prism));
|
|
|
|
}(this, (function (marked,Prism) { 'use strict';
|
|
|
|
|
|
|
|
marked = 'default' in marked ? marked['default'] : marked;
|
|
|
|
Prism = 'default' in Prism ? Prism['default'] : Prism;
|
|
|
|
|
|
|
|
var ajax = function (url, options) {
|
|
|
|
if ( options === void 0 ) options = {};
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open(options.method || 'get', url);
|
|
|
|
xhr.send();
|
|
|
|
|
|
|
|
return {
|
|
|
|
then: function (cb) { return xhr.addEventListener('load', cb); }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-20 22:29:36 +08:00
|
|
|
/**
|
|
|
|
* @link from https://github.com/killercup/grock/blob/5280ae63e16c5739e9233d9009bc235ed7d79a50/styles/solarized/assets/js/behavior.coffee#L54-L81
|
|
|
|
*/
|
|
|
|
var tocToTree = function (toc) {
|
|
|
|
var headlines = [];
|
|
|
|
var last = {};
|
|
|
|
|
|
|
|
toc.forEach(function (headline) {
|
|
|
|
var level = headline.level || 1;
|
|
|
|
var len = level - 1;
|
|
|
|
|
|
|
|
if (last[len]) {
|
|
|
|
last[len].children = last[len].children || [];
|
|
|
|
last[len].children.push(headline);
|
|
|
|
} else {
|
|
|
|
headlines.push(headline);
|
|
|
|
last[level] = headline;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return headlines
|
|
|
|
};
|
|
|
|
|
|
|
|
var buildHeadlinesTree = function (tree, tpl) {
|
|
|
|
if ( tpl === void 0 ) tpl = '';
|
|
|
|
|
|
|
|
if (!tree || !tree.length) { return '' }
|
|
|
|
|
|
|
|
tree.forEach(function (node) {
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return tpl + '</ul>'
|
|
|
|
};
|
|
|
|
|
|
|
|
var genToc = function (toc) {
|
|
|
|
return buildHeadlinesTree(tocToTree(toc), '<ul>')
|
|
|
|
};
|
|
|
|
|
|
|
|
var toc = [];
|
2016-11-20 15:56:37 +08:00
|
|
|
var renderer = new marked.Renderer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* render anchor tag
|
|
|
|
* @link https://github.com/chjj/marked#overriding-renderer-methods
|
|
|
|
*/
|
|
|
|
renderer.heading = function (text, level) {
|
2016-11-20 22:29:36 +08:00
|
|
|
var slug = text.toLowerCase().replace(/[\s\n\t]+/g, '-');
|
2016-11-20 15:56:37 +08:00
|
|
|
|
2016-11-20 22:29:36 +08:00
|
|
|
toc.push({ level: level, slug: slug, title: text });
|
2016-11-20 15:56:37 +08:00
|
|
|
|
2016-11-20 22:29:36 +08:00
|
|
|
return ("<h" + level + " id=\"" + slug + "\"><a href=\"#" + slug + "\" class=\"anchor\"></a>" + text + "</h" + level + ">")
|
|
|
|
};
|
2016-11-20 15:56:37 +08:00
|
|
|
marked.setOptions({
|
2016-11-20 22:29:36 +08:00
|
|
|
renderer: renderer,
|
2016-11-20 15:56:37 +08:00
|
|
|
highlight: function highlight (code, lang) {
|
2016-11-20 22:29:36 +08:00
|
|
|
return Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup)
|
2016-11-20 15:56:37 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var render = function (content) {
|
2016-11-20 22:29:36 +08:00
|
|
|
var section = "<section class=\"content\">\n <article class=\"markdown-section\">" + (marked(content)) + "</article>\n </section>";
|
|
|
|
var sidebar = "<aside class=\"sidebar\">" + (genToc(toc)) + "</aside>";
|
|
|
|
|
|
|
|
return ("<main>" + sidebar + section + "</main>")
|
2016-11-20 15:56:37 +08:00
|
|
|
};
|
|
|
|
|
2016-11-20 22:29:36 +08:00
|
|
|
function scrollActiveSidebar () {
|
|
|
|
if (/mobile/i.test(navigator.userAgent)) { return }
|
|
|
|
|
|
|
|
var anchors = document.querySelectorAll('.anchor');
|
|
|
|
var nav = {};
|
|
|
|
var lis = document.querySelectorAll('.sidebar li');
|
|
|
|
var active = null;
|
2016-11-20 15:56:37 +08:00
|
|
|
|
2016-11-20 22:29:36 +08:00
|
|
|
for (var i = 0, len = lis.length; i < len; i += 1) {
|
|
|
|
var li = lis[i];
|
|
|
|
var a = li.querySelector('a');
|
|
|
|
|
|
|
|
nav[a.getAttribute('href').slice(1)] = li;
|
|
|
|
}
|
|
|
|
|
|
|
|
function highlight () {
|
|
|
|
for (var i = 0, len = anchors.length; i < len; i += 1) {
|
|
|
|
var node = anchors[i].parentNode;
|
|
|
|
var bcr = node.getBoundingClientRect();
|
|
|
|
|
|
|
|
if (bcr.top < 150 && bcr.bottom > 150) {
|
|
|
|
var li = nav[ node.id ];
|
|
|
|
if (li === active) { return }
|
|
|
|
if (active) { active.classList.remove('active'); }
|
|
|
|
|
|
|
|
li.classList.add('active');
|
|
|
|
active = li;
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.querySelector('main .content').addEventListener('scroll', highlight);
|
|
|
|
highlight();
|
|
|
|
|
|
|
|
function scrollIntoView () {
|
|
|
|
var id = window.location.hash.slice(1);
|
2016-11-22 22:46:44 +08:00
|
|
|
if (!id) { return }
|
2016-11-20 22:29:36 +08:00
|
|
|
var section = document.querySelector('#' + id);
|
|
|
|
|
|
|
|
if (section) { section.scrollIntoView(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('hashchange', scrollIntoView);
|
|
|
|
scrollIntoView();
|
|
|
|
}
|
|
|
|
|
|
|
|
var bindEvent = function () {
|
|
|
|
scrollActiveSidebar();
|
|
|
|
};
|
|
|
|
|
|
|
|
var DEFAULT_OPTS = {
|
|
|
|
el: '#app',
|
|
|
|
title: document.title,
|
|
|
|
sep: ' - '
|
|
|
|
};
|
|
|
|
|
|
|
|
var Docsify = function Docsify (opts) {
|
2016-11-20 15:56:37 +08:00
|
|
|
Docsify.installed = true;
|
|
|
|
|
2016-11-20 22:29:36 +08:00
|
|
|
this.replace = true;
|
|
|
|
this.opts = Object.assign({}, opts, DEFAULT_OPTS);
|
|
|
|
this.dom = document.querySelector(this.opts.el);
|
|
|
|
if (!this.dom) {
|
|
|
|
this.dom = document.body;
|
|
|
|
this.replace = false;
|
|
|
|
}
|
2016-11-20 15:56:37 +08:00
|
|
|
this.loc = document.location.pathname;
|
2016-11-20 22:29:36 +08:00
|
|
|
if (/\/$/.test(this.loc)) { this.loc += 'README'; }
|
2016-11-20 15:56:37 +08:00
|
|
|
this.load();
|
|
|
|
};
|
|
|
|
|
|
|
|
Docsify.prototype.load = function load () {
|
|
|
|
var this$1 = this;
|
|
|
|
|
|
|
|
ajax(((this.loc) + ".md")).then(function (res) {
|
|
|
|
var target = res.target;
|
|
|
|
if (target.status >= 400) {
|
|
|
|
this$1.render('not found');
|
|
|
|
} else {
|
|
|
|
this$1.render(res.target.response);
|
2016-11-20 22:29:36 +08:00
|
|
|
bindEvent();
|
2016-11-20 15:56:37 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Docsify.prototype.render = function render$1 (content) {
|
2016-11-20 22:29:36 +08:00
|
|
|
document.title = this.loc.slice(1) + this.opts.sep + this.opts.title;
|
|
|
|
this.dom[this.replace ? 'outerHTML' : 'innerHTML'] = render(content);
|
|
|
|
};
|
|
|
|
|
|
|
|
Docsify.use = function () {
|
|
|
|
var plugin = arguments[0];
|
|
|
|
if (typeof plugin === 'function') {
|
|
|
|
plugin.call(Docsify);
|
|
|
|
} else {
|
|
|
|
throw TypeError('[docsify] Invalid plugin ' + plugin.name)
|
|
|
|
}
|
2016-11-20 15:56:37 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('load', function () {
|
|
|
|
if (Docsify.installed) { return }
|
|
|
|
new Docsify();
|
|
|
|
});
|
|
|
|
|
|
|
|
return Docsify;
|
|
|
|
|
|
|
|
})));
|