Skip to content

Commit b8a3d8f

Browse files
committed
fix(search): incorrect anchor link, fixed #90
1 parent 855c450 commit b8a3d8f

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

src/core/global-api.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import * as util from './util'
22
import * as dom from './util/dom'
33
import * as render from './render/compiler'
44
import * as route from './route/hash'
5+
import { slugify } from './render/slugify'
56
import { get } from './fetch/ajax'
67
import marked from 'marked'
78
import prism from 'prismjs'
89

910
export default function () {
10-
window.Docsify = { util, dom, render, route, get }
11+
window.Docsify = { util, dom, render, route, get, slugify }
1112
window.marked = marked
1213
window.Prism = prism
1314
}

src/core/render/compiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import marked from 'marked'
22
import Prism from 'prismjs'
33
import { helper as helperTpl, tree as treeTpl } from './tpl'
44
import { genTree } from './gen-tree'
5-
import { slugify, clearSlugCache } from './slugify'
5+
import { slugify } from './slugify'
66
import { emojify } from './emojify'
77
import { toURL, parse } from '../route/hash'
88
import { getBasePath, isAbsolutePath, getPath } from '../route/util'
@@ -25,7 +25,7 @@ export const markdown = cached(text => {
2525

2626
html = markdownCompiler(text)
2727
html = emojify(html)
28-
clearSlugCache()
28+
slugify.clear()
2929

3030
return html
3131
})

src/core/render/slugify.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ export function slugify (str) {
2222
return slug
2323
}
2424

25-
export function clearSlugCache () {
25+
slugify.clear = function () {
2626
cache = {}
2727
}

src/plugins/search/component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function bindEvents () {
107107
e => e.target.tagName !== 'A' && e.stopPropagation())
108108
dom.on($input, 'input', e => {
109109
clearTimeout(timeId)
110-
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 200)
110+
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100)
111111
})
112112
}
113113

src/plugins/search/search.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ function saveData (maxAge) {
4040

4141
export function genIndex (path, content = '') {
4242
const tokens = window.marked.lexer(content)
43+
const slugify = window.Docsify.slugify
4344
const toURL = Docsify.route.toURL
4445
const index = {}
4546
let slug
4647

4748
tokens.forEach(token => {
4849
if (token.type === 'heading' && token.depth <= 2) {
49-
slug = toURL(path, { id: token.text })
50+
slug = toURL(path, { id: slugify(token.text) })
5051
index[slug] = { slug, title: token.text, body: '' }
5152
} else {
5253
if (!slug) return
@@ -61,7 +62,7 @@ export function genIndex (path, content = '') {
6162
}
6263
}
6364
})
64-
65+
slugify.clear()
6566
return index
6667
}
6768

0 commit comments

Comments
 (0)