From b8a3d8f38094f7270bb37fde38d278fe43b6b827 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Wed, 22 Feb 2017 21:38:02 +0800 Subject: [PATCH] fix(search): incorrect anchor link, fixed #90 --- src/core/global-api.js | 3 ++- src/core/render/compiler.js | 4 ++-- src/core/render/slugify.js | 2 +- src/plugins/search/component.js | 2 +- src/plugins/search/search.js | 5 +++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/core/global-api.js b/src/core/global-api.js index aa2ae0e..b5565d9 100644 --- a/src/core/global-api.js +++ b/src/core/global-api.js @@ -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 } diff --git a/src/core/render/compiler.js b/src/core/render/compiler.js index 58db9b3..785f70c 100644 --- a/src/core/render/compiler.js +++ b/src/core/render/compiler.js @@ -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' @@ -25,7 +25,7 @@ export const markdown = cached(text => { html = markdownCompiler(text) html = emojify(html) - clearSlugCache() + slugify.clear() return html }) diff --git a/src/core/render/slugify.js b/src/core/render/slugify.js index a6e9d39..bb34758 100644 --- a/src/core/render/slugify.js +++ b/src/core/render/slugify.js @@ -22,6 +22,6 @@ export function slugify (str) { return slug } -export function clearSlugCache () { +slugify.clear = function () { cache = {} } diff --git a/src/plugins/search/component.js b/src/plugins/search/component.js index cc7d0ec..524448c 100644 --- a/src/plugins/search/component.js +++ b/src/plugins/search/component.js @@ -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) }) } diff --git a/src/plugins/search/search.js b/src/plugins/search/search.js index 58e8cbf..9888d63 100644 --- a/src/plugins/search/search.js +++ b/src/plugins/search/search.js @@ -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 @@ -61,7 +62,7 @@ export function genIndex (path, content = '') { } } }) - + slugify.clear() return index }