Add render sub menu
This commit is contained in:
parent
b9aeb4a922
commit
7ce166501b
3 changed files with 32 additions and 36 deletions
11
src/event.js
11
src/event.js
|
|
@ -16,7 +16,10 @@ export function scrollActiveSidebar () {
|
||||||
const li = lis[i]
|
const li = lis[i]
|
||||||
let href = li.querySelector('a').getAttribute('href')
|
let href = li.querySelector('a').getAttribute('href')
|
||||||
|
|
||||||
if (href !== '/') href = href.match(/#([^#]+)$/g)[0].slice(1)
|
if (href !== '/') {
|
||||||
|
const match = href.match('#([^#]+)$')
|
||||||
|
if (match && match.length) href = match[0].slice(1)
|
||||||
|
}
|
||||||
|
|
||||||
nav[decodeURIComponent(href)] = li
|
nav[decodeURIComponent(href)] = li
|
||||||
}
|
}
|
||||||
|
|
@ -61,18 +64,22 @@ export function activeLink (dom, activeParent) {
|
||||||
|
|
||||||
dom = typeof dom === 'object' ? dom : document.querySelector(dom)
|
dom = typeof dom === 'object' ? dom : document.querySelector(dom)
|
||||||
if (!dom) return
|
if (!dom) return
|
||||||
|
let target
|
||||||
|
|
||||||
;[].slice.call(dom.querySelectorAll('a')).forEach(node => {
|
;[].slice.call(dom.querySelectorAll('a')).forEach(node => {
|
||||||
if (node.href === host) {
|
if (node.href === host && !target) {
|
||||||
activeParent
|
activeParent
|
||||||
? node.parentNode.setAttribute('class', 'active')
|
? node.parentNode.setAttribute('class', 'active')
|
||||||
: node.setAttribute('class', 'active')
|
: node.setAttribute('class', 'active')
|
||||||
|
target = node
|
||||||
} else {
|
} else {
|
||||||
activeParent
|
activeParent
|
||||||
? node.parentNode.removeAttribute('class')
|
? node.parentNode.removeAttribute('class')
|
||||||
: node.removeAttribute('class')
|
: node.removeAttribute('class')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return target
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
25
src/index.js
25
src/index.js
|
|
@ -1,11 +1,12 @@
|
||||||
import { load, camel2kebab, isNil, getRoute } from './util'
|
import { load, camel2kebab, isNil, getRoute } from './util'
|
||||||
import { activeLink, scrollIntoView } from './event'
|
import { scrollIntoView } from './event'
|
||||||
import * as render from './render'
|
import * as render from './render'
|
||||||
|
|
||||||
const OPTIONS = {
|
const OPTIONS = {
|
||||||
el: '#app',
|
el: '#app',
|
||||||
repo: '',
|
repo: '',
|
||||||
maxLevel: 6,
|
maxLevel: 6,
|
||||||
|
maxSubLevel: 0,
|
||||||
sidebar: '',
|
sidebar: '',
|
||||||
sidebarToggle: false,
|
sidebarToggle: false,
|
||||||
loadSidebar: null,
|
loadSidebar: null,
|
||||||
|
|
@ -40,7 +41,6 @@ const mainRender = function (cb) {
|
||||||
const route = OPTIONS.basePath + getRoute()
|
const route = OPTIONS.basePath + getRoute()
|
||||||
if (cacheRoute === route) return cb()
|
if (cacheRoute === route) return cb()
|
||||||
|
|
||||||
let wait
|
|
||||||
let basePath = cacheRoute = route
|
let basePath = cacheRoute = route
|
||||||
|
|
||||||
if (!/\//.test(basePath)) {
|
if (!/\//.test(basePath)) {
|
||||||
|
|
@ -72,22 +72,16 @@ const mainRender = function (cb) {
|
||||||
if (OPTIONS.coverpage && page !== OPTIONS.homepage) render.renderCover()
|
if (OPTIONS.coverpage && page !== OPTIONS.homepage) render.renderCover()
|
||||||
// render sidebar
|
// render sidebar
|
||||||
if (OPTIONS.loadSidebar) {
|
if (OPTIONS.loadSidebar) {
|
||||||
if (wait === false) cb()
|
load(basePath + OPTIONS.loadSidebar)
|
||||||
wait = false
|
.then(result => {
|
||||||
|
render.renderSidebar(result)
|
||||||
|
cb()
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
}, _ => render.renderArticle(null))
|
}, _ => render.renderArticle(null))
|
||||||
|
|
||||||
// Render sidebar
|
|
||||||
if (OPTIONS.loadSidebar) {
|
|
||||||
load(basePath + OPTIONS.loadSidebar).then(result => {
|
|
||||||
render.renderSidebar(result)
|
|
||||||
if (wait === false) cb()
|
|
||||||
wait = false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render navbar
|
// Render navbar
|
||||||
if (OPTIONS.loadNavbar) {
|
if (OPTIONS.loadNavbar) {
|
||||||
load(basePath + OPTIONS.loadNavbar).then(render.renderNavbar)
|
load(basePath + OPTIONS.loadNavbar).then(render.renderNavbar)
|
||||||
|
|
@ -98,10 +92,7 @@ const Docsify = function () {
|
||||||
const dom = document.querySelector(OPTIONS.el) || document.body
|
const dom = document.querySelector(OPTIONS.el) || document.body
|
||||||
const replace = dom !== document.body
|
const replace = dom !== document.body
|
||||||
const main = function () {
|
const main = function () {
|
||||||
mainRender(_ => {
|
mainRender(_ => scrollIntoView())
|
||||||
activeLink('aside.sidebar', true)
|
|
||||||
scrollIntoView()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render app
|
// Render app
|
||||||
|
|
|
||||||
|
|
@ -78,10 +78,7 @@ export function renderApp (dom, replace) {
|
||||||
*/
|
*/
|
||||||
export function renderArticle (content) {
|
export function renderArticle (content) {
|
||||||
renderTo('article', content ? marked(content) : 'not found')
|
renderTo('article', content ? marked(content) : 'not found')
|
||||||
if (!renderSidebar.rendered) renderSidebar(null, OPTIONS)
|
if (!OPTIONS.sidebar && !OPTIONS.loadSidebar) renderSidebar()
|
||||||
if (!renderNavbar.rendered) renderNavbar(null, OPTIONS)
|
|
||||||
renderSidebar.rendered = false
|
|
||||||
renderNavbar.rendered = false
|
|
||||||
|
|
||||||
if (content && typeof Vue !== 'undefined' && typeof Vuep !== 'undefined') {
|
if (content && typeof Vue !== 'undefined' && typeof Vuep !== 'undefined') {
|
||||||
const vm = new Vue({ el: 'main' }) // eslint-disable-line
|
const vm = new Vue({ el: 'main' }) // eslint-disable-line
|
||||||
|
|
@ -96,7 +93,6 @@ export function renderArticle (content) {
|
||||||
export function renderNavbar (content) {
|
export function renderNavbar (content) {
|
||||||
if (CACHE.navbar && CACHE.navbar === content) return
|
if (CACHE.navbar && CACHE.navbar === content) return
|
||||||
CACHE.navbar = content
|
CACHE.navbar = content
|
||||||
renderNavbar.rendered = true
|
|
||||||
|
|
||||||
if (content) renderTo('nav', marked(content))
|
if (content) renderTo('nav', marked(content))
|
||||||
activeLink('nav')
|
activeLink('nav')
|
||||||
|
|
@ -106,25 +102,27 @@ export function renderNavbar (content) {
|
||||||
* sidebar
|
* sidebar
|
||||||
*/
|
*/
|
||||||
export function renderSidebar (content) {
|
export function renderSidebar (content) {
|
||||||
let isToc = false
|
let html
|
||||||
|
|
||||||
if (content) {
|
if (content) {
|
||||||
content = marked(content)
|
html = marked(content)
|
||||||
} else if (OPTIONS.sidebar) {
|
} else if (OPTIONS.sidebar) {
|
||||||
content = tpl.tree(OPTIONS.sidebar, '<ul>')
|
html = tpl.tree(OPTIONS.sidebar, '<ul>')
|
||||||
} else {
|
} else {
|
||||||
content = tpl.tree(genTree(toc, OPTIONS.maxLevel), '<ul>')
|
html = tpl.tree(genTree(toc, OPTIONS.maxLevel), '<ul>')
|
||||||
isToc = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
renderSidebar.rendered = true
|
renderTo('aside.sidebar', html)
|
||||||
if (CACHE.sidebar && CACHE.sidebar === content) return
|
const target = activeLink('aside.sidebar', true)
|
||||||
CACHE.sidebar = content
|
if (content) renderSubSidebar(target)
|
||||||
renderTo('aside.sidebar', content)
|
|
||||||
if (isToc) {
|
|
||||||
scrollActiveSidebar()
|
|
||||||
}
|
|
||||||
toc = []
|
toc = []
|
||||||
|
|
||||||
|
scrollActiveSidebar()
|
||||||
|
}
|
||||||
|
|
||||||
|
export function renderSubSidebar (target) {
|
||||||
|
if (!OPTIONS.maxSubLevel) return
|
||||||
|
target.parentNode.innerHTML += tpl.tree(genTree(toc, OPTIONS.maxSubLevel), '<ul>')
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue