docs_vue2/common/util.js

39 lines
808 B
JavaScript
Raw Normal View History

2020-08-13 11:47:56 +08:00
export function walk (list, base, callback) {
if (!Array.isArray(list)) {
return
}
let stopped = list.some(item => {
let newBase = `${base}/${item.slug}`
if (typeof callback === 'function') {
if (callback(item, newBase) === false) {
return true
}
}
if (item.children) {
if (walk(item.children, newBase, callback) === false) {
return true
}
}
return false
})
return !stopped
}
export function getLink ({ slug, link, children }) {
if (link === false) {
let actual = null
walk(children, slug, (item, base) => {
// leaf
if (!item.children || !item.children.length) {
actual = base
return false
}
})
if (actual !== null) {
return `/${actual}`
}
}
return `/${slug}`
}