Skip to content

Commit fcb66e8

Browse files
committed
fix(search): escape html
1 parent 2efd859 commit fcb66e8

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/plugins/search/search.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,36 @@ function saveData (maxAge) {
4040
export function genIndex (path, content = '') {
4141
const tokens = window.marked.lexer(content)
4242
const toURL = Docsify.route.toURL
43+
const index = {}
4344
let slug
4445

4546
tokens.forEach(token => {
46-
if (token.type === 'heading' && token.depth === 1) {
47+
if (token.type === 'heading' && token.depth <= 2) {
4748
slug = toURL(path, { id: token.text })
48-
INDEXS[slug] = { slug, title: token.text, body: '' }
49+
index[slug] = { slug, title: token.text, body: '' }
4950
} else {
5051
if (!slug) return
51-
if (!INDEXS[slug]) {
52-
INDEXS[slug] = { slug, title: '', body: '' }
52+
if (!index[slug]) {
53+
index[slug] = { slug, title: '', body: '' }
5354
} else {
54-
if (INDEXS[slug].body) {
55-
INDEXS[slug].body += '\n' + (token.text || '')
55+
if (index[slug].body) {
56+
index[slug].body += '\n' + (token.text || '')
5657
} else {
57-
INDEXS[slug].body = token.text
58+
index[slug].body = token.text
5859
}
5960
}
6061
}
6162
})
63+
64+
return index
6265
}
6366

6467
export function search (keywords) {
6568
const matchingResults = []
66-
const data = Object.keys(INDEXS).map(key => INDEXS[key])
69+
let data = []
70+
Object.keys(INDEXS).forEach(key => {
71+
data = data.concat(Object.keys(INDEXS[key]).map(page => INDEXS[key][page]))
72+
})
6773

6874
keywords = keywords.trim().split(/[\s\-\,\\/]+/)
6975

@@ -99,7 +105,7 @@ export function search (keywords) {
99105
if (end > postContent.length) end = postContent.length
100106

101107
const matchContent = '...' +
102-
postContent
108+
escapeHtml(postContent)
103109
.substring(start, end)
104110
.replace(regEx, `<em class="search-keyword">${keyword}</em>`) +
105111
'...'
@@ -144,11 +150,10 @@ export function init (config, vm) {
144150
paths.forEach(path => {
145151
if (INDEXS[path]) return count++
146152

147-
path = vm.$getFile(path)
148153
helper
149-
.get(path)
154+
.get(vm.$getFile(path))
150155
.then(result => {
151-
genIndex(path, result)
156+
INDEXS[path] = genIndex(path, result)
152157
len === ++count && saveData(config.maxAge)
153158
})
154159
})

0 commit comments

Comments
 (0)