2020-09-26 05:40:33 -04:00
|
|
|
interface pageData {
|
|
|
|
title: string,
|
|
|
|
date: string,
|
|
|
|
permalink: string,
|
|
|
|
content: string,
|
|
|
|
image?: string,
|
|
|
|
preview: string,
|
|
|
|
matchCount: number
|
|
|
|
}
|
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
interface match {
|
|
|
|
start: number,
|
|
|
|
end: number
|
|
|
|
}
|
|
|
|
|
2020-09-26 16:50:23 -04:00
|
|
|
/**
|
|
|
|
* Escape HTML tags as HTML entities
|
|
|
|
* Edited from:
|
|
|
|
* @link https://stackoverflow.com/a/5499821
|
|
|
|
*/
|
|
|
|
const tagsToReplace = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
2020-10-04 09:53:27 -04:00
|
|
|
'"': '"',
|
|
|
|
'…': '…'
|
2020-09-26 16:50:23 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
function replaceTag(tag) {
|
|
|
|
return tagsToReplace[tag] || tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceHTMLEnt(str) {
|
|
|
|
return str.replace(/[&<>"]/g, replaceTag);
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
2020-09-26 05:40:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
class Search {
|
|
|
|
private data: pageData[];
|
|
|
|
private form: HTMLFormElement;
|
|
|
|
private input: HTMLInputElement;
|
|
|
|
private list: HTMLDivElement;
|
|
|
|
private resultTitle: HTMLHeadElement;
|
2020-11-06 05:49:30 -05:00
|
|
|
private resultTitleTemplate: string;
|
2020-11-06 05:33:51 -05:00
|
|
|
|
2020-11-06 05:49:30 -05:00
|
|
|
constructor({ form, input, list, resultTitle, resultTitleTemplate }) {
|
2020-11-06 05:33:51 -05:00
|
|
|
this.form = form;
|
|
|
|
this.input = input;
|
|
|
|
this.list = list;
|
|
|
|
this.resultTitle = resultTitle;
|
2020-11-06 05:49:30 -05:00
|
|
|
this.resultTitleTemplate = resultTitleTemplate;
|
2020-11-06 05:33:51 -05:00
|
|
|
|
|
|
|
this.handleQueryString();
|
|
|
|
this.bindQueryStringChange();
|
|
|
|
this.bindSearchForm();
|
2020-09-26 05:40:33 -04:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
/**
|
|
|
|
* Processes search matches
|
|
|
|
* @param str original text
|
|
|
|
* @param matches array of matches
|
|
|
|
* @param ellipsis whether to add ellipsis to the end of each match
|
|
|
|
* @param charLimit max length of preview string
|
|
|
|
* @param offset how many characters before and after the match to include in preview
|
|
|
|
* @returns preview string
|
|
|
|
*/
|
|
|
|
private static processMatches(str: string, matches: match[], ellipsis: boolean = true, charLimit = 140, offset = 20): string {
|
|
|
|
matches.sort((a, b) => {
|
|
|
|
return a.start - b.start;
|
2020-11-06 05:33:51 -05:00
|
|
|
});
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
let i = 0,
|
|
|
|
lastIndex = 0,
|
|
|
|
charCount = 0;
|
|
|
|
|
|
|
|
const resultArray: string[] = [];
|
|
|
|
|
|
|
|
while (i < matches.length) {
|
|
|
|
const item = matches[i];
|
|
|
|
|
|
|
|
/// item.start >= lastIndex (equal only for the first iteration)
|
|
|
|
/// because of the while loop that comes after, iterating over variable j
|
|
|
|
|
|
|
|
if (ellipsis && item.start - offset > lastIndex) {
|
|
|
|
resultArray.push(`${replaceHTMLEnt(str.substring(lastIndex, lastIndex + offset))} [...] `);
|
|
|
|
resultArray.push(`${replaceHTMLEnt(str.substring(item.start - offset, item.start))}`);
|
|
|
|
charCount += offset * 2;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/// If the match is too close to the end of last match, don't add ellipsis
|
|
|
|
resultArray.push(replaceHTMLEnt(str.substring(lastIndex, item.start)));
|
|
|
|
charCount += item.start - lastIndex;
|
2020-11-06 05:33:51 -05:00
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
let j = i + 1,
|
|
|
|
end = item.end;
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
/// Include as many matches as possible
|
|
|
|
/// [item.start, end] is the range of the match
|
|
|
|
while (j < matches.length && matches[j].start <= end) {
|
|
|
|
end = Math.max(matches[j].end, end);
|
|
|
|
++j;
|
|
|
|
}
|
2020-10-04 09:53:27 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
resultArray.push(`<mark>${replaceHTMLEnt(str.substring(item.start, end))}</mark>`);
|
|
|
|
charCount += end - item.start;
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
i = j;
|
|
|
|
lastIndex = end;
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
if (ellipsis && charCount > charLimit) break;
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
/// Add the rest of the string
|
|
|
|
if (lastIndex < str.length) {
|
|
|
|
let end = str.length;
|
|
|
|
if (ellipsis) end = Math.min(end, lastIndex + offset);
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
resultArray.push(`${replaceHTMLEnt(str.substring(lastIndex, end))}`);
|
2020-10-04 09:53:27 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
if (ellipsis && end != str.length) {
|
|
|
|
resultArray.push(` [...]`);
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
return resultArray.join('');
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
private async searchKeywords(keywords: string[]) {
|
|
|
|
const rawData = await this.getData();
|
|
|
|
const results: pageData[] = [];
|
|
|
|
|
|
|
|
const regex = new RegExp(keywords.filter((v, index, arr) => {
|
|
|
|
arr[index] = escapeRegExp(v);
|
|
|
|
return v.trim() !== '';
|
|
|
|
}).join('|'), 'gi');
|
|
|
|
|
|
|
|
for (const item of rawData) {
|
|
|
|
const titleMatches: match[] = [],
|
|
|
|
contentMatches: match[] = [];
|
2020-11-06 05:33:51 -05:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
let result = {
|
|
|
|
...item,
|
|
|
|
preview: '',
|
|
|
|
matchCount: 0
|
2020-11-06 05:33:51 -05:00
|
|
|
}
|
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
const contentMatchAll = item.content.matchAll(regex);
|
|
|
|
for (const match of Array.from(contentMatchAll)) {
|
|
|
|
contentMatches.push({
|
|
|
|
start: match.index,
|
|
|
|
end: match.index + match[0].length
|
|
|
|
});
|
2020-11-06 05:33:51 -05:00
|
|
|
}
|
2022-01-18 17:19:30 -05:00
|
|
|
|
|
|
|
const titleMatchAll = item.title.matchAll(regex);
|
|
|
|
for (const match of Array.from(titleMatchAll)) {
|
|
|
|
titleMatches.push({
|
|
|
|
start: match.index,
|
|
|
|
end: match.index + match[0].length
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (titleMatches.length > 0) result.title = Search.processMatches(result.title, titleMatches, false);
|
|
|
|
if (contentMatches.length > 0) {
|
|
|
|
result.preview = Search.processMatches(result.content, contentMatches);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/// If there are no matches in the content, use the first 140 characters as preview
|
|
|
|
result.preview = replaceHTMLEnt(result.content.substring(0, 140));
|
|
|
|
}
|
|
|
|
|
|
|
|
result.matchCount = titleMatches.length + contentMatches.length;
|
|
|
|
if (result.matchCount > 0) results.push(result);
|
2020-11-06 05:33:51 -05:00
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2022-01-18 17:19:30 -05:00
|
|
|
/// Result with more matches appears first
|
2020-11-06 05:33:51 -05:00
|
|
|
return results.sort((a, b) => {
|
|
|
|
return b.matchCount - a.matchCount;
|
|
|
|
});
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
private async doSearch(keywords: string[]) {
|
|
|
|
const startTime = performance.now();
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
const results = await this.searchKeywords(keywords);
|
|
|
|
this.clear();
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
for (const item of results) {
|
|
|
|
this.list.append(Search.render(item));
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
const endTime = performance.now();
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:49:30 -05:00
|
|
|
this.resultTitle.innerText = this.generateResultTitle(results.length, ((endTime - startTime) / 1000).toPrecision(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
private generateResultTitle(resultLen, time) {
|
|
|
|
return this.resultTitleTemplate.replace("#PAGES_COUNT", resultLen).replace("#TIME_SECONDS", time);
|
2020-11-06 05:33:51 -05:00
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
public async getData() {
|
|
|
|
if (!this.data) {
|
|
|
|
/// Not fetched yet
|
|
|
|
const jsonURL = this.form.dataset.json;
|
|
|
|
this.data = await fetch(jsonURL).then(res => res.json());
|
2022-01-18 17:19:30 -05:00
|
|
|
const parser = new DOMParser();
|
|
|
|
|
|
|
|
for (const item of this.data) {
|
|
|
|
item.content = parser.parseFromString(item.content, 'text/html').body.innerText;
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
return this.data;
|
|
|
|
}
|
2020-09-26 16:50:23 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
private bindSearchForm() {
|
|
|
|
let lastSearch = '';
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
const eventHandler = (e) => {
|
|
|
|
e.preventDefault();
|
2022-01-18 17:19:30 -05:00
|
|
|
const keywords = this.input.value.trim();
|
2020-09-26 16:50:23 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
Search.updateQueryString(keywords, true);
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
if (keywords === '') {
|
2023-08-12 07:03:45 -04:00
|
|
|
lastSearch = '';
|
2020-11-06 05:33:51 -05:00
|
|
|
return this.clear();
|
2020-09-26 05:40:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
if (lastSearch === keywords) return;
|
|
|
|
lastSearch = keywords;
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
this.doSearch(keywords.split(' '));
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
this.input.addEventListener('input', eventHandler);
|
|
|
|
this.input.addEventListener('compositionend', eventHandler);
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
private clear() {
|
|
|
|
this.list.innerHTML = '';
|
|
|
|
this.resultTitle.innerText = '';
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
private bindQueryStringChange() {
|
|
|
|
window.addEventListener('popstate', (e) => {
|
|
|
|
this.handleQueryString()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleQueryString() {
|
|
|
|
const pageURL = new URL(window.location.toString());
|
|
|
|
const keywords = pageURL.searchParams.get('keyword');
|
|
|
|
this.input.value = keywords;
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
if (keywords) {
|
|
|
|
this.doSearch(keywords.split(' '));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.clear()
|
2020-09-26 05:40:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
private static updateQueryString(keywords: string, replaceState = false) {
|
|
|
|
const pageURL = new URL(window.location.toString());
|
|
|
|
|
|
|
|
if (keywords === '') {
|
|
|
|
pageURL.searchParams.delete('keyword')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pageURL.searchParams.set('keyword', keywords);
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
if (replaceState) {
|
|
|
|
window.history.replaceState('', '', pageURL.toString());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.history.pushState('', '', pageURL.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static render(item: pageData) {
|
|
|
|
return <article>
|
|
|
|
<a href={item.permalink}>
|
|
|
|
<div class="article-details">
|
|
|
|
<h2 class="article-title" dangerouslySetInnerHTML={{ __html: item.title }}></h2>
|
2022-01-18 17:19:30 -05:00
|
|
|
<section class="article-preview" dangerouslySetInnerHTML={{ __html: item.preview }}></section>
|
2020-09-26 05:40:33 -04:00
|
|
|
</div>
|
2020-11-06 05:33:51 -05:00
|
|
|
{item.image &&
|
|
|
|
<div class="article-image">
|
|
|
|
<img src={item.image} loading="lazy" />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</a>
|
|
|
|
</article>;
|
|
|
|
}
|
2020-09-26 05:40:33 -04:00
|
|
|
}
|
|
|
|
|
2020-11-06 05:49:30 -05:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
searchResultTitleTemplate: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 05:33:51 -05:00
|
|
|
window.addEventListener('load', () => {
|
|
|
|
setTimeout(function () {
|
|
|
|
const searchForm = document.querySelector('.search-form') as HTMLFormElement,
|
|
|
|
searchInput = searchForm.querySelector('input') as HTMLInputElement,
|
|
|
|
searchResultList = document.querySelector('.search-result--list') as HTMLDivElement,
|
|
|
|
searchResultTitle = document.querySelector('.search-result--title') as HTMLHeadingElement;
|
|
|
|
|
|
|
|
new Search({
|
|
|
|
form: searchForm,
|
|
|
|
input: searchInput,
|
|
|
|
list: searchResultList,
|
2020-11-06 05:49:30 -05:00
|
|
|
resultTitle: searchResultTitle,
|
|
|
|
resultTitleTemplate: window.searchResultTitleTemplate
|
2020-11-06 05:33:51 -05:00
|
|
|
});
|
|
|
|
}, 0);
|
|
|
|
})
|
|
|
|
|
|
|
|
export default Search;
|