diff --git a/CHANGELOG.md b/CHANGELOG.md
index c6a5869a1..a21a9ac7f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,9 +1,13 @@
+## 0.5.0
+### Features
+- Custom sidebars and navbars by markdown file
+
## 0.4.2
-## Bug fixes
+### Bug fixes
- Correct catch ajax error
## 0.4.1
-## Bug fixes
+### Bug fixes
- catch ajax error
## 0.4.0
diff --git a/build/build.js b/build/build.js
index b6862edff..f8a69229c 100644
--- a/build/build.js
+++ b/build/build.js
@@ -15,11 +15,14 @@ var build = function (opts) {
console.log(dest)
bundle.write({
- format: 'umd',
+ format: 'iife',
moduleName: opts.moduleName || 'Docsify',
dest: dest
})
})
+ .catch(function (err) {
+ console.error(err)
+ })
}
build({
diff --git a/docs/README.md b/docs/README.md
index 5b439d335..1be47040d 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -144,3 +144,60 @@ Custom sidebar. if it'set, the TOC will be disabeld. Bind global variables on th
```
+
+#### load-sidebar
+
+Load sidebar markdown file. If it is configured, load the current directory `_sidebar.md` by default. If the file isn't exist, sidebar will appear TOC.
+
+```html
+
+```
+
+You can specify a file:
+
+```html
+
+```
+
+The contents of the file can be:
+
+```markdown
+- [Home](/)
+- [Installation](/installation)
+- Essentials
+ - [Getting Started](/getting-started)
+ - [Dynamic Route Matching](/dynamic-matching)
+ - [Nested Routes](/nested-routes)
+ - [Programmatic Navigation](/navigation)
+ - [Named Routes](/named-routes)
+ - [Named Views](/named-views)
+ - [Redirect and Alias](/redirect-and-alias)
+ - [HTML5 History Mode](/history-mode)
+```
+
+#### load-navbar
+
+Load navbar markdown file. If it is configured, load the current directory `_navbar.md` by default.
+
+```html
+
+```
+
+You can specify a file:
+
+```html
+
+```
+
+The contents of the file can be:
+
+```markdown
+- [en](/)
+- [chinese](/zh-cn)
+```
+
+## FAQ
+
+### Why use `404.html` instead of `index.html`
+
+[issues/7](https://github.com/QingWei-Li/docsify/issues/7)
diff --git a/docs/zh-cn.md b/docs/zh-cn.md
index 7c1baa7e5..a664c4e22 100644
--- a/docs/zh-cn.md
+++ b/docs/zh-cn.md
@@ -142,3 +142,63 @@ docsify serve docs
```
+#### load-sidebar
+
+读取侧边栏配置文件,如果配置,默认加载当前目录下的 `_sidebar.md`。如果文件不存在,会显示 TOC 作为侧边栏内容。如果你有二级目录,也应该放置一份配置文件。
+
+```html
+
+```
+
+你可以指定侧边栏文件名
+
+```html
+
+```
+
+`_sidebar.md` 的内容可以是这样的
+
+```markdown
+- [Home](/)
+- [Installation](/installation)
+- Essentials
+ - [Getting Started](/getting-started)
+ - [Dynamic Route Matching](/dynamic-matching)
+ - [Nested Routes](/nested-routes)
+ - [Programmatic Navigation](/navigation)
+ - [Named Routes](/named-routes)
+ - [Named Views](/named-views)
+ - [Redirect and Alias](/redirect-and-alias)
+ - [HTML5 History Mode](/history-mode)
+```
+
+#### load-navbar
+
+读取导航配置文件,如果配置,默认加载当前目录下的 `_navbar.md`。如果文件不存在,会显示 html 里定义的导航栏。
+
+```html
+
+```
+
+你可以指定导航栏文件名
+
+```html
+
+```
+
+`_navbar.md` 的内容可以是这样
+
+```markdown
+- [en](/)
+- [中文](/zh-cn)
+```
+
+## FAQ
+
+### 为什么是 `404.html` 而不用 `index.html`
+
+docsify 想要实现的是用最简单的方式 **动态渲染内容**。
+
+例如我有两个文档分别为 `README.md` 和 `guide.md`,如果我用 `index.html` 作为文件名,`README.md` 可以被正确的渲染因为我们已经规定它为首页文件,但是如果我们访问 `my-domain.com/guide` 想要得到的结果是 `guide.md` 的内容,它将无法工作,因为目录下并不存在一个 `guide.html` 的文件。
+
+但是 GitHub Pages 服务器找不到资源, 就会回退并渲染 `404.html` 文件。😄
diff --git a/lib/docsify.js b/lib/docsify.js
index 38301bf8b..4057936e3 100644
--- a/lib/docsify.js
+++ b/lib/docsify.js
@@ -1,28 +1,80 @@
-(function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
- typeof define === 'function' && define.amd ? define(factory) :
- (global.Docsify = factory());
-}(this, (function () { 'use strict';
+var Docsify = (function () {
+'use strict';
-var ajax = function (url, options) {
- if ( options === void 0 ) options = {};
+/**
+ * Simple ajax
+ * @param {String} url
+ * @param {String} [method=get]
+ * @return {Promise}
+ */
+function load (url, method) {
+ if ( method === void 0 ) method = 'get';
var xhr = new XMLHttpRequest();
- xhr.open(options.method || 'get', url);
+ xhr.open(method, url);
xhr.send();
return {
- then: function (cb) {
- xhr.addEventListener('load', cb);
- return this
- },
- catch: function (cb) {
- xhr.addEventListener('error', cb);
- return this
+ then: function (success, error) {
+ if ( error === void 0 ) error = function () {};
+
+ xhr.addEventListener('error', error);
+ xhr.addEventListener('load', function (ref) {
+ var target = ref.target;
+
+ target.status >= 400 ? error(target) : success(target.response);
+ });
}
}
-};
+}
+
+/**
+ * gen toc tree
+ * @link https://github.com/killercup/grock/blob/5280ae63e16c5739e9233d9009bc235ed7d79a50/styles/solarized/assets/js/behavior.coffee#L54-L81
+ * @param {Array} toc
+ * @param {Number} maxLevel
+ * @return {Array}
+ */
+function genTree (toc, maxLevel) {
+ var headlines = [];
+ var last = {};
+
+ toc.forEach(function (headline) {
+ var level = headline.level || 1;
+ var len = level - 1;
+
+ if (level > maxLevel) { return }
+ if (last[len]) {
+ last[len].children = last[len].children || [];
+ last[len].children.push(headline);
+ } else {
+ headlines.push(headline);
+ }
+ last[level] = headline;
+ });
+
+ return headlines
+}
+
+/**
+ * camel to kebab
+ * @link https://github.com/bokuweb/kebab2camel/blob/master/index.js
+ * @param {String} str
+ * @return {String}
+ */
+function camel2kebab (str) {
+ return str.replace(/([A-Z])/g, function (m) { return '-' + m.toLowerCase(); })
+}
+
+/**
+ * is nil
+ * @param {Object} object
+ * @return {Boolean}
+ */
+function isNil (o) {
+ return o === null || o === undefined
+}
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
@@ -2119,96 +2171,52 @@ Prism.languages.js = Prism.languages.javascript;
});
/**
- * @link from https://github.com/killercup/grock/blob/5280ae63e16c5739e9233d9009bc235ed7d79a50/styles/solarized/assets/js/behavior.coffee#L54-L81
+ * Render github corner
+ * @param {Object} data
+ * @return {String}
*/
-var tocToTree = function (toc, maxLevel) {
- var headlines = [];
- var last = {};
+function corner (data) {
+ if (!data) { return '' }
+ if (!/\/\//.test(data)) { data = 'https://github.com/' + data; }
- toc.forEach(function (headline) {
- var level = headline.level || 1;
- var len = level - 1;
-
- if (level > maxLevel) { return }
- if (last[len]) {
- last[len].children = last[len].children || [];
- last[len].children.push(headline);
- } else {
- headlines.push(headline);
- }
- last[level] = headline;
- });
+ return ("\n \n \n ")
+}
- return headlines
-};
+/**
+ * Render main content
+ * @return {[type]} [description]
+ */
+function main () {
+ return "
" + hl + "
")
-};
-marked.setOptions({ renderer: renderer });
-
-var render = function (content, opts) {
- if ( opts === void 0 ) opts = {};
-
- var corner = '';
-
- if (opts.repo) {
- var repo = /\/\//.test(opts.repo) ? opts.repo : ('https://github.com/' + opts.repo);
- corner = cornerTpl.replace(/{{repo}}/g, repo);
- }
-
- var section = "" + hl + "
")
+};
+marked.setOptions({ renderer: renderer });
+
+/**
+ * App
+ */
+function renderApp (dom, replace, opts) {
+ var nav = document.querySelector('nav') || document.createElement('nav');
+
+ dom[replace ? 'outerHTML' : 'innerHTML'] = corner(opts.repo) + main();
+ document.body.insertBefore(nav, document.body.children[0]);
}
-var Docsify = function Docsify (opts) {
- Docsify.installed = true;
+/**
+ * article
+ */
+function renderArticle (content) {
+ if ( content === void 0 ) content = 'not found';
- 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]; }
+ renderTo('article', marked(content));
+ if (!renderSidebar.rendered) { renderSidebar(null); }
+ if (!renderNavbar.rendered) { renderNavbar(null); }
+}
- this.loc = document.location.pathname;
- if (/\/$/.test(this.loc)) { this.loc += 'README'; }
+/**
+ * navbar
+ */
+function renderNavbar (content, OPTIONS) {
+ if ( OPTIONS === void 0 ) OPTIONS = {};
- this.load();
+ renderNavbar.rendered = true;
- var nav = document.querySelector('nav');
- if (nav) { this.activeNav(nav); }
-};
+ if (content) { renderTo('nav', marked(content)); }
+ activeLink('nav');
+}
-Docsify.prototype.load = function load () {
- var this$1 = this;
+/**
+ * sidebar
+ */
+function renderSidebar (content, OPTIONS) {
+ if ( OPTIONS === void 0 ) OPTIONS = {};
- 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);
- bindEvent(!!this$1.opts.sidebar);
- if (this$1.opts.sidebar) {
- this$1.activeNav(document.querySelector('aside.sidebar'), true);
- }
- }
- })
- .catch(function (_) { return this$1.render('not found'); });
-};
+ renderSidebar.rendered = true;
+
+ var isToc = false;
+
+ if (content) {
+ content = marked(content);
+ } else if (OPTIONS.sidebar) {
+ content = tree(OPTIONS.sidebar, '"+a(e.message+"",!0)+"";throw e}}var h={newline:/^\n+/,code:/^( {4}[^\n]+\n*)+/,fences:u,hr:/^( *[-*_]){3,} *(?:\n+|$)/,heading:/^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,nptable:u,lheading:/^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,blockquote:/^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,list:/^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,html:/^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,def:/^ *\[([^\]]+)\]: *([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,table:u,paragraph:/^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,text:/^[^\n]+/};h.bullet=/(?:[*+-]|\d+\.)/,h.item=/^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/,h.item=l(h.item,"gm")(/bull/g,h.bullet)(),h.list=l(h.list)(/bull/g,h.bullet)("hr","\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))")("def","\\n+(?="+h.def.source+")")(),h.blockquote=l(h.blockquote)("def",h.def)(),h._tag="(?!(?:a|em|strong|small|s|cite|q|dfn|abbr|data|time|code|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b",h.html=l(h.html)("comment",//)("closed",/<(tag)[\s\S]+?<\/\1>/)("closing",/
1&&o.length>1||(e=s.slice(c+1).join("\n")+e,c=p-1)),i=r||/\n\n(?!\s*$)/.test(l),c!==p-1&&(r="\n"===l.charAt(l.length-1),i||(i=r)),g.tokens.push({type:i?"loose_item_start":"list_item_start"}),g.token(l,!1,n),g.tokens.push({type:"list_item_end"});g.tokens.push({type:"list_end"})}else if(s=g.rules.html.exec(e))e=e.substring(s[0].length),g.tokens.push({type:g.options.sanitize?"paragraph":"html",pre:!g.options.sanitizer&&("pre"===s[1]||"script"===s[1]||"style"===s[1]),text:s[0]});else if(!n&&t&&(s=g.rules.def.exec(e)))e=e.substring(s[0].length),g.tokens.links[s[1].toLowerCase()]={href:s[2],title:s[3]};else if(t&&(s=g.rules.table.exec(e))){for(e=e.substring(s[0].length),l={type:"table",header:s[1].replace(/^ *| *\| *$/g,"").split(/ *\| */),align:s[2].replace(/^ *|\| *$/g,"").split(/ *\| */),cells:s[3].replace(/(?: *\| *)?\n$/,"").split("\n")},c=0;c "+e+"
\n":"'+(n?e:a(e,!0))+"\n
"},i.prototype.blockquote=function(e){return""+(n?e:a(e,!0))+"\n
\n"+e+"
\n"},i.prototype.html=function(e){return e},i.prototype.heading=function(e,t,n){return"
\n":"
\n"},i.prototype.list=function(e,t){var n=t?"ol":"ul";return"<"+n+">\n"+e+""+n+">\n"},i.prototype.listitem=function(e){return"\n\n"+e+"\n\n"+t+"\n
\n"},i.prototype.tablerow=function(e){return"\n"+e+" \n"},i.prototype.tablecell=function(e,t){var n=t.header?"th":"td",r=t.align?"<"+n+' style="text-align:'+t.align+'">':"<"+n+">";return r+e+""+n+">\n"},i.prototype.strong=function(e){return""+e+""},i.prototype.em=function(e){return""+e+""},i.prototype.codespan=function(e){return""+e+"
"},i.prototype.br=function(){return this.options.xhtml?"
":"
"},i.prototype.del=function(e){return""+e+""},i.prototype.link=function(e,t,n){if(this.options.sanitize){try{var r=decodeURIComponent(o(e)).replace(/[^\w:]/g,"").toLowerCase()}catch(e){return""}if(0===r.indexOf("javascript:")||0===r.indexOf("vbscript:"))return""}var i='"+n+""},i.prototype.image=function(e,t,n){var r='":">"},i.prototype.text=function(e){return e},s.parse=function(e,t,n){var r=new s(t,n);return r.parse(e)},s.prototype.parse=function(e){var t=this;this.inline=new r(e.links,this.options,this.renderer),this.tokens=e.reverse();for(var n="";this.next();)n+=t.tok();return n},s.prototype.next=function(){return this.token=this.tokens.pop()},s.prototype.peek=function(){return this.tokens[this.tokens.length-1]||0},s.prototype.parseText=function(){for(var e=this,t=this.token.text;"text"===this.peek().type;)t+="\n"+e.next().text;return this.inline.output(t)},s.prototype.tok=function(){var e=this;switch(this.token.type){case"space":return"";case"hr":return this.renderer.hr();case"heading":return this.renderer.heading(this.inline.output(this.token.text),this.token.depth,this.token.text);case"code":return this.renderer.code(this.token.text,this.token.lang,this.token.escaped);case"table":var t,n,r,i,s,a="",o="";for(r="",t=0;t