From 522d174dbac3061a085cf29b70f2d818806038b5 Mon Sep 17 00:00:00 2001 From: "Rico Sta. Cruz" Date: Wed, 30 Aug 2017 19:56:59 +0800 Subject: [PATCH] Allow searching via ?q= --- _layouts/2017/home.html | 4 ++-- assets/2017/script.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/_layouts/2017/home.html b/_layouts/2017/home.html index 71c23099c..aecfe3809 100644 --- a/_layouts/2017/home.html +++ b/_layouts/2017/home.html @@ -24,9 +24,9 @@ type: website Hey! I'm @rstacruz and this is a modest collection of cheatsheets I've written.

- diff --git a/assets/2017/script.js b/assets/2017/script.js index ef891adcd..d60427838 100644 --- a/assets/2017/script.js +++ b/assets/2017/script.js @@ -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 +}