From 21e929dca299c65aba8ee8e3318216ad56acbb4c Mon Sep 17 00:00:00 2001 From: Pattara Kiatisevi Date: Wed, 4 Dec 2024 01:40:07 +0700 Subject: [PATCH] Fix Thai search keywords not properly hi-lighted in the search results. (#8347) --- app/utils/markdown/index.test.ts | 5 +++++ app/utils/markdown/index.ts | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/utils/markdown/index.test.ts b/app/utils/markdown/index.test.ts index 63f286851..6d4f34c25 100644 --- a/app/utils/markdown/index.test.ts +++ b/app/utils/markdown/index.test.ts @@ -185,6 +185,11 @@ describe('Utility functions', () => { expect(result.pattern).toEqual(/()(你好)/gi); }); + it('should create regex for Thai characters', () => { + const result = convertSearchTermToRegex('สวัสดี'); + expect(result.pattern).toEqual(/()(สวัสดี)/gi); + }); + it('should create regex for wildcard at the end', () => { const result = convertSearchTermToRegex('hello*'); expect(result.pattern).toEqual(/\b()(hello)/gi); diff --git a/app/utils/markdown/index.ts b/app/utils/markdown/index.ts index 95824ffcd..ab81985b3 100644 --- a/app/utils/markdown/index.ts +++ b/app/utils/markdown/index.ts @@ -18,9 +18,9 @@ type LanguageObject = { }; } -// pattern to detect the existence of a Chinese, Japanese, or Korean character in a string +// pattern to detect the existence of a Chinese, Japanese, Korean, or Thai character in a string // http://stackoverflow.com/questions/15033196/using-javascript-to-check-whether-a-string-contains-japanese-characters-includi -const cjkPattern = /[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf\uac00-\ud7a3]/; +const cjkPattern = /[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf\uac00-\ud7a3\u0e00-\u0e7f]/; const puncStart = /^[^\p{L}\d\s#]+/u; const puncEnd = /[^\p{L}\d\s]+$/u; @@ -347,7 +347,7 @@ export function convertSearchTermToRegex(term: string): SearchPattern { let pattern; if (cjkPattern.test(term)) { - // term contains Chinese, Japanese, or Korean characters so don't mark word boundaries + // term contains Chinese, Japanese, Korean or Thai characters so don't mark word boundaries pattern = '()(' + escapeRegex(term.replace(/\*/g, '')) + ')'; } else if ((/[^\s][*]$/).test(term)) { pattern = '\\b()(' + escapeRegex(term.substring(0, term.length - 1)) + ')';