feat(search): Supports the max depth of the search headline, fixed #223, resolve #129

This commit is contained in:
qingwei.li 2017-07-26 08:36:35 +08:00
commit b7b589b1bf
5 changed files with 17 additions and 6 deletions

View file

@ -35,7 +35,10 @@ Als Standardeinstellung werden Hyperlinks auf der aktuellen Seite erkannt und de
noData: {
'/de-de/': 'Keine Ergebnisse',
'/': 'No Results'
}
},
// Headline depth, 1 - 6
depth: 2
}
}
</script>

View file

@ -35,7 +35,10 @@ By default, the hyperlink on the current page is recognized and the content is s
noData: {
'/zh-cn/': '找不到结果',
'/': 'No Results'
}
},
// Headline depth, 1 - 6
depth: 2
}
}
</script>

View file

@ -35,7 +35,10 @@
noData: {
'/zh-cn/': '找不到结果',
'/': 'No Results'
}
},
// 搜索标题的最大程级, 1 - 6
depth: 2
}
}
</script>

View file

@ -5,6 +5,7 @@ const CONFIG = {
placeholder: 'Type to search',
noData: 'No Results!',
paths: 'auto',
depth: 2,
maxAge: 86400000 // 1 day
}
@ -19,6 +20,7 @@ const install = function (hook, vm) {
CONFIG.maxAge = util.isPrimitive(opts.maxAge) ? opts.maxAge : CONFIG.maxAge
CONFIG.placeholder = opts.placeholder || CONFIG.placeholder
CONFIG.noData = opts.noData || CONFIG.noData
CONFIG.depth = opts.depth || CONFIG.depth
}
const isAuto = CONFIG.paths === 'auto'

View file

@ -38,14 +38,14 @@ function saveData (maxAge) {
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
}
export function genIndex (path, content = '', router) {
export function genIndex (path, content = '', router, depth) {
const tokens = window.marked.lexer(content)
const slugify = window.Docsify.slugify
const index = {}
let slug
tokens.forEach(token => {
if (token.type === 'heading' && token.depth <= 2) {
if (token.type === 'heading' && token.depth <= depth) {
slug = router.toURL(path, { id: slugify(token.text) })
index[slug] = { slug, title: token.text, body: '' }
} else {
@ -154,7 +154,7 @@ export function init (config, vm) {
helper
.get(vm.router.getFile(path))
.then(result => {
INDEXS[path] = genIndex(path, result, vm.router)
INDEXS[path] = genIndex(path, result, vm.router, config.depth)
len === ++count && saveData(config.maxAge)
})
})