[MFM] Fix hashtag parsing

This commit is contained in:
syuilo 2018-11-21 08:30:29 +09:00
parent ef30f36f55
commit 8f5f3985f4
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
2 changed files with 41 additions and 20 deletions

View file

@ -110,7 +110,7 @@ const mfm = P.createLanguage({
const text = input.substr(i); const text = input.substr(i);
const match = text.match(/^#([^\s\.,!\?#]+)/i); const match = text.match(/^#([^\s\.,!\?#]+)/i);
if (!match) return P.makeFailure(i, 'not a hashtag'); 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] })); return P.makeSuccess(i + match[0].length, makeNode('hashtag', { hashtag: match[1] }));
}), }),
//#endregion //#endregion

View file

@ -162,27 +162,48 @@ describe('Text', () => {
}); });
}); });
it('hashtag', () => { describe('hashtag', () => {
const tokens1 = analyze('Strawberry Pasta #alice'); it('simple', () => {
assert.deepEqual([ const tokens = analyze('#alice');
text('Strawberry Pasta '), assert.deepEqual([
node('hashtag', { hashtag: 'alice' }) node('hashtag', { hashtag: 'alice' })
], tokens1); ], tokens);
});
const tokens2 = analyze('Foo #bar, baz #piyo.'); it('after line break', () => {
assert.deepEqual([ const tokens = analyze('foo\n#alice');
text('Foo '), assert.deepEqual([
node('hashtag', { hashtag: 'bar' }), text('foo\n'),
text(', baz '), node('hashtag', { hashtag: 'alice' })
node('hashtag', { hashtag: 'piyo' }), ], tokens);
text('.'), });
], tokens2);
const tokens3 = analyze('#Foo!'); it('with text', () => {
assert.deepEqual([ const tokens = analyze('Strawberry Pasta #alice');
node('hashtag', { hashtag: 'Foo' }), assert.deepEqual([
text('!'), text('Strawberry Pasta '),
], tokens3); 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', () => { describe('quote', () => {