feat: support query string for the search, fixed #156
This commit is contained in:
parent
6ed25cebe5
commit
da75d70dee
3 changed files with 31 additions and 25 deletions
|
|
@ -12,7 +12,7 @@ function replaceHash (path) {
|
||||||
const replaceSlug = cached(path => {
|
const replaceSlug = cached(path => {
|
||||||
return path
|
return path
|
||||||
.replace('#', '?id=')
|
.replace('#', '?id=')
|
||||||
.replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
|
// .replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
|
||||||
})
|
})
|
||||||
/**
|
/**
|
||||||
* Normalize the current url
|
* Normalize the current url
|
||||||
|
|
|
||||||
|
|
@ -69,9 +69,9 @@ function style () {
|
||||||
dom.appendTo(dom.head, style)
|
dom.appendTo(dom.head, style)
|
||||||
}
|
}
|
||||||
|
|
||||||
function tpl (opts) {
|
function tpl (opts, defaultValue = '') {
|
||||||
const html =
|
const html =
|
||||||
`<input type="search" />` +
|
`<input type="search" value="${defaultValue}" />` +
|
||||||
'<div class="results-panel"></div>' +
|
'<div class="results-panel"></div>' +
|
||||||
'</div>'
|
'</div>'
|
||||||
const el = dom.create('div', html)
|
const el = dom.create('div', html)
|
||||||
|
|
@ -81,29 +81,32 @@ function tpl (opts) {
|
||||||
dom.before(aside, el)
|
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 () {
|
function bindEvents () {
|
||||||
const $search = dom.find('div.search')
|
const $search = dom.find('div.search')
|
||||||
const $input = dom.find($search, 'input')
|
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
|
let timeId
|
||||||
// Prevent to Fold sidebar
|
// Prevent to Fold sidebar
|
||||||
|
|
@ -137,9 +140,12 @@ function updateNoData (text, path) {
|
||||||
|
|
||||||
export function init (opts) {
|
export function init (opts) {
|
||||||
dom = Docsify.dom
|
dom = Docsify.dom
|
||||||
|
const keywords = Docsify.route.parse().query.s
|
||||||
|
|
||||||
style()
|
style()
|
||||||
tpl(opts)
|
tpl(opts, keywords)
|
||||||
bindEvents()
|
bindEvents()
|
||||||
|
keywords && setTimeout(_ => doSearch(keywords), 500)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function update (opts, vm) {
|
export function update (opts, vm) {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ const CONFIG = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const install = function (hook, vm) {
|
const install = function (hook, vm) {
|
||||||
const util = Docsify.util
|
const { util } = Docsify
|
||||||
const opts = vm.config.search || CONFIG
|
const opts = vm.config.search || CONFIG
|
||||||
|
|
||||||
if (Array.isArray(opts)) {
|
if (Array.isArray(opts)) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue