From ad16ca0ca6c068403fd359bcf9729db75b8321ac Mon Sep 17 00:00:00 2001 From: tdro Date: Sun, 22 Jan 2023 12:41:29 -0500 Subject: static/js/fixedsearch: Add license link Defang the inner . Can refactor this later --- assets/js/index.js | 17 +++++++++++------ static/js/fixedsearch.ts | 30 +++++++++++++++++++----------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index 8927f21..bcb4509 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -454,6 +454,11 @@ }); } } + function escape(text) { + const escaped = document.createElement("textarea"); + escaped.textContent = text; + return escaped.innerHTML; + } function search(term, data, options) { const results = fuzzysort.go(term, data, options); let items = ""; @@ -461,17 +466,17 @@ let separator = "—"; if (term.length === 0) separator = ""; items = ` -
  • - ${term} ${separator} No Results Found -
  • - `; +
  • + ${escape(term)} ${separator} No Results Found +
  • + `; dropdown.removeAttribute("hidden"); container.setAttribute("data-focus", ""); } else { dropdown.removeAttribute("hidden"); for(const string in results.slice(0, 10)){ const title = results[string].obj.title; - let highlight = fuzzysort.highlight(fuzzysort.single(term, title), "", ""); + let highlight = fuzzysort.highlight(fuzzysort.single(escape(term), escape(title)), "", ""); if (highlight === null) { highlight = title; } @@ -479,7 +484,7 @@
  • ${highlight}
  • - `; + `; } } dropdown.innerHTML = items; diff --git a/static/js/fixedsearch.ts b/static/js/fixedsearch.ts index fafe5ee..63a3b4b 100644 --- a/static/js/fixedsearch.ts +++ b/static/js/fixedsearch.ts @@ -1,10 +1,10 @@ +/* + Modified Version of Fixed Search: https://gist.github.com/cmod/5410eae147e4318164258742dd053993 + MIT License: https://gist.github.com/Arty2/8b0c43581013753438a3d35c15091a9f#file-license-md +*/ + (function () { self.addEventListener("DOMContentLoaded", function () { - /* - Originally Based on fixedsearch, a super fast, client side search for Hugo.io with Fusejs.io - based on https://gist.github.com/cmod/5410eae147e4318164258742dd053993 - */ - const form = document.getElementById("search-form"); // search form const query = document.getElementById("search-input"); // input box for search const submit = document.getElementById("search-submit"); // form submit button @@ -167,6 +167,12 @@ } } + function escape(text) { + const escaped = document.createElement("textarea"); + escaped.textContent = text; + return escaped.innerHTML; + } + function search(term, data, options) { const results = fuzzysort.go(term, data, options); let items = ""; @@ -175,10 +181,12 @@ let separator = "—"; if (term.length === 0) separator = ""; items = ` -
  • - ${term} ${separator} No Results Found -
  • - `; +
  • + ${ + escape(term) + } ${separator} No Results Found +
  • + `; dropdown.removeAttribute("hidden"); container.setAttribute("data-focus", ""); } else { @@ -186,7 +194,7 @@ for (const string in results.slice(0, 10)) { const title = results[string].obj.title; let highlight = fuzzysort.highlight( - fuzzysort.single(term, title), + fuzzysort.single(escape(term), escape(title)), "", "", ); @@ -200,7 +208,7 @@
  • ${highlight}
  • - `; + `; } } -- cgit v1.2.3