Sharkey/test/mfm.ts

1202 lines
28 KiB
TypeScript
Raw Normal View History

2018-10-16 00:37:21 +03:00
/*
* Tests of MFM
2019-01-22 11:30:58 +02:00
*
2019-01-21 06:30:30 +02:00
* How to run the tests:
2020-03-06 17:12:23 +02:00
* > npx cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true npx mocha test/mfm.ts --require ts-node/register
2019-01-22 11:30:58 +02:00
*
* To specify test:
2020-03-06 17:12:23 +02:00
* > npx cross-env TS_NODE_FILES=true TS_NODE_TRANSPILE_ONLY=true npx mocha test/mfm.ts --require ts-node/register -g 'test name'
2018-10-16 00:37:21 +03:00
*/
2018-05-18 03:21:19 +03:00
import * as assert from 'assert';
2017-02-11 18:03:57 +02:00
2019-01-30 09:56:27 +02:00
import { parse, parsePlain } from '../src/mfm/parse';
2020-04-26 08:58:36 +03:00
import { toHtml } from '../src/mfm/to-html';
import { toString } from '../src/mfm/to-string';
import { createTree as tree, createLeaf as leaf, MfmTree } from '../src/mfm/prelude';
2019-01-30 10:15:12 +02:00
import { removeOrphanedBrackets } from '../src/mfm/language';
function text(text: string): MfmTree {
return leaf('text', { text });
}
describe('createLeaf', () => {
it('creates leaf', () => {
assert.deepStrictEqual(leaf('text', { text: 'abc' }), {
node: {
type: 'text',
props: {
text: 'abc'
}
},
children: [],
});
});
});
describe('createTree', () => {
it('creates tree', () => {
const t = tree('tree', [
leaf('left', { a: 2 }),
leaf('right', { b: 'hi' })
], {
c: 4
});
assert.deepStrictEqual(t, {
node: {
type: 'tree',
props: {
c: 4
}
},
children: [
leaf('left', { a: 2 }),
leaf('right', { b: 'hi' })
],
});
});
});
2016-12-30 06:28:56 +02:00
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', () => {
2018-01-21 08:49:31 +02:00
it('can be analyzed', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr');
assert.deepStrictEqual(tokens, [
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@himawari',
canonical: '@himawari',
username: 'himawari',
host: null
}),
text(' '),
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@hima_sub@namori.net',
canonical: '@hima_sub@namori.net',
username: 'hima_sub',
host: 'namori.net'
}),
text(' お腹ペコい '),
leaf('emoji', { name: 'cat' }),
text(' '),
leaf('hashtag', { hashtag: 'yryr' }),
]);
2016-12-30 06:28:56 +02:00
});
2017-03-01 07:29:02 +02:00
describe('elements', () => {
describe('bold', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('**foo**');
assert.deepStrictEqual(tokens, [
tree('bold', [
text('foo')
], {}),
]);
});
it('with other texts', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('bar**foo**bar');
assert.deepStrictEqual(tokens, [
text('bar'),
tree('bold', [
text('foo')
], {}),
text('bar'),
]);
});
it('with underscores', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('__foo__');
assert.deepStrictEqual(tokens, [
tree('bold', [
text('foo')
], {}),
]);
});
it('with underscores (ensure it allows alphabet only)', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('(=^・__________・^=)');
assert.deepStrictEqual(tokens, [
text('(=^・__________・^=)')
]);
});
it('mixed syntax', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('**foo__');
assert.deepStrictEqual(tokens, [
text('**foo__'),
]);
});
it('mixed syntax', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('__foo**');
assert.deepStrictEqual(tokens, [
text('__foo**'),
]);
});
2017-03-01 07:29:02 +02:00
});
2017-02-11 18:01:35 +02:00
2018-12-05 13:11:54 +02:00
it('small', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('<small>smaller</small>');
assert.deepStrictEqual(tokens, [
tree('small', [
2018-12-05 13:11:54 +02:00
text('smaller')
], {}),
]);
2018-12-05 13:11:54 +02:00
});
2018-09-30 08:46:18 +03:00
describe('mention', () => {
it('local', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('@himawari foo');
assert.deepStrictEqual(tokens, [
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@himawari',
canonical: '@himawari',
username: 'himawari',
host: null
}),
text(' foo')
]);
2018-09-30 08:46:18 +03:00
});
2017-02-11 18:01:35 +02:00
2018-09-30 08:46:18 +03:00
it('remote', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('@hima_sub@namori.net foo');
assert.deepStrictEqual(tokens, [
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@hima_sub@namori.net',
canonical: '@hima_sub@namori.net',
username: 'hima_sub',
host: 'namori.net'
}),
text(' foo')
]);
});
it('remote punycode', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('@hima_sub@xn--q9j5bya.xn--zckzah foo');
assert.deepStrictEqual(tokens, [
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@hima_sub@xn--q9j5bya.xn--zckzah',
canonical: '@hima_sub@なもり.テスト',
username: 'hima_sub',
host: 'xn--q9j5bya.xn--zckzah'
}),
text(' foo')
]);
2018-09-30 08:46:18 +03:00
});
2018-09-30 08:46:18 +03:00
it('ignore', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('idolm@ster');
assert.deepStrictEqual(tokens, [
text('idolm@ster')
]);
2018-09-30 08:46:18 +03:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse('@a\n@b\n@c');
assert.deepStrictEqual(tokens2, [
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@a',
canonical: '@a',
username: 'a',
host: null
}),
text('\n'),
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@b',
canonical: '@b',
username: 'b',
host: null
}),
text('\n'),
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@c',
canonical: '@c',
username: 'c',
host: null
})
]);
2018-09-30 08:46:18 +03:00
2019-01-30 08:30:05 +02:00
const tokens3 = parse('**x**@a');
assert.deepStrictEqual(tokens3, [
tree('bold', [
text('x')
], {}),
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@a',
canonical: '@a',
username: 'a',
host: null
})
]);
2019-01-30 08:30:05 +02:00
const tokens4 = parse('@\n@v\n@veryverylongusername');
assert.deepStrictEqual(tokens4, [
text('@\n'),
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@v',
canonical: '@v',
username: 'v',
host: null
}),
text('\n'),
2019-01-27 06:55:11 +02:00
leaf('mention', {
acct: '@veryverylongusername',
canonical: '@veryverylongusername',
username: 'veryverylongusername',
host: null
}),
]);
2018-09-30 08:46:18 +03:00
});
});
2018-11-21 01:30:29 +02:00
describe('hashtag', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#alice');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'alice' })
]);
2018-11-21 01:30:29 +02:00
});
2018-09-17 16:51:10 +03:00
2018-11-21 01:30:29 +02:00
it('after line break', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('foo\n#alice');
assert.deepStrictEqual(tokens, [
2018-11-21 01:30:29 +02:00
text('foo\n'),
leaf('hashtag', { hashtag: 'alice' })
]);
2018-11-21 01:30:29 +02:00
});
it('with text', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('Strawberry Pasta #alice');
assert.deepStrictEqual(tokens, [
2018-11-21 01:30:29 +02:00
text('Strawberry Pasta '),
leaf('hashtag', { hashtag: 'alice' })
]);
2018-11-21 01:30:29 +02:00
});
2018-11-29 13:12:37 +02:00
it('with text (zenkaku)', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('こんにちは#世界');
assert.deepStrictEqual(tokens, [
2018-12-01 23:53:57 +02:00
text('こんにちは'),
leaf('hashtag', { hashtag: '世界' })
]);
2018-11-29 13:12:37 +02:00
});
2018-11-21 01:30:29 +02:00
it('ignore comma and period', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('Foo #bar, baz #piyo.');
assert.deepStrictEqual(tokens, [
2018-11-21 01:30:29 +02:00
text('Foo '),
leaf('hashtag', { hashtag: 'bar' }),
2018-11-21 01:30:29 +02:00
text(', baz '),
leaf('hashtag', { hashtag: 'piyo' }),
2018-11-21 01:30:29 +02:00
text('.'),
]);
2018-11-21 01:30:29 +02:00
});
it('ignore exclamation mark', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#Foo!');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'Foo' }),
2018-11-21 01:30:29 +02:00
text('!'),
]);
2018-11-21 01:30:29 +02:00
});
it('ignore colon', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#Foo:');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'Foo' }),
text(':'),
]);
});
it('ignore single quote', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#foo\'');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text('\''),
]);
});
it('ignore double quote', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#foo"');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text('"'),
]);
});
2019-03-05 15:18:29 +02:00
it('ignore square brackets', () => {
const tokens = parse('#foo]');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text(']'),
]);
});
2019-04-17 18:40:56 +03:00
it('ignore 】', () => {
const tokens = parse('#foo】');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text('】'),
]);
});
2018-11-24 10:18:11 +02:00
it('allow including number', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#foo123');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo123' }),
]);
2018-11-24 10:18:11 +02:00
});
2018-11-24 21:44:42 +02:00
it('with brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('(#foo)');
assert.deepStrictEqual(tokens1, [
2018-11-24 21:44:42 +02:00
text('('),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-24 21:44:42 +02:00
text(')'),
]);
2018-11-26 19:08:51 +02:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse('「#foo」');
assert.deepStrictEqual(tokens2, [
2018-11-26 19:08:51 +02:00
text('「'),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-26 19:08:51 +02:00
text('」'),
]);
2018-11-26 19:08:51 +02:00
});
it('with mixed brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('「#foo(bar)」');
assert.deepStrictEqual(tokens, [
2018-11-26 19:08:51 +02:00
text('「'),
leaf('hashtag', { hashtag: 'foo(bar)' }),
2018-11-26 19:08:51 +02:00
text('」'),
]);
2018-11-24 21:44:42 +02:00
});
it('with brackets (space before)', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('(bar #foo)');
assert.deepStrictEqual(tokens1, [
2018-11-24 21:44:42 +02:00
text('(bar '),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-24 21:44:42 +02:00
text(')'),
]);
2018-11-26 19:08:51 +02:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse('「bar #foo」');
assert.deepStrictEqual(tokens2, [
2018-11-26 19:08:51 +02:00
text('「bar '),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-26 19:08:51 +02:00
text('」'),
]);
2018-11-24 21:44:42 +02:00
});
it('disallow number only', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('#123');
assert.deepStrictEqual(tokens, [
text('#123'),
]);
});
2018-11-24 21:44:42 +02:00
it('disallow number only (with brackets)', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('(#123)');
assert.deepStrictEqual(tokens, [
2018-11-24 21:44:42 +02:00
text('(#123)'),
]);
2018-11-24 21:44:42 +02:00
});
it('ignore slash', () => {
const tokens = parse('#foo/bar');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text('/bar'),
]);
});
it('ignore Keycap Number Sign (U+0023 + U+20E3)', () => {
const tokens = parse('#⃣');
assert.deepStrictEqual(tokens, [
leaf('emoji', { emoji: '#⃣' })
]);
});
it('ignore Keycap Number Sign (U+0023 + U+FE0F + U+20E3)', () => {
const tokens = parse('#️⃣');
assert.deepStrictEqual(tokens, [
leaf('emoji', { emoji: '#️⃣' })
]);
});
2017-03-01 07:29:02 +02:00
});
2017-02-11 18:01:35 +02:00
describe('quote', () => {
it('basic', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('> foo');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo')
], {})
]);
2018-09-20 00:27:41 +03:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse('>foo');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo')
], {})
]);
});
2018-09-21 02:33:24 +03:00
it('series', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('> foo\n\n> bar');
assert.deepStrictEqual(tokens, [
tree('quote', [
text('foo')
], {}),
2018-12-01 03:40:09 +02:00
text('\n'),
tree('quote', [
text('bar')
], {}),
]);
});
2018-09-21 02:33:24 +03:00
it('trailing line break', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('> foo\n');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo')
], {}),
]);
2018-10-29 12:09:24 +02:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse('> foo\n\n');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo')
], {}),
text('\n')
]);
});
it('multiline', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('>foo\n>bar');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo\nbar')
], {})
]);
2019-01-30 08:30:05 +02:00
const tokens2 = parse('> foo\n> bar');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo\nbar')
], {})
]);
});
it('multiline with trailing line break', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('> foo\n> bar\n');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo\nbar')
], {}),
]);
2019-01-30 08:30:05 +02:00
const tokens2 = parse('> foo\n> bar\n\n');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo\nbar')
], {}),
text('\n')
]);
});
it('with before and after texts', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('before\n> foo\nafter');
assert.deepStrictEqual(tokens, [
2018-12-01 03:40:09 +02:00
text('before\n'),
tree('quote', [
text('foo')
], {}),
text('after'),
]);
});
2018-12-01 03:40:09 +02:00
it('multiple quotes', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('> foo\nbar\n\n> foo\nbar\n\n> foo\nbar');
assert.deepStrictEqual(tokens, [
tree('quote', [
2018-12-01 03:40:09 +02:00
text('foo')
], {}),
2018-12-01 03:40:09 +02:00
text('bar\n\n'),
tree('quote', [
2018-12-01 03:40:09 +02:00
text('foo')
], {}),
2018-12-01 03:40:09 +02:00
text('bar\n\n'),
tree('quote', [
2018-12-01 03:40:09 +02:00
text('foo')
], {}),
2018-12-01 03:40:09 +02:00
text('bar'),
]);
2018-12-01 03:40:09 +02:00
});
it('require line break before ">"', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('foo>bar');
assert.deepStrictEqual(tokens, [
text('foo>bar'),
]);
});
it('nested', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('>> foo\n> bar');
assert.deepStrictEqual(tokens, [
tree('quote', [
tree('quote', [
text('foo')
], {}),
text('bar')
], {})
]);
});
it('trim line breaks', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('foo\n\n>a\n>>b\n>>\n>>>\n>>>c\n>>>\n>d\n\n');
assert.deepStrictEqual(tokens, [
2018-12-01 03:40:09 +02:00
text('foo\n\n'),
tree('quote', [
2018-12-01 03:40:09 +02:00
text('a\n'),
tree('quote', [
2018-12-01 03:40:09 +02:00
text('b\n\n'),
tree('quote', [
text('\nc\n')
], {})
], {}),
text('d')
], {}),
text('\n'),
]);
});
2018-09-20 00:27:41 +03:00
});
describe('url', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('https://example.com');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com' })
]);
});
2018-11-16 14:30:01 +02:00
2018-11-17 05:52:20 +02:00
it('ignore trailing period', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('https://example.com.');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com' }),
text('.')
]);
});
2018-11-16 14:30:01 +02:00
2019-06-17 14:15:19 +03:00
it('ignore trailing periods', () => {
const tokens = parse('https://example.com...');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com' }),
text('...')
]);
});
it('with comma', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('https://example.com/foo?bar=a,b');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com/foo?bar=a,b' })
]);
});
it('ignore trailing comma', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('https://example.com/foo, bar');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com/foo' }),
text(', bar')
]);
});
it('with brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('https://example.com/foo(bar)');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com/foo(bar)' })
]);
});
it('ignore parent brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('(https://example.com/foo)');
assert.deepStrictEqual(tokens, [
text('('),
leaf('url', { url: 'https://example.com/foo' }),
text(')')
]);
});
2018-11-17 05:52:20 +02:00
it('ignore parent []', () => {
2019-07-02 14:08:30 +03:00
const tokens = parse('foo [https://example.com/foo] bar');
assert.deepStrictEqual(tokens, [
2019-07-02 14:08:30 +03:00
text('foo ['),
leaf('url', { url: 'https://example.com/foo' }),
2019-07-02 14:08:30 +03:00
text('] bar')
]);
});
2018-11-21 22:02:38 +02:00
it('ignore parent brackets 2', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('(foo https://example.com/foo)');
assert.deepStrictEqual(tokens, [
2018-11-21 22:02:38 +02:00
text('(foo '),
leaf('url', { url: 'https://example.com/foo' }),
2018-11-21 22:02:38 +02:00
text(')')
]);
2018-11-21 22:02:38 +02:00
});
2018-11-17 05:52:20 +02:00
it('ignore parent brackets with internal brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('(https://example.com/foo(bar))');
assert.deepStrictEqual(tokens, [
text('('),
leaf('url', { url: 'https://example.com/foo(bar)' }),
text(')')
]);
2018-11-17 05:52:20 +02:00
});
it('ignore non-ascii characters contained url without angle brackets', () => {
const tokens = parse('https://大石泉すき.example.com');
assert.deepStrictEqual(tokens, [
text('https://大石泉すき.example.com')
]);
});
it('match non-ascii characters contained url with angle brackets', () => {
const tokens = parse('<https://大石泉すき.example.com>');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://大石泉すき.example.com' })
]);
});
2017-03-17 18:16:32 +02:00
});
2018-11-21 22:02:38 +02:00
describe('link', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('[foo](https://example.com)');
assert.deepStrictEqual(tokens, [
tree('link', [
2018-11-21 22:02:38 +02:00
text('foo')
], { url: 'https://example.com', silent: false })
]);
2018-11-21 22:02:38 +02:00
});
2018-11-25 06:21:39 +02:00
it('simple (with silent flag)', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('?[foo](https://example.com)');
assert.deepStrictEqual(tokens, [
tree('link', [
2018-11-25 06:21:39 +02:00
text('foo')
], { url: 'https://example.com', silent: true })
]);
2018-11-25 06:21:39 +02:00
});
2018-11-21 22:02:38 +02:00
it('in text', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('before[foo](https://example.com)after');
assert.deepStrictEqual(tokens, [
2018-11-21 22:02:38 +02:00
text('before'),
tree('link', [
2018-11-21 22:02:38 +02:00
text('foo')
], { url: 'https://example.com', silent: false }),
text('after'),
]);
2018-11-21 22:02:38 +02:00
});
2018-11-21 22:04:45 +02:00
it('with brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('[foo](https://example.com/foo(bar))');
assert.deepStrictEqual(tokens, [
tree('link', [
2018-11-21 22:04:45 +02:00
text('foo')
], { url: 'https://example.com/foo(bar)', silent: false })
]);
2018-11-21 22:04:45 +02:00
});
it('with parent brackets', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('([foo](https://example.com/foo(bar)))');
assert.deepStrictEqual(tokens, [
2018-11-21 22:04:45 +02:00
text('('),
tree('link', [
2018-11-21 22:04:45 +02:00
text('foo')
], { url: 'https://example.com/foo(bar)', silent: false }),
text(')')
]);
2018-11-21 22:04:45 +02:00
});
2017-03-01 07:29:02 +02:00
});
2017-03-01 05:15:45 +02:00
2017-03-01 07:29:02 +02:00
it('emoji', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse(':cat:');
assert.deepStrictEqual(tokens1, [
leaf('emoji', { name: 'cat' })
]);
2018-11-03 15:35:24 +02:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse(':cat::cat::cat:');
assert.deepStrictEqual(tokens2, [
leaf('emoji', { name: 'cat' }),
leaf('emoji', { name: 'cat' }),
leaf('emoji', { name: 'cat' })
]);
2018-11-05 13:14:49 +02:00
2019-01-30 08:30:05 +02:00
const tokens3 = parse('🍎');
assert.deepStrictEqual(tokens3, [
leaf('emoji', { emoji: '🍎' })
]);
2017-03-01 07:29:02 +02:00
});
2017-02-11 18:01:35 +02:00
describe('block code', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('```\nvar x = "Strawberry Pasta";\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'var x = "Strawberry Pasta";', lang: null })
]);
});
it('can specify language', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('``` json\n{ "x": 42 }\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: '{ "x": 42 }', lang: 'json' })
]);
});
it('require line break before "```"', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('before```\nfoo\n```');
assert.deepStrictEqual(tokens, [
text('before'),
leaf('inlineCode', { code: '`' }),
text('\nfoo\n'),
leaf('inlineCode', { code: '`' })
]);
});
it('series', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('```\nfoo\n```\n```\nbar\n```\n```\nbaz\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'foo', lang: null }),
leaf('blockCode', { code: 'bar', lang: null }),
leaf('blockCode', { code: 'baz', lang: null }),
]);
});
it('ignore internal marker', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('```\naaa```bbb\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'aaa```bbb', lang: null })
]);
});
it('trim after line break', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('```\nfoo\n```\nbar');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'foo', lang: null }),
text('bar')
]);
});
2017-03-01 07:29:02 +02:00
});
2018-11-21 05:55:15 +02:00
describe('inline code', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('`var x = "Strawberry Pasta";`');
assert.deepStrictEqual(tokens, [
leaf('inlineCode', { code: 'var x = "Strawberry Pasta";' })
]);
2018-11-21 05:55:15 +02:00
});
it('disallow line break', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('`foo\nbar`');
assert.deepStrictEqual(tokens, [
2018-11-21 05:55:15 +02:00
text('`foo\nbar`')
]);
2018-11-21 05:55:15 +02:00
});
it('disallow ´', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('`foo´bar`');
assert.deepStrictEqual(tokens, [
2018-11-21 05:55:15 +02:00
text('`foo´bar`')
]);
2018-11-21 05:55:15 +02:00
});
2017-03-01 07:29:02 +02:00
});
2018-06-23 13:31:28 +03:00
it('mathInline', () => {
2018-11-17 05:52:20 +02:00
const fomula = 'x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}';
const content = `\\(${fomula}\\)`;
2019-01-30 08:30:05 +02:00
const tokens = parse(content);
assert.deepStrictEqual(tokens, [
leaf('mathInline', { formula: fomula })
]);
2018-11-16 10:03:52 +02:00
});
describe('mathBlock', () => {
it('simple', () => {
const fomula = 'x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}';
const content = `\\[\n${fomula}\n\\]`;
2019-01-30 08:30:05 +02:00
const tokens = parse(content);
assert.deepStrictEqual(tokens, [
leaf('mathBlock', { formula: fomula })
]);
});
});
2018-06-23 13:31:28 +03:00
it('search', () => {
2019-01-30 08:30:05 +02:00
const tokens1 = parse('a b c 検索');
assert.deepStrictEqual(tokens1, [
leaf('search', { content: 'a b c 検索', query: 'a b c' })
]);
2018-06-23 13:31:28 +03:00
2019-01-30 08:30:05 +02:00
const tokens2 = parse('a b c Search');
assert.deepStrictEqual(tokens2, [
leaf('search', { content: 'a b c Search', query: 'a b c' })
]);
2018-06-23 13:31:28 +03:00
2019-01-30 08:30:05 +02:00
const tokens3 = parse('a b c search');
assert.deepStrictEqual(tokens3, [
leaf('search', { content: 'a b c search', query: 'a b c' })
]);
2018-06-23 13:31:28 +03:00
2019-01-30 08:30:05 +02:00
const tokens4 = parse('a b c SEARCH');
assert.deepStrictEqual(tokens4, [
leaf('search', { content: 'a b c SEARCH', query: 'a b c' })
]);
2018-06-23 13:31:28 +03:00
});
2018-06-26 12:42:00 +03:00
describe('title', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('【foo】');
assert.deepStrictEqual(tokens, [
tree('title', [
text('foo')
], {})
]);
});
2017-03-18 13:05:11 +02:00
it('require line break', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('a【foo】');
assert.deepStrictEqual(tokens, [
text('a【foo】')
]);
});
2017-02-28 14:00:59 +02:00
it('with before and after texts', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('before\n【foo】\nafter');
assert.deepStrictEqual(tokens, [
2018-12-01 03:40:09 +02:00
text('before\n'),
tree('title', [
text('foo')
], {}),
text('after')
]);
});
2019-01-27 06:40:38 +02:00
it('ignore multiple title blocks', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('【foo】bar【baz】');
2019-01-27 06:40:38 +02:00
assert.deepStrictEqual(tokens, [
text('【foo】bar【baz】')
]);
});
2019-01-27 06:48:56 +02:00
it('disallow linebreak in title', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('【foo\nbar】');
2019-01-27 06:48:56 +02:00
assert.deepStrictEqual(tokens, [
text('【foo\nbar】')
]);
});
2017-02-28 14:00:59 +02:00
});
2018-11-25 06:36:40 +02:00
describe('center', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('<center>foo</center>');
assert.deepStrictEqual(tokens, [
tree('center', [
2018-11-25 06:36:40 +02:00
text('foo')
], {}),
]);
2018-11-25 06:36:40 +02:00
});
});
describe('strike', () => {
it('simple', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('~~foo~~');
assert.deepStrictEqual(tokens, [
tree('strike', [
text('foo')
], {}),
]);
});
2019-06-16 15:42:57 +03:00
// https://misskey.io/notes/7u1kv5dmia
it('ignore internal tilde', () => {
const tokens = parse('~~~~~');
assert.deepStrictEqual(tokens, [
text('~~~~~')
]);
});
});
2018-12-05 10:39:26 +02:00
describe('italic', () => {
2019-01-20 10:52:11 +02:00
it('<i>', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('<i>foo</i>');
2019-01-20 10:52:11 +02:00
assert.deepStrictEqual(tokens, [
tree('italic', [
text('foo')
], {}),
]);
});
it('underscore', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('_foo_');
assert.deepStrictEqual(tokens, [
tree('italic', [
2018-12-05 10:39:26 +02:00
text('foo')
], {}),
]);
2018-12-05 10:39:26 +02:00
});
it('simple with asterix', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('*foo*');
assert.deepStrictEqual(tokens, [
tree('italic', [
text('foo')
], {}),
]);
});
it('exlude emotes', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('*.*');
assert.deepStrictEqual(tokens, [
text('*.*'),
]);
});
it('mixed', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('_foo*');
assert.deepStrictEqual(tokens, [
text('_foo*'),
]);
});
it('mixed', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('*foo_');
assert.deepStrictEqual(tokens, [
text('*foo_'),
]);
});
2019-01-25 09:41:51 +02:00
it('ignore snake_case string', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('foo_bar_baz');
2019-01-25 09:41:51 +02:00
assert.deepStrictEqual(tokens, [
text('foo_bar_baz'),
]);
});
2019-06-16 15:26:43 +03:00
it('require spaces', () => {
2019-06-16 15:29:31 +03:00
const tokens = parse('日目_L38b a_b');
2019-06-16 15:26:43 +03:00
assert.deepStrictEqual(tokens, [
2019-06-16 15:29:31 +03:00
text('日目_L38b a_b'),
2019-06-16 15:26:43 +03:00
]);
});
it('newline sandwich', () => {
const tokens = parse('foo\n_bar_\nbaz');
assert.deepStrictEqual(tokens, [
text('foo\n'),
tree('italic', [
text('bar')
], {}),
2019-06-16 15:30:26 +03:00
text('\nbaz'),
2019-06-16 15:26:43 +03:00
]);
});
2019-01-20 10:53:08 +02:00
});
2017-02-28 14:00:59 +02:00
});
2018-09-16 20:45:30 +03:00
2019-01-30 08:12:48 +02:00
describe('plainText', () => {
it('text', () => {
2019-01-30 08:27:54 +02:00
const tokens = parsePlain('foo');
2019-01-30 08:12:48 +02:00
assert.deepStrictEqual(tokens, [
text('foo'),
]);
});
it('emoji', () => {
2019-01-30 08:27:54 +02:00
const tokens = parsePlain(':foo:');
2019-01-30 08:12:48 +02:00
assert.deepStrictEqual(tokens, [
leaf('emoji', { name: 'foo' })
]);
});
it('emoji in text', () => {
2019-01-30 08:27:54 +02:00
const tokens = parsePlain('foo:bar:baz');
2019-01-30 08:12:48 +02:00
assert.deepStrictEqual(tokens, [
text('foo'),
leaf('emoji', { name: 'bar' }),
text('baz'),
]);
});
it('disallow other syntax', () => {
2019-01-30 08:27:54 +02:00
const tokens = parsePlain('foo **bar** baz');
2019-01-30 08:12:48 +02:00
assert.deepStrictEqual(tokens, [
text('foo **bar** baz'),
]);
});
});
2018-09-16 20:45:30 +03:00
describe('toHtml', () => {
it('br', () => {
const input = 'foo\nbar\nbaz';
const output = '<p><span>foo<br>bar<br>baz</span></p>';
2019-01-30 08:30:05 +02:00
assert.equal(toHtml(parse(input)), output);
2018-09-16 20:45:30 +03:00
});
it('br alt', () => {
const input = 'foo\r\nbar\rbaz';
const output = '<p><span>foo<br>bar<br>baz</span></p>';
2019-01-30 08:30:05 +02:00
assert.equal(toHtml(parse(input)), output);
});
2018-09-16 20:45:30 +03:00
});
it('code block with quote', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('> foo\n```\nbar\n```');
assert.deepStrictEqual(tokens, [
tree('quote', [
text('foo')
], {}),
leaf('blockCode', { code: 'bar', lang: null })
]);
});
it('quote between two code blocks', () => {
2019-01-30 08:30:05 +02:00
const tokens = parse('```\nbefore\n```\n> foo\n```\nafter\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'before', lang: null }),
tree('quote', [
text('foo')
], {}),
leaf('blockCode', { code: 'after', lang: null })
]);
});
describe('toString', () => {
it('太字', () => {
assert.deepStrictEqual(toString(parse('**太字**')), '**太字**');
});
it('中央揃え', () => {
assert.deepStrictEqual(toString(parse('<center>中央揃え</center>')), '<center>中央揃え</center>');
});
it('打ち消し線', () => {
assert.deepStrictEqual(toString(parse('~~打ち消し線~~')), '~~打ち消し線~~');
});
it('小さい字', () => {
assert.deepStrictEqual(toString(parse('<small>小さい字</small>')), '<small>小さい字</small>');
});
it('コードブロック', () => {
assert.deepStrictEqual(toString(parse('```\nコードブロック\n```')), '```\nコードブロック\n```');
});
it('インラインコード', () => {
assert.deepStrictEqual(toString(parse('`インラインコード`')), '`インラインコード`');
});
it('引用行', () => {
assert.deepStrictEqual(toString(parse('>引用行')), '>引用行');
});
it('検索', () => {
assert.deepStrictEqual(toString(parse('検索 [search]')), '検索 [search]');
});
it('リンク', () => {
assert.deepStrictEqual(toString(parse('[リンク](http://example.com)')), '[リンク](http://example.com)');
});
it('詳細なしリンク', () => {
assert.deepStrictEqual(toString(parse('?[詳細なしリンク](http://example.com)')), '?[詳細なしリンク](http://example.com)');
});
it('インライン数式', () => {
assert.deepStrictEqual(toString(parse('\\(インライン数式\\)')), '\\(インライン数式\\)');
});
it('ブロック数式', () => {
assert.deepStrictEqual(toString(parse('\\\[\nブロック数式\n\]\\')), '\\\[\nブロック数式\n\]\\');
});
});
2016-12-30 06:28:56 +02:00
});