From 013f3af23e74b6ac22b91a3b500c248520f9eb34 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 9 Jan 2023 16:05:16 +0900 Subject: [PATCH] improve emoji code parsing Fix #129 --- src/internal/parser.ts | 5 ++++- test/parser.ts | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/internal/parser.ts b/src/internal/parser.ts index 3cca5ea..3d1a04f 100644 --- a/src/internal/parser.ts +++ b/src/internal/parser.ts @@ -626,12 +626,15 @@ export const language = P.createLanguage({ }, emojiCode: r => { + const side = P.notMatch(P.regexp(/[a-z0-9]/i)); const mark = P.str(':'); return P.seq([ + P.alt([P.lineBegin, side]), mark, P.regexp(/[a-z0-9_+-]+/i), mark, - ], 1).map(name => M.EMOJI_CODE(name as string)); + P.alt([P.lineEnd, side]), + ], 2).map(name => M.EMOJI_CODE(name as string)); }, link: r => { diff --git a/test/parser.ts b/test/parser.ts index 6a2393f..77a7a41 100644 --- a/test/parser.ts +++ b/test/parser.ts @@ -34,7 +34,19 @@ describe('SimpleParser', () => { it('between texts', () => { const input = 'foo:bar:baz'; - const output = [TEXT('foo'), EMOJI_CODE('bar'), TEXT('baz')]; + const output = [TEXT('foo:bar:baz')]; + assert.deepStrictEqual(mfm.parseSimple(input), output); + }); + + it('between texts 2', () => { + const input = '12:34:56'; + const output = [TEXT('12:34:56')]; + assert.deepStrictEqual(mfm.parseSimple(input), output); + }); + + it('between texts 3', () => { + const input = 'あ:bar:い'; + const output = [TEXT('あ'), EMOJI_CODE('bar'), TEXT('い')]; assert.deepStrictEqual(mfm.parseSimple(input), output); }); });