Add chiniese doc

This commit is contained in:
qingwei.li 2016-11-26 17:24:32 +08:00
commit 1bf7b23d33
8 changed files with 242 additions and 42 deletions

View file

@ -18,12 +18,12 @@ function scrollActiveSidebar () {
const node = anchors[i].parentNode
const bcr = node.getBoundingClientRect()
if (bcr.top < 150 && bcr.bottom > 150) {
const li = nav[ node.id ]
if (bcr.top < 10 && bcr.bottom > 10) {
const li = nav[node.id]
if (li === active) return
if (active) active.classList.remove('active')
if (active) active.setAttribute('class', '')
li.classList.add('active')
li.setAttribute('class', 'active')
active = li
return

View file

@ -33,6 +33,9 @@ class Docsify {
this.loc = document.location.pathname
if (/\/$/.test(this.loc)) this.loc += 'README'
this.load()
const nav = document.querySelector('nav')
if (nav) this.activeNav(nav)
}
load () {
@ -48,9 +51,21 @@ class Docsify {
}
render (content) {
document.title = this.loc.slice(1) + this.opts.title
if (this.loc.slice(1) !== 'README') {
document.title = this.loc.slice(1) + this.opts.title
}
this.dom[this.replace ? 'outerHTML' : 'innerHTML'] = render(content, this.opts)
}
activeNav (elm) {
const host = document.location.origin + document.location.pathname
;[].slice.call(elm.querySelectorAll('a')).forEach(node => {
if (node.href === host) {
node.setAttribute('class', 'active')
}
})
}
}
window.addEventListener('load', () => {

View file

@ -14,8 +14,8 @@ const tocToTree = function (toc) {
last[len].children.push(headline)
} else {
headlines.push(headline)
last[level] = headline
}
last[level] = headline
})
return headlines
@ -27,13 +27,15 @@ const buildHeadlinesTree = function (tree, tpl = '') {
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>`
tpl += `<li><ul class="children">${buildHeadlinesTree(node.children)}</li></ul>`
}
})
return tpl + '</ul>'
return tpl
}
export default function (toc) {
return buildHeadlinesTree(tocToTree(toc), '<ul>')
var tree = tocToTree(toc)
var result = buildHeadlinesTree(tree, '<ul>')
return result
}