Skip to content

Commit

Permalink
Merge pull request #24 from solo-io/flexsearch-polish
Browse files Browse the repository at this point in the history
"link" text removed (again)
  • Loading branch information
Nadine2016 authored Jun 13, 2024
2 parents c6f75eb + 3430bfd commit d379366
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
2 changes: 2 additions & 0 deletions assets/docs/scss/custom/plugins/flexsearch/_flexsearch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,13 @@

.suggestion__title {
font-weight: 700;
word-break: break-word;
color: var(--flexsearch-suggestion-title-color);
}

.suggestion__description,
.suggestion__no-results {
word-break: break-word;
display: block !important;
color: var(--flexsearch-suggestion-desc-color);
}
Expand Down
23 changes: 4 additions & 19 deletions assets/js/flexsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const flexsearchSiteParams = JSON.parse(params.siteParamsFlexsearch), // .Site.P
indexVersionMap = JSON.parse(params.index),
i18nSearchNoResults = params.i18nSearchNoResults;

console.log("ibndex", indexVersionMap);

// Setup config
const siteFlexConfig = {
enabled: flexsearchSiteParams.enabled ?? true,
Expand Down Expand Up @@ -138,7 +136,6 @@ Source:
// this should work by finding any cases where something in url exists in search index key
const curSection = urlParts.find((part) => !!versionSubversionMap[part]) || siteVersion || "main";
const curSubversion = urlParts.find((part) => !!versionSubversionMap[curSection]?.includes(part)) || "";
console.log("sub", curSection, curSubversion, urlParts, versionSubversionMap);
return curSection + "-" + curSubversion;
}

Expand All @@ -149,11 +146,8 @@ Source:
/* `indexVersionMap` is generated via hugo inside flexsearch.html and passed into here */

const currentPageIndexId = getIndexIdOfCurrentPage();
const indexEntriesForCurrentVersion = indexVersionMap[currentPageIndexId];
for (const docIn of indexEntriesForCurrentVersion) {
const doc = JSON.parse(JSON.stringify(docIn)); // create a copy
// Words that are to long cause flexsearch to freeze, so remove them from text we give to index
doc.content = doc.content.replaceAll(/\w{40,}/g, "-");
const indexEntriesForCurrentVersion = indexVersionMap[currentPageIndexId] ?? [];
for (const doc of indexEntriesForCurrentVersion) {
getIndex(currentPageIndexId).add(doc);
}

Expand Down Expand Up @@ -197,7 +191,7 @@ Source:
// inform user that no results were found
if (flatResults.size === 0 && searchQuery) {
const noResultsMessage = document.createElement("div");
noResultsMessage.innerHTML = getNoResultsMessage(searchQuery);
noResultsMessage.innerHTML = `${i18nSearchNoResults} "<strong>${searchQuery}</strong>"`;
noResultsMessage.classList.add("suggestion__no-results");
suggestions.appendChild(noResultsMessage);
return;
Expand Down Expand Up @@ -316,15 +310,6 @@ Source:
string.substring(0, index) + `${pre}<b>${word}</b>${suff}` + string.substring(index + fullmatch.length);
}
});
return cleanHugoFormatStringForDesc(string);
}

function cleanHugoFormatStringForDesc(string) {
// replaces the `link` text that appears right after hugo headers
return string.replace(/ link([A-Z])/i, " $1");
}

function getNoResultsMessage(searchQuery) {
return `${i18nSearchNoResults} "<strong>${searchQuery}</strong>"`;
return string;
}
})();
15 changes: 13 additions & 2 deletions layouts/partials/docs/footer/flexsearch.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,26 @@
{{- with .Keywords -}}
{{- $keywords = delimit . ", " -}}
{{- end -}}

{{- $content := .Content -}}
{{/* Unescape html so we can run "plainify" to remove it completely */}}
{{- $content = $content | htmlUnescape -}}
{{/* Removes the heading id attribute (hidden) "link" text */}}
{{- $content = replaceRE `<a href="#[\w-]+" class="anchor" aria-hidden="true"><i class="material-icons align-middle">link<\/i><\/a>` "" $content -}}
{{/* Remove all html & markdown formatting */}}
{{- $content = $content | plainify -}}
{{/* Words that are to long cause flexsearch to freeze, so remove them from text we give to index */}}
{{- $content = replaceRE `\w{40,}` "-" $content -}}

{{- $indexEntry := newScratch -}}
{{- $indexEntry.Set "id" $index -}}
{{- $indexEntry.Set "href" .RelPermalink -}}
{{- $indexEntry.Set "title" .Title -}}
{{- $indexEntry.Set "description" $description -}}
{{- $indexEntry.Set "keywords" $keywords -}}
{{- $indexEntry.Set "content" (.Content | htmlUnescape | plainify) -}}

{{- $indexEntry.Set "content" $content -}}


{{- $indexKey := printf "%s-%s" .Section $subversion -}}
{{- $indexVersionMap.Add $indexKey (slice $indexEntry.Values) -}}
{{- end -}}
Expand Down

0 comments on commit d379366

Please sign in to comment.