aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortdro <tdro@noreply.example.com>2023-01-22 12:41:29 -0500
committertdro <tdro@noreply.example.com>2023-01-22 12:41:29 -0500
commitad16ca0ca6c068403fd359bcf9729db75b8321ac (patch)
tree1f237ec3e4e50b497012718597d81929e67b53ed
parent1a2c89c1837cb167585a1f7afaabec81a5ffae43 (diff)
downloadcanory-ad16ca0ca6c068403fd359bcf9729db75b8321ac.tar.gz
canory-ad16ca0ca6c068403fd359bcf9729db75b8321ac.tar.bz2
canory-ad16ca0ca6c068403fd359bcf9729db75b8321ac.zip
static/js/fixedsearch: Add license link
Defang the inner <html>. Can refactor this later
-rw-r--r--assets/js/index.js17
-rw-r--r--static/js/fixedsearch.ts30
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 = `
- <li>
- <a href="javascript: void(0)" tabindex="0">${term} ${separator} No Results Found</a>
- </li>
- `;
+ <li>
+ <a href="javascript: void(0)" tabindex="0">${escape(term)} ${separator} No Results Found</a>
+ </li>
+ `;
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), "<span>", "</span>");
+ let highlight = fuzzysort.highlight(fuzzysort.single(escape(term), escape(title)), "<span>", "</span>");
if (highlight === null) {
highlight = title;
}
@@ -479,7 +484,7 @@
<li>
<a href="${results[string].obj.url}" tabindex="0">${highlight}</a>
</li>
- `;
+ `;
}
}
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 = `
- <li>
- <a href="javascript: void(0)" tabindex="0">${term} ${separator} No Results Found</a>
- </li>
- `;
+ <li>
+ <a href="javascript: void(0)" tabindex="0">${
+ escape(term)
+ } ${separator} No Results Found</a>
+ </li>
+ `;
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)),
"<span>",
"</span>",
);
@@ -200,7 +208,7 @@
<li>
<a href="${results[string].obj.url}" tabindex="0">${highlight}</a>
</li>
- `;
+ `;
}
}