fix(search): escape html

This commit is contained in:
qingwei.li 2017-02-19 13:24:21 +08:00 • committed by cinwell.li
commit fcb66e8bc7

View file

@ -40,30 +40,36 @@ function saveData (maxAge) {
export function genIndex (path, content = '') { export function genIndex (path, content = '') {
const tokens = window.marked.lexer(content) const tokens = window.marked.lexer(content)
const toURL = Docsify.route.toURL const toURL = Docsify.route.toURL
const index = {}
let slug let slug
tokens.forEach(token => { tokens.forEach(token => {
if (token.type === 'heading' && token.depth === 1) { if (token.type === 'heading' && token.depth <= 2) {
slug = toURL(path, { id: token.text }) slug = toURL(path, { id: token.text })
INDEXS[slug] = { slug, title: token.text, body: '' } index[slug] = { slug, title: token.text, body: '' }
} else { } else {
if (!slug) return if (!slug) return
if (!INDEXS[slug]) { if (!index[slug]) {
INDEXS[slug] = { slug, title: '', body: '' } index[slug] = { slug, title: '', body: '' }
} else { } else {
if (INDEXS[slug].body) { if (index[slug].body) {
INDEXS[slug].body += '\n' + (token.text || '') index[slug].body += '\n' + (token.text || '')
} else { } else {
INDEXS[slug].body = token.text index[slug].body = token.text
} }
} }
} }
}) })
return index
} }
export function search (keywords) { export function search (keywords) {
const matchingResults = [] const matchingResults = []
const data = Object.keys(INDEXS).map(key => INDEXS[key]) let data = []
Object.keys(INDEXS).forEach(key => {
data = data.concat(Object.keys(INDEXS[key]).map(page => INDEXS[key][page]))
})
keywords = keywords.trim().split(/[\s\-\,\\/]+/) keywords = keywords.trim().split(/[\s\-\,\\/]+/)
@ -99,7 +105,7 @@ export function search (keywords) {
if (end > postContent.length) end = postContent.length if (end > postContent.length) end = postContent.length
const matchContent = '...' + const matchContent = '...' +
postContent escapeHtml(postContent)
.substring(start, end) .substring(start, end)
.replace(regEx, `<em class="search-keyword">${keyword}</em>`) + .replace(regEx, `<em class="search-keyword">${keyword}</em>`) +
'...' '...'
@ -144,11 +150,10 @@ export function init (config, vm) {
paths.forEach(path => { paths.forEach(path => {
if (INDEXS[path]) return count++ if (INDEXS[path]) return count++
path = vm.$getFile(path)
helper helper
.get(path) .get(vm.$getFile(path))
.then(result => { .then(result => {
genIndex(path, result) INDEXS[path] = genIndex(path, result)
len === ++count && saveData(config.maxAge) len === ++count && saveData(config.maxAge)
}) })
}) })