diff --git a/app/components/markdown/transform.js b/app/components/markdown/transform.js index 3879b0720..bb1502118 100644 --- a/app/components/markdown/transform.js +++ b/app/components/markdown/transform.js @@ -7,6 +7,8 @@ import {escapeRegex} from 'app/utils/markdown'; /* eslint-disable no-underscore-dangle */ +const cjkPattern = /[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf\uac00-\ud7a3]/; + // Combines adjacent text nodes into a single text node to make further transformation easier export function combineTextNodes(ast) { const walker = ast.walker(); @@ -315,7 +317,12 @@ export function getFirstMention(str, mentionKeys) { } const flags = mention.caseSensitive ? '' : 'i'; - const pattern = new RegExp(`\\b${escapeRegex(mention.key)}_*\\b`, flags); + let pattern; + if (cjkPattern.test(mention.key)) { + pattern = new RegExp(`${escapeRegex(mention.key)}`, flags); + } else { + pattern = new RegExp(`\\b${escapeRegex(mention.key)}_*\\b`, flags); + } const match = pattern.exec(str); if (!match || match[0] === '') { diff --git a/app/components/markdown/transform.test.js b/app/components/markdown/transform.test.js index 42f1f0cc5..88a2e1fb6 100644 --- a/app/components/markdown/transform.test.js +++ b/app/components/markdown/transform.test.js @@ -2693,6 +2693,64 @@ describe('Components.Markdown.transform', () => { }], }], }, + }, { + name: 'multibyte keyword', + input: '我爱吃番茄炒饭', + mentionKeys: [{key: '番茄'}], + expected: { + type: 'document', + children: [{ + type: 'paragraph', + children: [{ + type: 'text', + literal: '我爱吃', + }, { + type: 'mention_highlight', + children: [{ + type: 'text', + literal: '番茄', + }], + }, { + type: 'text', + literal: '炒饭', + }], + }], + }, + }, { + name: 'multiple multibyte keywords', + input: 'CJK is 中國日本한국.', + mentionKeys: [{key: '中國'}, {key: '日本'}, {key: '한국'}], + expected: { + type: 'document', + children: [{ + type: 'paragraph', + children: [{ + type: 'text', + literal: 'CJK is ', + }, { + type: 'mention_highlight', + children: [{ + type: 'text', + literal: '中國', + }], + }, { + type: 'mention_highlight', + children: [{ + type: 'text', + literal: '日本', + }], + }, { + type: 'mention_highlight', + children: [{ + type: 'text', + literal: '한국', + }], + }, { + type: 'text', + literal: '.', + }], + }], + }, }]; for (const test of tests) { @@ -2763,6 +2821,11 @@ describe('Components.Markdown.transform', () => { input: 'apple banana orange', mentionKeys: [{key: ''}], expected: {index: -1, mention: null}, + }, { + name: 'multibyte key', + input: '좋은 하루 되세요.', + mentionKeys: [{key: '하루'}], + expected: {index: 3, mention: {key: '하루'}}, }]; for (const test of tests) {