Skip to content

Commit

Permalink
feat: support query string for the search, fixed #156
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed May 6, 2017
1 parent 6ed25ce commit da75d70
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/core/route/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 28 additions & 22 deletions src/plugins/search/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -81,29 +81,32 @@ function tpl (opts) {
dom.before(aside, el)
}

function bindEvents () {
function doSearch (value) {
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>`
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')

let timeId
// Prevent to Fold sidebar
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit da75d70

Please sign in to comment.