Allow searching via ?q=

This commit is contained in:
Rico Sta. Cruz 2017-08-30 19:56:59 +08:00
parent 8c01130950
commit 522d174dba
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
2 changed files with 26 additions and 2 deletions

View File

@ -24,9 +24,9 @@ type: website
Hey! I'm <a href='https://ricostacruz.com'>@rstacruz</a> and this is a
modest collection of cheatsheets I've written.
</p>
<form data-js-search-form class='search'>
<form data-js-search-form class='search' action='.' method='get'>
<label class='search-box'>
<input type='text' placeholder='Search...' autofocus data-js-search-input>
<input name='q' type='text' placeholder='Search...' autofocus data-js-search-input>
</label>
</form>
</div>

View File

@ -64,6 +64,12 @@ $(function () {
Search.show(val)
}
})
const query = (qs(window.location.search) || {}).q
if (query && query.length) {
$this.val(query)
Search.show(query)
}
})
$('[data-js-search-form]').each(function () {
@ -207,3 +213,21 @@ function groupify ($this, { tag, wrapper, body }) {
return $wrap
}
}
/*
* Helper: minimal qs implementation
*/
function qs (search) {
search = search.substr(1)
const parts = search.split('&').map(p => p.split('='))
return parts.reduce((result, part) => {
result[part[0]] = qsdecode(part[1])
return result
}, {})
}
function qsdecode (string) {
string = string.replace(/\+/g, ' ')
return string
}