fix(search): incorrect anchor link, fixed #90

This commit is contained in:
qingwei.li 2017-02-22 21:38:02 +08:00 committed by cinwell.li
parent 855c450a97
commit b8a3d8f380
5 changed files with 9 additions and 7 deletions

View File

@ -2,12 +2,13 @@ import * as util from './util'
import * as dom from './util/dom' import * as dom from './util/dom'
import * as render from './render/compiler' import * as render from './render/compiler'
import * as route from './route/hash' import * as route from './route/hash'
import { slugify } from './render/slugify'
import { get } from './fetch/ajax' import { get } from './fetch/ajax'
import marked from 'marked' import marked from 'marked'
import prism from 'prismjs' import prism from 'prismjs'
export default function () { export default function () {
window.Docsify = { util, dom, render, route, get } window.Docsify = { util, dom, render, route, get, slugify }
window.marked = marked window.marked = marked
window.Prism = prism window.Prism = prism
} }

View File

@ -2,7 +2,7 @@ import marked from 'marked'
import Prism from 'prismjs' import Prism from 'prismjs'
import { helper as helperTpl, tree as treeTpl } from './tpl' import { helper as helperTpl, tree as treeTpl } from './tpl'
import { genTree } from './gen-tree' import { genTree } from './gen-tree'
import { slugify, clearSlugCache } from './slugify' import { slugify } from './slugify'
import { emojify } from './emojify' import { emojify } from './emojify'
import { toURL, parse } from '../route/hash' import { toURL, parse } from '../route/hash'
import { getBasePath, isAbsolutePath, getPath } from '../route/util' import { getBasePath, isAbsolutePath, getPath } from '../route/util'
@ -25,7 +25,7 @@ export const markdown = cached(text => {
html = markdownCompiler(text) html = markdownCompiler(text)
html = emojify(html) html = emojify(html)
clearSlugCache() slugify.clear()
return html return html
}) })

View File

@ -22,6 +22,6 @@ export function slugify (str) {
return slug return slug
} }
export function clearSlugCache () { slugify.clear = function () {
cache = {} cache = {}
} }

View File

@ -107,7 +107,7 @@ function bindEvents () {
e => e.target.tagName !== 'A' && e.stopPropagation()) e => e.target.tagName !== 'A' && e.stopPropagation())
dom.on($input, 'input', e => { dom.on($input, 'input', e => {
clearTimeout(timeId) clearTimeout(timeId)
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 200) timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100)
}) })
} }

View File

@ -40,13 +40,14 @@ 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 slugify = window.Docsify.slugify
const toURL = Docsify.route.toURL const toURL = Docsify.route.toURL
const index = {} const index = {}
let slug let slug
tokens.forEach(token => { tokens.forEach(token => {
if (token.type === 'heading' && token.depth <= 2) { 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: '' } index[slug] = { slug, title: token.text, body: '' }
} else { } else {
if (!slug) return if (!slug) return
@ -61,7 +62,7 @@ export function genIndex (path, content = '') {
} }
} }
}) })
slugify.clear()
return index return index
} }