From e627bcb2cbcddfc969f5ba8007e2f10ade9ae79b Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Mon, 14 Dec 2020 10:28:35 +0800 Subject: [PATCH] Refactor getQueryParameters --- .../_common/components/post/post-footer.styl | 4 +-- .../_common/scaffolding/tags/link-grid.styl | 2 ++ source/css/_schemes/Pisces/_sidebar.styl | 1 + source/js/local-search.js | 26 +++---------------- source/js/utils.js | 2 +- 5 files changed, 9 insertions(+), 26 deletions(-) diff --git a/source/css/_common/components/post/post-footer.styl b/source/css/_common/components/post/post-footer.styl index bf661ba..5351db2 100644 --- a/source/css/_common/components/post/post-footer.styl +++ b/source/css/_common/components/post/post-footer.styl @@ -1,8 +1,8 @@ // Flexbox layout makes it possible to reorder the child // elements of .post-footer through the `order` CSS property // Fix issue #16 -// To do: use `gap` instead of `margin-bottom` -// See https://caniuse.com/#feat=flexbox-gap +// To do: use `gap` instead of `margin` +// See https://caniuse.com/flexbox-gap .post-footer { flex-column(); } diff --git a/source/css/_common/scaffolding/tags/link-grid.styl b/source/css/_common/scaffolding/tags/link-grid.styl index 79f5db6..5fc7811 100644 --- a/source/css/_common/scaffolding/tags/link-grid.styl +++ b/source/css/_common/scaffolding/tags/link-grid.styl @@ -1,5 +1,7 @@ .post-body .link-grid { display: grid; + // https://caniuse.com/mdn-css_properties_gap_grid_context + grid-gap: 1.5rem 1.5rem; gap: 1.5rem 1.5rem; grid-template-columns: 1fr 1fr; margin-bottom: 20px; diff --git a/source/css/_schemes/Pisces/_sidebar.styl b/source/css/_schemes/Pisces/_sidebar.styl index 10268fc..ae8875a 100644 --- a/source/css/_schemes/Pisces/_sidebar.styl +++ b/source/css/_schemes/Pisces/_sidebar.styl @@ -1,5 +1,6 @@ .sidebar { margin-top: $sidebar-offset; + // https://caniuse.com/css-sticky position: -webkit-sticky; position: sticky; top: $sidebar-offset; diff --git a/source/js/local-search.js b/source/js/local-search.js index 50148f8..3159920 100644 --- a/source/js/local-search.js +++ b/source/js/local-search.js @@ -222,26 +222,6 @@ document.addEventListener('DOMContentLoaded', () => { }); }; - /** - * This function returns the parsed url parameters of the - * current request. Multiple values per key are supported, - * it will always return arrays of strings for the value parts. - */ - const getQueryParameters = () => { - const s = location.search; - const parts = s.substr(s.indexOf('?') + 1).split('&'); - const result = {}; - for (const part of parts) { - const [key, value] = part.split('=', 2); - if (key in result) { - result[key].push(value); - } else { - result[key] = [value]; - } - } - return result; - }; - // Highlight by wrapping node in mark elements with the given class name const highlightText = (node, slice, className) => { const val = node.nodeValue; @@ -263,11 +243,11 @@ document.addEventListener('DOMContentLoaded', () => { // Highlight the search words provided in the url in the text const highlightSearchWords = () => { - const params = getQueryParameters(); - const keywords = params.highlight ? params.highlight[0].split(/\+/).map(decodeURIComponent) : []; + const params = new URL(location.href).searchParams.get('highlight'); + const keywords = params ? params.split(' ') : []; const body = document.querySelector('.post-body'); if (!keywords.length || !body) return; - const walk = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, false); + const walk = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null); const allNodes = []; while (walk.nextNode()) { if (!walk.currentNode.parentNode.matches('button, select, textarea')) allNodes.push(walk.currentNode); diff --git a/source/js/utils.js b/source/js/utils.js index 36355bf..e451f64 100644 --- a/source/js/utils.js +++ b/source/js/utils.js @@ -6,7 +6,7 @@ HTMLElement.prototype.wrap = function(wrapper) { wrapper.appendChild(this); }; -// https://caniuse.com/#feat=mdn-api_element_classlist_replace +// https://caniuse.com/mdn-api_element_classlist_replace if (typeof DOMTokenList.prototype.replace !== 'function') { DOMTokenList.prototype.replace = function(remove, add) { this.remove(remove);