From 20c4d59c850a9bcff0a9021735597b0c4dbe1c59 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 17 Sep 2018 22:51:10 +0900 Subject: [PATCH] Better hashtag parsing --- src/mfm/parse/elements/hashtag.ts | 4 ++-- test/mfm.ts | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/mfm/parse/elements/hashtag.ts b/src/mfm/parse/elements/hashtag.ts index f4b6a78fa..339026228 100644 --- a/src/mfm/parse/elements/hashtag.ts +++ b/src/mfm/parse/elements/hashtag.ts @@ -9,9 +9,9 @@ export type TextElementHashtag = { }; export default function(text: string, i: number) { - if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null; + if (!(/^\s#[^\s\.,]+/.test(text) || (i == 0 && /^#[^\s\.,]+/.test(text)))) return null; const isHead = text.startsWith('#'); - const hashtag = text.match(/^\s?#[^\s]+/)[0]; + const hashtag = text.match(/^\s?#[^\s\.,]+/)[0]; const res: any[] = !isHead ? [{ type: 'text', content: text[0] diff --git a/test/mfm.ts b/test/mfm.ts index 901ffb80e..a015092f0 100644 --- a/test/mfm.ts +++ b/test/mfm.ts @@ -71,11 +71,20 @@ describe('Text', () => { }); it('hashtag', () => { - const tokens = analyze('Strawberry Pasta #alice'); + const tokens1 = analyze('Strawberry Pasta #alice'); assert.deepEqual([ { type: 'text', content: 'Strawberry Pasta ' }, { type: 'hashtag', content: '#alice', hashtag: 'alice' } - ], tokens); + ], tokens1); + + const tokens2 = analyze('Foo #bar, baz #piyo.'); + assert.deepEqual([ + { type: 'text', content: 'Foo ' }, + { type: 'hashtag', content: '#bar', hashtag: 'bar' }, + { type: 'text', content: ', baz ' }, + { type: 'hashtag', content: '#piyo', hashtag: 'piyo' }, + { type: 'text', content: '.' } + ], tokens2); }); it('url', () => {