Compare commits

..
11 Commits
Author SHA1 Message Date
qingwei.li 7c05d729b4 -> v0.4.1 2016-11-28 12:24:14 +08:00
qingwei.li 5953ef7739 bump 0.4.1 2016-11-28 12:23:43 +08:00
qingwei.li d556282471 add docsify.js.org
fix cdn
2016-11-28 12:22:47 +08:00
qingwei.li ec58149aad add catch ajax error 2016-11-28 12:17:57 +08:00
qingwei.li e15c53a6b8 fix site
remove cname

remove cdn

remove cdn
2016-11-28 12:14:24 +08:00
qingwei.li f450b118ee use docsify.js.org 2016-11-28 11:27:28 +08:00
qingwei.li 6ac7d4f66f Add changelog 2016-11-27 19:06:14 +08:00
qingwei.li 17806def5a -> v0.4.0 2016-11-27 18:57:06 +08:00
qingwei.li ea03583d3d bump 0.4.0 2016-11-27 18:56:25 +08:00
cinwell.liandGitHub 37e7984e7e feat: custom sidebar, #4 (#5)
* feat: custom sidebar, #4

* fix dev html

* fix doc

* fix doc
2016-11-27 18:53:31 +08:00
qingwei.li fc2e5b6fb9 fix undefined language 2016-11-27 15:24:07 +08:00
12 changed files with 147 additions and 38 deletions
+33
View File
@@ -0,0 +1,33 @@
## 0.4.1
## Bug fixes
- catch ajax error
## 0.4.0
### Features
- Custom sidebar
### Bug fixes
- Fix undefined language
## 0.3.1
### Bug fixes
- Strip HTML tag when rendering the headings
## 0.3.0
### Features
- Add minified css files
- Add max level option
- Add pure.css
## 0.2.1
### Bug fixes
- Fix vue.css
## 0.2.0
### Bug fixes
- Fix route
- Remove dynamic title
## 0.1.0
### Features
- Add buble.css
+1
View File
@@ -0,0 +1 @@
docsify.js.org
+23
View File
@@ -121,3 +121,26 @@ Root element.
<script src="//unpkg.com/docsify" data-el="#app"></script>
```
#### sidebar
Custom sidebar. if it'set, the TOC will be disabeld. Bind global variables on the `data-sidebar`.
![image](https://cloud.githubusercontent.com/assets/7565692/20647425/de5ab1c2-b4ce-11e6-863a-135868f2f9b4.png)
```html
<script>
window.sidebar = [
{ slug: '/', title: 'Home' },
{
slug: '/pageA',
title: 'page A',
children: [
{ slug: '/pageA/childrenB', title: 'children B' }
]
},
{ slug: '/PageC', title: 'Page C' }
]
</script>
<script src="/lib/docsify.js" data-sidebar="sidebar"></script>
```
+23 -1
View File
@@ -75,7 +75,7 @@ docsify serve docs
<link rel="stylesheet" href="//unpkg.com/docsify/themes/buble.css">
```
### 压缩版
压缩版
```html
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
@@ -119,4 +119,26 @@ docsify serve docs
<script src="//unpkg.com/docsify" data-el="#app"></script>
```
#### sidebar
设置后 TOC 功能将不可用,适合导航较多的文档,`data-sidebar` 传入全局变量名。
![image](https://cloud.githubusercontent.com/assets/7565692/20647425/de5ab1c2-b4ce-11e6-863a-135868f2f9b4.png)
```html
<script>
window.sidebar = [
{ slug: '/', title: 'Home' },
{
slug: '/pageA',
title: 'page A',
children: [
{ slug: '/pageA/childrenB', title: 'children B' }
]
},
{ slug: '/PageC', title: 'Page C' }
]
</script>
<script src="/lib/docsify.js" data-sidebar="sidebar"></script>
```
+33 -17
View File
@@ -13,7 +13,8 @@ var ajax = function (url, options) {
xhr.send();
return {
then: function (cb) { return xhr.addEventListener('load', cb); }
then: function (cb) { return xhr.addEventListener('load', cb); },
catch: function (cb) { return xhr.addEventListener('error', cb); }
}
};
@@ -2141,7 +2142,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 +2151,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 +2170,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 +2196,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 +2251,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 +2274,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');
@@ -2285,25 +2294,32 @@ var Docsify = function Docsify (opts) {
Docsify.prototype.load = function load () {
var this$1 = this;
ajax(((this.loc) + ".md")).then(function (res) {
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();
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'); });
};
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');
}
});
};
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docsify",
"version": "0.3.1",
"version": "0.4.1",
"description": "A magical documentation generator.",
"main": "lib/docsify.js",
"files": [
+2 -1
View File
@@ -5,6 +5,7 @@ export default function (url, options = {}) {
xhr.send()
return {
then: cb => xhr.addEventListener('load', cb)
then: cb => xhr.addEventListener('load', cb),
catch: cb => xhr.addEventListener('error', cb)
}
}
+4 -4
View File
@@ -1,5 +1,5 @@
function scrollActiveSidebar () {
if (/mobile/i.test(navigator.userAgent)) return
function scrollActiveSidebar (isCustom) {
if (/mobile/i.test(navigator.userAgent) || isCustom) return
const anchors = document.querySelectorAll('.anchor')
const nav = {}
@@ -48,6 +48,6 @@ function scrollActiveSidebar () {
scrollIntoView()
}
export default function () {
scrollActiveSidebar()
export default function (isCustom) {
scrollActiveSidebar(isCustom)
}
+7 -4
View File
@@ -25,8 +25,8 @@ const tocToTree = function (toc, maxLevel) {
const buildHeadlinesTree = function (tree, tpl = '') {
if (!tree || !tree.length) return ''
tree.forEach((node) => {
tpl += `<li><a class="section-link" href="#${node.slug}">${node.title}</a></li>`
tree.forEach(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></ul>`
}
@@ -35,7 +35,10 @@ const buildHeadlinesTree = function (tree, tpl = '') {
return tpl
}
export default function (toc, maxLevel) {
var tree = tocToTree(toc, maxLevel)
export default function (toc, opts) {
var tree = Array.isArray(opts.sidebar)
? opts.sidebar
: tocToTree(toc, opts['max-level'])
return buildHeadlinesTree(tree, '<ul>')
}
+16 -6
View File
@@ -5,7 +5,8 @@ import bindEvent from './bind-event'
const DEFAULT_OPTS = {
el: '#app',
repo: '',
'max-level': 6
'max-level': 6,
sidebar: ''
}
const script = document.currentScript || [].slice.call(document.getElementsByTagName('script')).pop()
@@ -21,15 +22,17 @@ class Docsify {
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()
const nav = document.querySelector('nav')
@@ -37,27 +40,34 @@ class Docsify {
}
load () {
ajax(`${this.loc}.md`).then(res => {
ajax(`${this.loc}.md`)
.then(res => {
const target = res.target
if (target.status >= 400) {
this.render('not found')
} else {
this.render(res.target.response)
bindEvent()
bindEvent(!!this.opts.sidebar)
if (this.opts.sidebar) {
this.activeNav(document.querySelector('aside.sidebar'), true)
}
}
})
.catch(_ => this.render('not found'))
}
render (content) {
this.dom[this.replace ? 'outerHTML' : 'innerHTML'] = render(content, this.opts)
}
activeNav (elm) {
activeNav (elm, activeParentNode) {
const host = document.location.origin + document.location.pathname
;[].slice.call(elm.querySelectorAll('a')).forEach(node => {
if (node.href === host) {
node.setAttribute('class', 'active')
activeParentNode
? node.parentNode.setAttribute('class', 'active')
: node.setAttribute('class', 'active')
}
})
}
+3 -3
View File
@@ -21,13 +21,13 @@ const renderer = new marked.Renderer()
renderer.heading = function (text, level) {
const slug = text.replace(/<(?:.|\n)*?>/gm, '').toLowerCase().replace(/[\s\n\t]+/g, '-')
toc.push({ level, slug, title: text })
toc.push({ 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) {
renderer.code = function (code, lang = '') {
const hl = Prism.highlight(code, Prism.languages[lang] || Prism.languages.markup)
return `<pre data-lang="${lang}"><code class="lang-${lang}">${hl}</code></pre>`
@@ -45,7 +45,7 @@ export default function (content, opts = {}) {
const section = `<section class="content">
<article class="markdown-section">${marked(content)}</article>
</section>`
const sidebar = `<aside class="sidebar">${genToc(toc, opts['max-level'])}</aside>`
const sidebar = `<aside class="sidebar">${genToc(toc, opts)}</aside>`
return `${corner}<main>${sidebar}${section}</main>`
}