From da75d70dee4f0b7e5f07d2f06a3d437aff5980b3 Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Sat, 6 May 2017 21:11:04 +0800 Subject: [PATCH] feat: support query string for the search, fixed #156 --- src/core/route/hash.js | 2 +- src/plugins/search/component.js | 52 ++++++++++++++++++--------------- src/plugins/search/index.js | 2 +- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/core/route/hash.js b/src/core/route/hash.js index 417ae96a..fb441f0b 100644 --- a/src/core/route/hash.js +++ b/src/core/route/hash.js @@ -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 diff --git a/src/plugins/search/component.js b/src/plugins/search/component.js index 3baf3bc6..455314f9 100644 --- a/src/plugins/search/component.js +++ b/src/plugins/search/component.js @@ -69,9 +69,9 @@ function style () { dom.appendTo(dom.head, style) } -function tpl (opts) { +function tpl (opts, defaultValue = '') { const html = - `` + + `` + '
' + '' 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 += `
+

${post.title}

+

${post.content}

+
` + }) + + $panel.classList.add('show') + $panel.innerHTML = html || `

${NO_DATA_TEXT}

` +} + 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 += `
-

${post.title}

-

${post.content}

-
` - }) - - $panel.classList.add('show') - $panel.innerHTML = html || `

${NO_DATA_TEXT}

` - } 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) { diff --git a/src/plugins/search/index.js b/src/plugins/search/index.js index 6b9d3e16..8f6124cb 100644 --- a/src/plugins/search/index.js +++ b/src/plugins/search/index.js @@ -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)) {