From 86cbc1b682c999f62708bb3aea82615e862c7f95 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Mon, 3 Jan 2022 14:15:05 +0100 Subject: [PATCH 01/49] fix(README): demo site link closes #449 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ddacf82..6ac9e91 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Demo -[Example Site](https://theme-stack.jimmycai.com/) +[Example Site](https://demo.stack.jimmycai.com/) [![Netlify Status](https://api.netlify.com/api/v1/badges/a2d2807a-a905-4bcb-97da-8da8d847da3d/deploy-status)](https://app.netlify.com/sites/hugo-theme-stack/deploys) @@ -77,4 +77,4 @@ Some references that I took while building this theme: | Project | Licence| | ------- | ------| | [artchen/hexo-theme-element](https://github.com/artchen/hexo-theme-element) | [MIT](https://github.com/artchen/hexo-theme-element/blob/master/LICENSE) | -| [MunifTanjim/minimo](https://github.com/MunifTanjim/minimo) | [MIT](https://github.com/MunifTanjim/minimo/blob/master/LICENSE) | \ No newline at end of file +| [MunifTanjim/minimo](https://github.com/MunifTanjim/minimo) | [MIT](https://github.com/MunifTanjim/minimo/blob/master/LICENSE) | From 4764a92df3f929f98741e59cee79fe90add523d3 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Tue, 18 Jan 2022 23:19:30 +0100 Subject: [PATCH 02/49] refactor(search): avoid issue with one character keyword (#447) * refactor(search): avoid issue with one character keyword closes https://github.com/CaiJimmy/hugo-theme-stack/issues/184 * Remove keyword sorts * fix typo: secion -> section * fix(search): avoid matching html entity * Use | operator to concatenate keywords Idea from https://github.com/CaiJimmy/hugo-theme-stack/pull/436 * Add missing `matchCount` * Limit preview length * Don't add ellipsis to title * add comment to `processMatches` * Initialize DOMParser only once * Remove marker function * Deal with blank search * Use const keyword for constant arrays --- assets/jsconfig.json | 2 + assets/ts/search.tsx | 174 +++++++++++++++++++++++++++++-------------- 2 files changed, 120 insertions(+), 56 deletions(-) diff --git a/assets/jsconfig.json b/assets/jsconfig.json index 9321136..040177a 100644 --- a/assets/jsconfig.json +++ b/assets/jsconfig.json @@ -6,5 +6,7 @@ "*" ] }, + "lib": ["es2020", "dom"], + "jsx": "preserve" } } \ No newline at end of file diff --git a/assets/ts/search.tsx b/assets/ts/search.tsx index 8e4eb6f..68db7b3 100644 --- a/assets/ts/search.tsx +++ b/assets/ts/search.tsx @@ -8,6 +8,11 @@ interface pageData { matchCount: number } +interface match { + start: number, + end: number +} + /** * Escape HTML tags as HTML entities * Edited from: @@ -53,79 +58,131 @@ class Search { this.bindSearchForm(); } - private async searchKeywords(keywords: string[]) { - const rawData = await this.getData(); - let results: pageData[] = []; - - /// Sort keywords by their length - keywords.sort((a, b) => { - return b.length - a.length + /** + * 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; }); + 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; + } + + let j = i + 1, + end = item.end; + + /// 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; + } + + resultArray.push(`${replaceHTMLEnt(str.substring(item.start, end))}`); + charCount += end - item.start; + + i = j; + lastIndex = end; + + if (ellipsis && charCount > charLimit) break; + } + + /// Add the rest of the string + if (lastIndex < str.length) { + let end = str.length; + if (ellipsis) end = Math.min(end, lastIndex + offset); + + resultArray.push(`${replaceHTMLEnt(str.substring(lastIndex, end))}`); + + if (ellipsis && end != str.length) { + resultArray.push(` [...]`); + } + } + + return resultArray.join(''); + } + + 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[] = []; + let result = { ...item, preview: '', matchCount: 0 } - let matched = false; - - for (const keyword of keywords) { - if (keyword === '') continue; - - const regex = new RegExp(escapeRegExp(replaceHTMLEnt(keyword)), 'gi'); - - const contentMatch = regex.exec(result.content); - regex.lastIndex = 0; /// Reset regex - - const titleMatch = regex.exec(result.title); - regex.lastIndex = 0; /// Reset regex - - if (titleMatch) { - result.title = result.title.replace(regex, Search.marker); - } - - if (titleMatch || contentMatch) { - matched = true; - ++result.matchCount; - - let start = 0, - end = 100; - - if (contentMatch) { - start = contentMatch.index - 20; - end = contentMatch.index + 80 - - if (start < 0) start = 0; - } - - if (result.preview.indexOf(keyword) !== -1) { - result.preview = result.preview.replace(regex, Search.marker); - } - else { - if (start !== 0) result.preview += `[...] `; - result.preview += `${result.content.slice(start, end).replace(regex, Search.marker)} `; - } - } + const contentMatchAll = item.content.matchAll(regex); + for (const match of Array.from(contentMatchAll)) { + contentMatches.push({ + start: match.index, + end: match.index + match[0].length + }); } - if (matched) { - result.preview += '[...]'; - results.push(result); + 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); } - /** Result with more matches appears first */ + /// Result with more matches appears first return results.sort((a, b) => { return b.matchCount - a.matchCount; }); } - public static marker(match) { - return '' + match + ''; - } - private async doSearch(keywords: string[]) { const startTime = performance.now(); @@ -150,6 +207,11 @@ class Search { /// Not fetched yet const jsonURL = this.form.dataset.json; this.data = await fetch(jsonURL).then(res => res.json()); + const parser = new DOMParser(); + + for (const item of this.data) { + item.content = parser.parseFromString(item.content, 'text/html').body.innerText; + } } return this.data; @@ -160,7 +222,7 @@ class Search { const eventHandler = (e) => { e.preventDefault(); - const keywords = this.input.value; + const keywords = this.input.value.trim(); Search.updateQueryString(keywords, true); @@ -225,7 +287,7 @@ class Search {

- +
{item.image &&
From ca260db07e396fba61955e0262930fad3daa64cd Mon Sep 17 00:00:00 2001 From: zhixuan <59254886+zhixuan2333@users.noreply.github.com> Date: Wed, 19 Jan 2022 09:12:29 +0900 Subject: [PATCH 03/49] fix(shortcode): conflict with gist shortcode (#455) --- assets/ts/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/ts/main.ts b/assets/ts/main.ts index d79c127..625bbbc 100644 --- a/assets/ts/main.ts +++ b/assets/ts/main.ts @@ -58,7 +58,7 @@ let Stack = { /** * Add copy button to code block */ - const codeBlocks = document.querySelectorAll('.article-content .highlight'); + const codeBlocks = document.querySelectorAll('.article-content > div.highlight'); const copyText = `Copy`, copiedText = `Copied!`; codeBlocks.forEach(codeBlock => { From eebaea81e82557663a8a0832d59d07ff531f4664 Mon Sep 17 00:00:00 2001 From: tanmx Date: Wed, 19 Jan 2022 22:22:51 +0800 Subject: [PATCH 04/49] feat(comment): update twikoo version to 1.4.15 (#464) --- layouts/partials/comments/provider/twikoo.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/partials/comments/provider/twikoo.html b/layouts/partials/comments/provider/twikoo.html index 4dbf976..30c7033 100644 --- a/layouts/partials/comments/provider/twikoo.html +++ b/layouts/partials/comments/provider/twikoo.html @@ -1,4 +1,4 @@ - +
+ +
+ + +{{- end -}} From 6474b9dfd8a5164a191b8aa8f33900e79a2ef0c6 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Sun, 23 Jan 2022 12:59:21 +0000 Subject: [PATCH 12/49] fix(comments): fix cactus comments closes https://github.com/CaiJimmy/hugo-theme-stack/issues/470 --- data/external.yaml | 3 +-- layouts/partials/comments/provider/cactus.html | 2 +- layouts/partials/helper/external.html | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/data/external.yaml b/data/external.yaml index 4b2713f..fd2b665 100644 --- a/data/external.yaml +++ b/data/external.yaml @@ -41,7 +41,6 @@ Cactus: - src: https://latest.cactus.chat/cactus.js integrity: type: script - defer: false - src: https://latest.cactus.chat/style.css integrity: - type: style \ No newline at end of file + type: style diff --git a/layouts/partials/comments/provider/cactus.html b/layouts/partials/comments/provider/cactus.html index 8224c8b..ae172d3 100644 --- a/layouts/partials/comments/provider/cactus.html +++ b/layouts/partials/comments/provider/cactus.html @@ -1,5 +1,5 @@ {{- with .Site.Params.comments.cactus -}} -{{- partial "helper/external" (dict "Context" . "Namespace" "Cactus") -}} +{{- partial "helper/external" (dict "Context" $ "Namespace" "Cactus") -}}