[MFM] Better hashtag parsing
This commit is contained in:
parent
02b07c1b5b
commit
3b10e93efe
2 changed files with 46 additions and 3 deletions
|
@ -112,9 +112,27 @@ 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 (match[1].match(/^[0-9]+$/)) return P.makeFailure(i, 'not a hashtag');
|
let hashtag = match[1];
|
||||||
if (input[i - 1] != '\n' && input[i - 1] != ' ' && input[i - 1] != null) return P.makeFailure(i, 'require space before "#"');
|
let pendingBracket = 0;
|
||||||
return P.makeSuccess(i + match[0].length, makeNode('hashtag', { hashtag: match[1] }));
|
const end = hashtag.split('').findIndex(char => {
|
||||||
|
if (char == ')') {
|
||||||
|
if (pendingBracket > 0) {
|
||||||
|
pendingBracket--;
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} else if (char == '(') {
|
||||||
|
pendingBracket++;
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (end > 0) hashtag = hashtag.substr(0, end);
|
||||||
|
if (hashtag.match(/^[0-9]+$/)) return P.makeFailure(i, 'not a hashtag');
|
||||||
|
if (!['\n', ' ', '(', null, undefined].includes(input[i - 1])) return P.makeFailure(i, 'require space before "#"');
|
||||||
|
return P.makeSuccess(i + ('#' + hashtag).length, makeNode('hashtag', { hashtag: hashtag }));
|
||||||
}),
|
}),
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
|
|
25
test/mfm.ts
25
test/mfm.ts
|
@ -212,12 +212,37 @@ describe('Text', () => {
|
||||||
], tokens);
|
], tokens);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('with brackets', () => {
|
||||||
|
const tokens = analyze('(#foo)');
|
||||||
|
assert.deepEqual([
|
||||||
|
text('('),
|
||||||
|
node('hashtag', { hashtag: 'foo' }),
|
||||||
|
text(')'),
|
||||||
|
], tokens);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('with brackets (space before)', () => {
|
||||||
|
const tokens = analyze('(bar #foo)');
|
||||||
|
assert.deepEqual([
|
||||||
|
text('(bar '),
|
||||||
|
node('hashtag', { hashtag: 'foo' }),
|
||||||
|
text(')'),
|
||||||
|
], tokens);
|
||||||
|
});
|
||||||
|
|
||||||
it('disallow number only', () => {
|
it('disallow number only', () => {
|
||||||
const tokens = analyze('#123');
|
const tokens = analyze('#123');
|
||||||
assert.deepEqual([
|
assert.deepEqual([
|
||||||
text('#123'),
|
text('#123'),
|
||||||
], tokens);
|
], tokens);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('disallow number only (with brackets)', () => {
|
||||||
|
const tokens = analyze('(#123)');
|
||||||
|
assert.deepEqual([
|
||||||
|
text('(#123)'),
|
||||||
|
], tokens);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('quote', () => {
|
describe('quote', () => {
|
||||||
|
|
Loading…
Reference in a new issue