Highlight CJK mention keywords in sentense (#2590)

This commit is contained in:
Yusuke Nemoto 2019-03-07 23:04:10 +09:00 committed by Saturnino Abril
parent ede80d0fe4
commit e1e1e7cf53
2 changed files with 71 additions and 1 deletions

View file

@ -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] === '') {

View file

@ -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) {