mirror of
https://github.com/docsifyjs/docsify.git
synced 2025-12-08 19:55:52 +00:00
feat: support query string for the search, fixed #156
This commit is contained in:
parent
6ed25cebe5
commit
da75d70dee
@ -12,7 +12,7 @@ function replaceHash (path) {
|
||||
const replaceSlug = cached(path => {
|
||||
return path
|
||||
.replace('#', '?id=')
|
||||
.replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
|
||||
// .replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
|
||||
})
|
||||
/**
|
||||
* Normalize the current url
|
||||
|
||||
@ -69,9 +69,9 @@ function style () {
|
||||
dom.appendTo(dom.head, style)
|
||||
}
|
||||
|
||||
function tpl (opts) {
|
||||
function tpl (opts, defaultValue = '') {
|
||||
const html =
|
||||
`<input type="search" />` +
|
||||
`<input type="search" value="${defaultValue}" />` +
|
||||
'<div class="results-panel"></div>' +
|
||||
'</div>'
|
||||
const el = dom.create('div', html)
|
||||
@ -81,29 +81,32 @@ function tpl (opts) {
|
||||
dom.before(aside, el)
|
||||
}
|
||||
|
||||
function doSearch (value) {
|
||||
const $search = dom.find('div.search')
|
||||
const $panel = dom.find($search, '.results-panel')
|
||||
|
||||
if (!value) {
|
||||
$panel.classList.remove('show')
|
||||
$panel.innerHTML = ''
|
||||
return
|
||||
}
|
||||
const matchs = search(value)
|
||||
|
||||
let html = ''
|
||||
matchs.forEach(post => {
|
||||
html += `<div class="matching-post">
|
||||
<h2><a href="${post.url}">${post.title}</a></h2>
|
||||
<p>${post.content}</p>
|
||||
</div>`
|
||||
})
|
||||
|
||||
$panel.classList.add('show')
|
||||
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
|
||||
}
|
||||
|
||||
function bindEvents () {
|
||||
const $search = dom.find('div.search')
|
||||
const $input = dom.find($search, 'input')
|
||||
const $panel = dom.find($search, '.results-panel')
|
||||
const doSearch = function (value) {
|
||||
if (!value) {
|
||||
$panel.classList.remove('show')
|
||||
$panel.innerHTML = ''
|
||||
return
|
||||
}
|
||||
const matchs = search(value)
|
||||
|
||||
let html = ''
|
||||
matchs.forEach(post => {
|
||||
html += `<div class="matching-post">
|
||||
<h2><a href="${post.url}">${post.title}</a></h2>
|
||||
<p>${post.content}</p>
|
||||
</div>`
|
||||
})
|
||||
|
||||
$panel.classList.add('show')
|
||||
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
|
||||
}
|
||||
|
||||
let timeId
|
||||
// Prevent to Fold sidebar
|
||||
@ -137,9 +140,12 @@ function updateNoData (text, path) {
|
||||
|
||||
export function init (opts) {
|
||||
dom = Docsify.dom
|
||||
const keywords = Docsify.route.parse().query.s
|
||||
|
||||
style()
|
||||
tpl(opts)
|
||||
tpl(opts, keywords)
|
||||
bindEvents()
|
||||
keywords && setTimeout(_ => doSearch(keywords), 500)
|
||||
}
|
||||
|
||||
export function update (opts, vm) {
|
||||
|
||||
@ -9,7 +9,7 @@ const CONFIG = {
|
||||
}
|
||||
|
||||
const install = function (hook, vm) {
|
||||
const util = Docsify.util
|
||||
const { util } = Docsify
|
||||
const opts = vm.config.search || CONFIG
|
||||
|
||||
if (Array.isArray(opts)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user