Skip to content

Commit

Permalink
feat(search): Supports the max depth of the search headline, fixed #223
Browse files Browse the repository at this point in the history
…, resolve #129
  • Loading branch information
QingWei-Li committed Jul 26, 2017
1 parent 0dd2497 commit b7b589b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/de-de/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 4 additions & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
5 changes: 4 additions & 1 deletion docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
noData: {
'/zh-cn/': '找不到结果',
'/': 'No Results'
}
},
// 搜索标题的最大程级, 1 - 6
depth: 2
}
}
</script>
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const CONFIG = {
placeholder: 'Type to search',
noData: 'No Results!',
paths: 'auto',
depth: 2,
maxAge: 86400000 // 1 day
}

Expand All @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
})
})
Expand Down

0 comments on commit b7b589b

Please sign in to comment.