feat: support query string for the search, fixed #156

This commit is contained in:
qingwei.li 2017-05-06 21:11:04 +08:00
parent 6ed25cebe5
commit da75d70dee
3 changed files with 31 additions and 25 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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)) {