Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da75d70

Browse filesBrowse files
committed
feat: support query string for the search, fixed #156
1 parent 6ed25ce commit da75d70
Copy full SHA for da75d70

3 files changed

+30-24Lines changed: 30 additions & 24 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/core/route/hash.js‎

Copy file name to clipboardExpand all lines: src/core/route/hash.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function replaceHash (path) {
1212
const replaceSlug = cached(path => {
1313
return path
1414
.replace('#', '?id=')
15-
.replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
15+
// .replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
1616
})
1717
/**
1818
* Normalize the current url
Collapse file

‎src/plugins/search/component.js‎

Copy file name to clipboardExpand all lines: src/plugins/search/component.js
+28-22Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ function style () {
6969
dom.appendTo(dom.head, style)
7070
}
7171

72-
function tpl (opts) {
72+
function tpl (opts, defaultValue = '') {
7373
const html =
74-
`<input type="search" />` +
74+
`<input type="search" value="${defaultValue}" />` +
7575
'<div class="results-panel"></div>' +
7676
'</div>'
7777
const el = dom.create('div', html)
@@ -81,29 +81,32 @@ function tpl (opts) {
8181
dom.before(aside, el)
8282
}
8383

84-
function bindEvents () {
84+
function doSearch (value) {
8585
const $search = dom.find('div.search')
86-
const $input = dom.find($search, 'input')
8786
const $panel = dom.find($search, '.results-panel')
88-
const doSearch = function (value) {
89-
if (!value) {
90-
$panel.classList.remove('show')
91-
$panel.innerHTML = ''
92-
return
93-
}
94-
const matchs = search(value)
95-
96-
let html = ''
97-
matchs.forEach(post => {
98-
html += `<div class="matching-post">
99-
<h2><a href="${post.url}">${post.title}</a></h2>
100-
<p>${post.content}</p>
101-
</div>`
102-
})
10387

104-
$panel.classList.add('show')
105-
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
88+
if (!value) {
89+
$panel.classList.remove('show')
90+
$panel.innerHTML = ''
91+
return
10692
}
93+
const matchs = search(value)
94+
95+
let html = ''
96+
matchs.forEach(post => {
97+
html += `<div class="matching-post">
98+
<h2><a href="${post.url}">${post.title}</a></h2>
99+
<p>${post.content}</p>
100+
</div>`
101+
})
102+
103+
$panel.classList.add('show')
104+
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
105+
}
106+
107+
function bindEvents () {
108+
const $search = dom.find('div.search')
109+
const $input = dom.find($search, 'input')
107110

108111
let timeId
109112
// Prevent to Fold sidebar
@@ -137,9 +140,12 @@ function updateNoData (text, path) {
137140

138141
export function init (opts) {
139142
dom = Docsify.dom
143+
const keywords = Docsify.route.parse().query.s
144+
140145
style()
141-
tpl(opts)
146+
tpl(opts, keywords)
142147
bindEvents()
148+
keywords && setTimeout(_ => doSearch(keywords), 500)
143149
}
144150

145151
export function update (opts, vm) {
Collapse file

‎src/plugins/search/index.js‎

Copy file name to clipboardExpand all lines: src/plugins/search/index.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const CONFIG = {
99
}
1010

1111
const install = function (hook, vm) {
12-
const util = Docsify.util
12+
const { util } = Docsify
1313
const opts = vm.config.search || CONFIG
1414

1515
if (Array.isArray(opts)) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.