Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fcb66e8

Browse filesBrowse files
committed
fix(search): escape html
1 parent 2efd859 commit fcb66e8
Copy full SHA for fcb66e8

1 file changed

+17-12Lines changed: 17 additions & 12 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/plugins/search/search.js‎

Copy file name to clipboardExpand all lines: src/plugins/search/search.js
+17-12Lines changed: 17 additions & 12 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.