Improve MFM bracket matching
Co-authored-by: syuilo <syuilotan@yahoo.co.jp>
This commit is contained in:
parent
be0cb88b6c
commit
580191fb17
3 changed files with 115 additions and 28 deletions
95
test/mfm.ts
95
test/mfm.ts
|
@ -6,7 +6,7 @@ import * as assert from 'assert';
|
|||
|
||||
import analyze from '../src/mfm/parse';
|
||||
import toHtml from '../src/mfm/html';
|
||||
import { createTree as tree, createLeaf as leaf, MfmTree } from '../src/mfm/parser';
|
||||
import { createTree as tree, createLeaf as leaf, MfmTree, removeOrphanedBrackets } from '../src/mfm/parser';
|
||||
|
||||
function text(text: string): MfmTree {
|
||||
return leaf('text', { text });
|
||||
|
@ -49,6 +49,99 @@ describe('createTree', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('removeOrphanedBrackets', () => {
|
||||
it('single (contained)', () => {
|
||||
const input = '(foo)';
|
||||
const expected = '(foo)';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('single (head)', () => {
|
||||
const input = '(foo)bar';
|
||||
const expected = '(foo)bar';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('single (tail)', () => {
|
||||
const input = 'foo(bar)';
|
||||
const expected = 'foo(bar)';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('a', () => {
|
||||
const input = '(foo';
|
||||
const expected = '';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('b', () => {
|
||||
const input = ')foo';
|
||||
const expected = '';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('nested', () => {
|
||||
const input = 'foo(「(bar)」)';
|
||||
const expected = 'foo(「(bar)」)';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('no brackets', () => {
|
||||
const input = 'foo';
|
||||
const expected = 'foo';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('with foreign bracket (single)', () => {
|
||||
const input = 'foo(bar))';
|
||||
const expected = 'foo(bar)';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('with foreign bracket (open)', () => {
|
||||
const input = 'foo(bar';
|
||||
const expected = 'foo';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('with foreign bracket (close)', () => {
|
||||
const input = 'foo)bar';
|
||||
const expected = 'foo';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('with foreign bracket (close and open)', () => {
|
||||
const input = 'foo)(bar';
|
||||
const expected = 'foo';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('various bracket type', () => {
|
||||
const input = 'foo「(bar)」(';
|
||||
const expected = 'foo「(bar)」';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
|
||||
it('intersected', () => {
|
||||
const input = 'foo(「)」';
|
||||
const expected = 'foo(「)」';
|
||||
const actual = removeOrphanedBrackets(input);
|
||||
assert.deepStrictEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('MFM', () => {
|
||||
it('can be analyzed', () => {
|
||||
const tokens = analyze('@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue