Skip to content

Commit

Permalink
fix(search): incorrect anchor link, fixed #90
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 22, 2017
1 parent 855c450 commit b8a3d8f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/core/global-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import * as util from './util'
import * as dom from './util/dom'
import * as render from './render/compiler'
import * as route from './route/hash'
import { slugify } from './render/slugify'
import { get } from './fetch/ajax'
import marked from 'marked'
import prism from 'prismjs'

export default function () {
window.Docsify = { util, dom, render, route, get }
window.Docsify = { util, dom, render, route, get, slugify }
window.marked = marked
window.Prism = prism
}
4 changes: 2 additions & 2 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import marked from 'marked'
import Prism from 'prismjs'
import { helper as helperTpl, tree as treeTpl } from './tpl'
import { genTree } from './gen-tree'
import { slugify, clearSlugCache } from './slugify'
import { slugify } from './slugify'
import { emojify } from './emojify'
import { toURL, parse } from '../route/hash'
import { getBasePath, isAbsolutePath, getPath } from '../route/util'
Expand All @@ -25,7 +25,7 @@ export const markdown = cached(text => {

html = markdownCompiler(text)
html = emojify(html)
clearSlugCache()
slugify.clear()

return html
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/render/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export function slugify (str) {
return slug
}

export function clearSlugCache () {
slugify.clear = function () {
cache = {}
}
2 changes: 1 addition & 1 deletion src/plugins/search/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function bindEvents () {
e => e.target.tagName !== 'A' && e.stopPropagation())
dom.on($input, 'input', e => {
clearTimeout(timeId)
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 200)
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100)
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ function saveData (maxAge) {

export function genIndex (path, content = '') {
const tokens = window.marked.lexer(content)
const slugify = window.Docsify.slugify
const toURL = Docsify.route.toURL
const index = {}
let slug

tokens.forEach(token => {
if (token.type === 'heading' && token.depth <= 2) {
slug = toURL(path, { id: token.text })
slug = toURL(path, { id: slugify(token.text) })
index[slug] = { slug, title: token.text, body: '' }
} else {
if (!slug) return
Expand All @@ -61,7 +62,7 @@ export function genIndex (path, content = '') {
}
}
})

slugify.clear()
return index
}

Expand Down

0 comments on commit b8a3d8f

Please sign in to comment.