From 20990fcf08d0c9e796adbc7c9b5a28d991e7a108 Mon Sep 17 00:00:00 2001 From: Ovler <86chengyujin@gmail.com> Date: Sat, 5 Feb 2022 23:07:08 +0800 Subject: [PATCH 1/4] Try to fix CJK search --- pages/api/search.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pages/api/search.ts b/pages/api/search.ts index d140f6c..0edae83 100644 --- a/pages/api/search.ts +++ b/pages/api/search.ts @@ -12,7 +12,10 @@ import siteConfig from '../../config/site.config' * @returns Sanitised query string which replaces non-alphanumeric characters with ' ' */ function sanitiseQuery(query: string): string { - const sanitisedQuery = query.replace(/[^a-zA-Z0-9]/g, ' ') +/** const sanitisedQuery = query.replace(/[^a-zA-Z0-9]/g, ' ') + * Actually this will cause problems with CJK only query words as it will remove every character. + * After removing it, CJK search will be much improved. + */ return encodeURIComponent(sanitisedQuery) } From 234d826d3a95415490c3c534a5c460ad3e2c7f86 Mon Sep 17 00:00:00 2001 From: Ovler <86chengyujin@gmail.com> Date: Sat, 5 Feb 2022 23:36:49 +0800 Subject: [PATCH 2/4] fix typo --- pages/api/search.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/api/search.ts b/pages/api/search.ts index 0edae83..f7c07f9 100644 --- a/pages/api/search.ts +++ b/pages/api/search.ts @@ -12,8 +12,8 @@ import siteConfig from '../../config/site.config' * @returns Sanitised query string which replaces non-alphanumeric characters with ' ' */ function sanitiseQuery(query: string): string { -/** const sanitisedQuery = query.replace(/[^a-zA-Z0-9]/g, ' ') - * Actually this will cause problems with CJK only query words as it will remove every character. + const sanitisedQuery = query +/** Actually this will cause problems with CJK only query words as it will remove every character. * After removing it, CJK search will be much improved. */ return encodeURIComponent(sanitisedQuery) From d4998466debc198ad1e8e882ada5895dbab90eb0 Mon Sep 17 00:00:00 2001 From: Ovler <86chengyujin@gmail.com> Date: Sun, 6 Feb 2022 15:27:06 +0800 Subject: [PATCH 3/4] Update search.ts --- pages/api/search.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pages/api/search.ts b/pages/api/search.ts index f7c07f9..ece3ccd 100644 --- a/pages/api/search.ts +++ b/pages/api/search.ts @@ -9,13 +9,10 @@ import siteConfig from '../../config/site.config' * Sanitize the search query * * @param query User search query, which may contain special characters - * @returns Sanitised query string which replaces non-alphanumeric characters with ' ' + * @returns Sanitised query string which encodes the '<' and '>' characters, replaces '?' and '/' characters with ' ', and replaces ''' with '''' accroading to https://stackoverflow.com/questions/41491222/single-quote-escaping-in-microsoft-graph. */ function sanitiseQuery(query: string): string { - const sanitisedQuery = query -/** Actually this will cause problems with CJK only query words as it will remove every character. - * After removing it, CJK search will be much improved. - */ + const sanitisedQuery = query.replace(/'/g, "''").replace('<', ' < ').replace('>', ' > ').replace('?', ' ').replace('/', ' ') return encodeURIComponent(sanitisedQuery) } From 96b3c92df941218e39cfadb9c890133a04cd9182 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Sun, 6 Feb 2022 16:01:21 +0800 Subject: [PATCH 4/4] some housekeeping --- pages/api/search.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pages/api/search.ts b/pages/api/search.ts index ece3ccd..1762264 100644 --- a/pages/api/search.ts +++ b/pages/api/search.ts @@ -9,10 +9,19 @@ import siteConfig from '../../config/site.config' * Sanitize the search query * * @param query User search query, which may contain special characters - * @returns Sanitised query string which encodes the '<' and '>' characters, replaces '?' and '/' characters with ' ', and replaces ''' with '''' accroading to https://stackoverflow.com/questions/41491222/single-quote-escaping-in-microsoft-graph. + * @returns Sanitised query string, which: + * - encodes the '<' and '>' characters, + * - replaces '?' and '/' characters with ' ', + * - replaces ''' with '''' + * Reference: https://stackoverflow.com/questions/41491222/single-quote-escaping-in-microsoft-graph. */ function sanitiseQuery(query: string): string { - const sanitisedQuery = query.replace(/'/g, "''").replace('<', ' < ').replace('>', ' > ').replace('?', ' ').replace('/', ' ') + const sanitisedQuery = query + .replace(/'/g, "''") + .replace('<', ' < ') + .replace('>', ' > ') + .replace('?', ' ') + .replace('/', ' ') return encodeURIComponent(sanitisedQuery) }