From 8f5f3985f4609776eb21ca8d89a67ef10886df52 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 21 Nov 2018 08:30:29 +0900 Subject: [PATCH] [MFM] Fix hashtag parsing --- src/mfm/parser.ts | 2 +- test/mfm.ts | 59 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/mfm/parser.ts b/src/mfm/parser.ts index 5f89696c20..4ad38d7a24 100644 --- a/src/mfm/parser.ts +++ b/src/mfm/parser.ts @@ -110,7 +110,7 @@ const mfm = P.createLanguage({ const text = input.substr(i); const match = text.match(/^#([^\s\.,!\?#]+)/i); if (!match) return P.makeFailure(i, 'not a hashtag'); - if (input[i - 1] != ' ' && input[i - 1] != null) return P.makeFailure(i, 'require space before "#"'); + if (input[i - 1] != '\n' && input[i - 1] != ' ' && input[i - 1] != null) return P.makeFailure(i, 'require space before "#"'); return P.makeSuccess(i + match[0].length, makeNode('hashtag', { hashtag: match[1] })); }), //#endregion diff --git a/test/mfm.ts b/test/mfm.ts index f020ffd5a5..0f878ea9b1 100644 --- a/test/mfm.ts +++ b/test/mfm.ts @@ -162,27 +162,48 @@ describe('Text', () => { }); }); - it('hashtag', () => { - const tokens1 = analyze('Strawberry Pasta #alice'); - assert.deepEqual([ - text('Strawberry Pasta '), - node('hashtag', { hashtag: 'alice' }) - ], tokens1); + describe('hashtag', () => { + it('simple', () => { + const tokens = analyze('#alice'); + assert.deepEqual([ + node('hashtag', { hashtag: 'alice' }) + ], tokens); + }); - const tokens2 = analyze('Foo #bar, baz #piyo.'); - assert.deepEqual([ - text('Foo '), - node('hashtag', { hashtag: 'bar' }), - text(', baz '), - node('hashtag', { hashtag: 'piyo' }), - text('.'), - ], tokens2); + it('after line break', () => { + const tokens = analyze('foo\n#alice'); + assert.deepEqual([ + text('foo\n'), + node('hashtag', { hashtag: 'alice' }) + ], tokens); + }); - const tokens3 = analyze('#Foo!'); - assert.deepEqual([ - node('hashtag', { hashtag: 'Foo' }), - text('!'), - ], tokens3); + it('with text', () => { + const tokens = analyze('Strawberry Pasta #alice'); + assert.deepEqual([ + text('Strawberry Pasta '), + node('hashtag', { hashtag: 'alice' }) + ], tokens); + }); + + it('ignore comma and period', () => { + const tokens = analyze('Foo #bar, baz #piyo.'); + assert.deepEqual([ + text('Foo '), + node('hashtag', { hashtag: 'bar' }), + text(', baz '), + node('hashtag', { hashtag: 'piyo' }), + text('.'), + ], tokens); + }); + + it('ignore exclamation mark', () => { + const tokens = analyze('#Foo!'); + assert.deepEqual([ + node('hashtag', { hashtag: 'Foo' }), + text('!'), + ], tokens); + }); }); describe('quote', () => {