From dc4373789617a58d517eac59c3e60fabb47eafd0 Mon Sep 17 00:00:00 2001
From: chaosarium <38693485+chaosarium@users.noreply.github.com>
Date: Sat, 24 Dec 2022 17:10:59 +0000
Subject: [PATCH] fix edge cases link processing (#258)
---
assets/js/util.js | 38 ++++++++++++++++++++++++--------------
1 files changed, 24 insertions(+), 14 deletions(-)
diff --git a/assets/js/util.js b/assets/js/util.js
index 72541d8..3346b7d 100644
--- a/assets/js/util.js
+++ b/assets/js/util.js
@@ -58,14 +58,12 @@
// try to find direct match first
const directMatchIdx = content.indexOf(term)
if (directMatchIdx !== -1) {
- console.log(directMatchIdx)
const h = highlightWindow
const before = content.substring(0, directMatchIdx).split(" ").slice(-h)
const after = content
.substring(directMatchIdx + term.length, content.length - 2)
.split(" ")
.slice(0, h)
- console.log(before, after)
return (
(before.length == h ? `...${before.join(" ")}` : before.join(" ")) +
`<span class="search-highlight">${term}</span>` +
@@ -117,9 +115,11 @@
}
const redir = (id, term) => {
- // SPA navigation
+ const shouldTrim = PRODUCTION && SEARCH_ENABLED
+ const baseURLPrefix = shouldTrim ? "" : BASE_URL.replace(/\/$/g, "")
+ const urlString = `${baseURLPrefix}${id}#:~:text=${encodeURIComponent(term)}/`
window.Million.navigate(
- new URL(`${BASE_URL.replace(/\/$/g, "")}${id}#:~:text=${encodeURIComponent(term)}/`),
+ new URL(urlString),
".singlePage",
)
closeSearch()
@@ -181,7 +181,7 @@
})
}
-const displayResults = (finalResults, extractHighlight = false) => {
+const displayResults = (term, finalResults, extractHighlight = false) => {
const results = document.getElementById("results-container")
if (finalResults.length === 0) {
results.innerHTML = `<button class="result-card">
@@ -191,18 +191,28 @@
} else {
results.innerHTML = finalResults
.map((result) => {
- if (extractHighlight) {
- return resultToHTML({
- url: result.url,
- title: highlight(result.title, term),
- content: highlight(removeMarkdown(result.content), term)
- })
- } else {
- return resultToHTML(result)
- }
+ if (extractHighlight) {
+ return resultToHTML({
+ url: result.url,
+ title: highlight(result.title, term),
+ content: highlight(removeMarkdown(result.content), term)
+ })
+ } else {
+ return resultToHTML(result)
}
+ }
)
.join("\n")
+ if (LATEX_ENABLED) {
+ renderMathInElement(results, {
+ delimiters: [
+ { left: '$$', right: '$$', display: false },
+ { left: '$', right: '$', display: false },
+ ],
+ throwOnError: false
+ })
+ }
+
const anchors = [...document.getElementsByClassName("result-card")]
anchors.forEach((anchor) => {
anchor.onclick = () => redir(anchor.id, term)
--
Gitblit v1.10.0