jormungandr-bite/test/mfm.ts

997 lines
24 KiB
TypeScript
Raw Normal View History

2018-10-15 15:37:21 -06:00
/*
* Tests of MFM
2019-01-20 21:30:30 -07:00
* How to run the tests:
* > mocha test/mfm.ts --require ts-node/register
2018-10-15 15:37:21 -06:00
*/
2018-05-17 18:21:19 -06:00
import * as assert from 'assert';
2017-02-11 09:03:57 -07:00
import analyze from '../src/mfm/parse';
2018-09-16 11:45:30 -06:00
import toHtml from '../src/mfm/html';
import { createTree as tree, createLeaf as leaf, MfmTree, removeOrphanedBrackets } from '../src/mfm/parser';
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-29 21:28:56 -07: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-20 23:49:31 -07:00
it('can be analyzed', () => {
const tokens = analyze('@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr');
assert.deepStrictEqual(tokens, [
leaf('mention', { acct: '@himawari', canonical: '@himawari', username: 'himawari', host: null }),
text(' '),
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-29 21:28:56 -07:00
});
2017-02-28 22:29:02 -07:00
describe('elements', () => {
describe('bold', () => {
it('simple', () => {
const tokens = analyze('**foo**');
assert.deepStrictEqual(tokens, [
tree('bold', [
text('foo')
], {}),
]);
});
it('with other texts', () => {
const tokens = analyze('bar**foo**bar');
assert.deepStrictEqual(tokens, [
text('bar'),
tree('bold', [
text('foo')
], {}),
text('bar'),
]);
});
it('with underscores', () => {
const tokens = analyze('__foo__');
assert.deepStrictEqual(tokens, [
tree('bold', [
text('foo')
], {}),
]);
});
it('with underscores (ensure it allows alphabet only)', () => {
const tokens = analyze('(=^・__________・^=)');
assert.deepStrictEqual(tokens, [
text('(=^・__________・^=)')
]);
});
it('mixed syntax', () => {
const tokens = analyze('**foo__');
assert.deepStrictEqual(tokens, [
text('**foo__'),
]);
});
it('mixed syntax', () => {
const tokens = analyze('__foo**');
assert.deepStrictEqual(tokens, [
text('__foo**'),
]);
});
2017-02-28 22:29:02 -07:00
});
2017-02-11 09:01:35 -07:00
2018-08-03 08:27:37 -06:00
it('big', () => {
const tokens = analyze('***Strawberry*** Pasta');
assert.deepStrictEqual(tokens, [
tree('big', [
text('Strawberry')
], {}),
text(' Pasta'),
]);
2018-08-03 08:27:37 -06:00
});
2018-12-05 04:11:54 -07:00
it('small', () => {
const tokens = analyze('<small>smaller</small>');
assert.deepStrictEqual(tokens, [
tree('small', [
2018-12-05 04:11:54 -07:00
text('smaller')
], {}),
]);
2018-12-05 04:11:54 -07:00
});
describe('motion', () => {
it('by triple brackets', () => {
const tokens = analyze('(((foo)))');
assert.deepStrictEqual(tokens, [
tree('motion', [
text('foo')
], {}),
]);
});
it('by triple brackets (with other texts)', () => {
const tokens = analyze('bar(((foo)))bar');
assert.deepStrictEqual(tokens, [
text('bar'),
tree('motion', [
text('foo')
], {}),
text('bar'),
]);
});
it('by <motion> tag', () => {
const tokens = analyze('<motion>foo</motion>');
assert.deepStrictEqual(tokens, [
tree('motion', [
text('foo')
], {}),
]);
});
it('by <motion> tag (with other texts)', () => {
const tokens = analyze('bar<motion>foo</motion>bar');
assert.deepStrictEqual(tokens, [
text('bar'),
tree('motion', [
text('foo')
], {}),
text('bar'),
]);
});
2018-08-04 21:33:51 -06:00
});
2018-09-29 23:46:18 -06:00
describe('mention', () => {
it('local', () => {
const tokens = analyze('@himawari foo');
assert.deepStrictEqual(tokens, [
leaf('mention', { acct: '@himawari', canonical: '@himawari', username: 'himawari', host: null }),
text(' foo')
]);
2018-09-29 23:46:18 -06:00
});
2017-02-11 09:01:35 -07:00
2018-09-29 23:46:18 -06:00
it('remote', () => {
const tokens = analyze('@hima_sub@namori.net foo');
assert.deepStrictEqual(tokens, [
leaf('mention', { acct: '@hima_sub@namori.net', canonical: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' }),
text(' foo')
]);
});
it('remote punycode', () => {
const tokens = analyze('@hima_sub@xn--q9j5bya.xn--zckzah foo');
assert.deepStrictEqual(tokens, [
leaf('mention', { acct: '@hima_sub@xn--q9j5bya.xn--zckzah', canonical: '@hima_sub@なもり.テスト', username: 'hima_sub', host: 'xn--q9j5bya.xn--zckzah' }),
text(' foo')
]);
2018-09-29 23:46:18 -06:00
});
2018-09-29 23:46:18 -06:00
it('ignore', () => {
const tokens = analyze('idolm@ster');
assert.deepStrictEqual(tokens, [
text('idolm@ster')
]);
2018-09-29 23:46:18 -06:00
const tokens2 = analyze('@a\n@b\n@c');
assert.deepStrictEqual(tokens2, [
leaf('mention', { acct: '@a', canonical: '@a', username: 'a', host: null }),
text('\n'),
leaf('mention', { acct: '@b', canonical: '@b', username: 'b', host: null }),
text('\n'),
leaf('mention', { acct: '@c', canonical: '@c', username: 'c', host: null })
]);
2018-09-29 23:46:18 -06:00
const tokens3 = analyze('**x**@a');
assert.deepStrictEqual(tokens3, [
tree('bold', [
text('x')
], {}),
leaf('mention', { acct: '@a', canonical: '@a', username: 'a', host: null })
]);
const tokens4 = analyze('@\n@v\n@veryverylongusername' /* \n@toolongtobeasamention */);
assert.deepStrictEqual(tokens4, [
text('@\n'),
leaf('mention', { acct: '@v', canonical: '@v', username: 'v', host: null }),
text('\n'),
leaf('mention', { acct: '@veryverylongusername', canonical: '@veryverylongusername', username: 'veryverylongusername', host: null }),
// text('\n@toolongtobeasamention')
]);
/*
const tokens5 = analyze('@domain_is@valid.example.com\n@domain_is@.invalid\n@domain_is@invali.d\n@domain_is@invali.d\n@domain_is@-invalid.com\n@domain_is@invalid-.com');
assert.deepStrictEqual([
leaf('mention', { acct: '@domain_is@valid.example.com', canonical: '@domain_is@valid.example.com', username: 'domain_is', host: 'valid.example.com' }),
text('\n@domain_is@.invalid\n@domain_is@invali.d\n@domain_is@invali.d\n@domain_is@-invalid.com\n@domain_is@invalid-.com')
], tokens5);
*/
2018-09-29 23:46:18 -06:00
});
});
2018-11-20 16:30:29 -07:00
describe('hashtag', () => {
it('simple', () => {
const tokens = analyze('#alice');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'alice' })
]);
2018-11-20 16:30:29 -07:00
});
2018-09-17 07:51:10 -06:00
2018-11-20 16:30:29 -07:00
it('after line break', () => {
const tokens = analyze('foo\n#alice');
assert.deepStrictEqual(tokens, [
2018-11-20 16:30:29 -07:00
text('foo\n'),
leaf('hashtag', { hashtag: 'alice' })
]);
2018-11-20 16:30:29 -07:00
});
it('with text', () => {
const tokens = analyze('Strawberry Pasta #alice');
assert.deepStrictEqual(tokens, [
2018-11-20 16:30:29 -07:00
text('Strawberry Pasta '),
leaf('hashtag', { hashtag: 'alice' })
]);
2018-11-20 16:30:29 -07:00
});
2018-11-29 04:12:37 -07:00
it('with text (zenkaku)', () => {
2018-12-01 14:53:57 -07:00
const tokens = analyze('こんにちは#世界');
assert.deepStrictEqual(tokens, [
2018-12-01 14:53:57 -07:00
text('こんにちは'),
leaf('hashtag', { hashtag: '世界' })
]);
2018-11-29 04:12:37 -07:00
});
2018-11-20 16:30:29 -07:00
it('ignore comma and period', () => {
const tokens = analyze('Foo #bar, baz #piyo.');
assert.deepStrictEqual(tokens, [
2018-11-20 16:30:29 -07:00
text('Foo '),
leaf('hashtag', { hashtag: 'bar' }),
2018-11-20 16:30:29 -07:00
text(', baz '),
leaf('hashtag', { hashtag: 'piyo' }),
2018-11-20 16:30:29 -07:00
text('.'),
]);
2018-11-20 16:30:29 -07:00
});
it('ignore exclamation mark', () => {
const tokens = analyze('#Foo!');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'Foo' }),
2018-11-20 16:30:29 -07:00
text('!'),
]);
2018-11-20 16:30:29 -07:00
});
it('ignore colon', () => {
const tokens = analyze('#Foo:');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'Foo' }),
text(':'),
]);
});
it('ignore single quote', () => {
const tokens = analyze('#foo\'');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text('\''),
]);
});
it('ignore double quote', () => {
const tokens = analyze('#foo"');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo' }),
text('"'),
]);
});
2018-11-24 01:18:11 -07:00
it('allow including number', () => {
const tokens = analyze('#foo123');
assert.deepStrictEqual(tokens, [
leaf('hashtag', { hashtag: 'foo123' }),
]);
2018-11-24 01:18:11 -07:00
});
2018-11-24 12:44:42 -07:00
it('with brackets', () => {
2018-11-26 10:08:51 -07:00
const tokens1 = analyze('(#foo)');
assert.deepStrictEqual(tokens1, [
2018-11-24 12:44:42 -07:00
text('('),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-24 12:44:42 -07:00
text(')'),
]);
2018-11-26 10:08:51 -07:00
const tokens2 = analyze('「#foo」');
assert.deepStrictEqual(tokens2, [
2018-11-26 10:08:51 -07:00
text('「'),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-26 10:08:51 -07:00
text('」'),
]);
2018-11-26 10:08:51 -07:00
});
it('with mixed brackets', () => {
const tokens = analyze('「#foo(bar)」');
assert.deepStrictEqual(tokens, [
2018-11-26 10:08:51 -07:00
text('「'),
leaf('hashtag', { hashtag: 'foo(bar)' }),
2018-11-26 10:08:51 -07:00
text('」'),
]);
2018-11-24 12:44:42 -07:00
});
it('with brackets (space before)', () => {
2018-11-26 10:08:51 -07:00
const tokens1 = analyze('(bar #foo)');
assert.deepStrictEqual(tokens1, [
2018-11-24 12:44:42 -07:00
text('(bar '),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-24 12:44:42 -07:00
text(')'),
]);
2018-11-26 10:08:51 -07:00
const tokens2 = analyze('「bar #foo」');
assert.deepStrictEqual(tokens2, [
2018-11-26 10:08:51 -07:00
text('「bar '),
leaf('hashtag', { hashtag: 'foo' }),
2018-11-26 10:08:51 -07:00
text('」'),
]);
2018-11-24 12:44:42 -07:00
});
it('disallow number only', () => {
const tokens = analyze('#123');
assert.deepStrictEqual(tokens, [
text('#123'),
]);
});
2018-11-24 12:44:42 -07:00
it('disallow number only (with brackets)', () => {
const tokens = analyze('(#123)');
assert.deepStrictEqual(tokens, [
2018-11-24 12:44:42 -07:00
text('(#123)'),
]);
2018-11-24 12:44:42 -07:00
});
2017-02-28 22:29:02 -07:00
});
2017-02-11 09:01:35 -07:00
describe('quote', () => {
it('basic', () => {
const tokens1 = analyze('> foo');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo')
], {})
]);
2018-09-19 15:27:41 -06:00
const tokens2 = analyze('>foo');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo')
], {})
]);
});
2018-09-20 17:33:24 -06:00
it('series', () => {
const tokens = analyze('> foo\n\n> bar');
assert.deepStrictEqual(tokens, [
tree('quote', [
text('foo')
], {}),
2018-11-30 18:40:09 -07:00
text('\n'),
tree('quote', [
text('bar')
], {}),
]);
});
2018-09-20 17:33:24 -06:00
it('trailing line break', () => {
const tokens1 = analyze('> foo\n');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo')
], {}),
]);
2018-10-29 04:09:24 -06:00
const tokens2 = analyze('> foo\n\n');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo')
], {}),
text('\n')
]);
});
it('multiline', () => {
const tokens1 = analyze('>foo\n>bar');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo\nbar')
], {})
]);
const tokens2 = analyze('> foo\n> bar');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo\nbar')
], {})
]);
});
it('multiline with trailing line break', () => {
const tokens1 = analyze('> foo\n> bar\n');
assert.deepStrictEqual(tokens1, [
tree('quote', [
text('foo\nbar')
], {}),
]);
const tokens2 = analyze('> foo\n> bar\n\n');
assert.deepStrictEqual(tokens2, [
tree('quote', [
text('foo\nbar')
], {}),
text('\n')
]);
});
it('with before and after texts', () => {
const tokens = analyze('before\n> foo\nafter');
assert.deepStrictEqual(tokens, [
2018-11-30 18:40:09 -07:00
text('before\n'),
tree('quote', [
text('foo')
], {}),
text('after'),
]);
});
2018-11-30 18:40:09 -07:00
it('multiple quotes', () => {
const tokens = analyze('> foo\nbar\n\n> foo\nbar\n\n> foo\nbar');
assert.deepStrictEqual(tokens, [
tree('quote', [
2018-11-30 18:40:09 -07:00
text('foo')
], {}),
2018-11-30 18:40:09 -07:00
text('bar\n\n'),
tree('quote', [
2018-11-30 18:40:09 -07:00
text('foo')
], {}),
2018-11-30 18:40:09 -07:00
text('bar\n\n'),
tree('quote', [
2018-11-30 18:40:09 -07:00
text('foo')
], {}),
2018-11-30 18:40:09 -07:00
text('bar'),
]);
2018-11-30 18:40:09 -07:00
});
it('require line break before ">"', () => {
const tokens = analyze('foo>bar');
assert.deepStrictEqual(tokens, [
text('foo>bar'),
]);
});
it('nested', () => {
const tokens = analyze('>> foo\n> bar');
assert.deepStrictEqual(tokens, [
tree('quote', [
tree('quote', [
text('foo')
], {}),
text('bar')
], {})
]);
});
it('trim line breaks', () => {
const tokens = analyze('foo\n\n>a\n>>b\n>>\n>>>\n>>>c\n>>>\n>d\n\n');
assert.deepStrictEqual(tokens, [
2018-11-30 18:40:09 -07:00
text('foo\n\n'),
tree('quote', [
2018-11-30 18:40:09 -07:00
text('a\n'),
tree('quote', [
2018-11-30 18:40:09 -07:00
text('b\n\n'),
tree('quote', [
text('\nc\n')
], {})
], {}),
text('d')
], {}),
text('\n'),
]);
});
2018-09-19 15:27:41 -06:00
});
describe('url', () => {
it('simple', () => {
const tokens = analyze('https://example.com');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com' })
]);
});
2018-11-16 05:30:01 -07:00
2018-11-16 20:52:20 -07:00
it('ignore trailing period', () => {
const tokens = analyze('https://example.com.');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com' }),
text('.')
]);
});
2018-11-16 05:30:01 -07:00
it('with comma', () => {
const tokens = analyze('https://example.com/foo?bar=a,b');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com/foo?bar=a,b' })
]);
});
it('ignore trailing comma', () => {
const tokens = analyze('https://example.com/foo, bar');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com/foo' }),
text(', bar')
]);
});
it('with brackets', () => {
const tokens = analyze('https://example.com/foo(bar)');
assert.deepStrictEqual(tokens, [
leaf('url', { url: 'https://example.com/foo(bar)' })
]);
});
it('ignore parent brackets', () => {
const tokens = analyze('(https://example.com/foo)');
assert.deepStrictEqual(tokens, [
text('('),
leaf('url', { url: 'https://example.com/foo' }),
text(')')
]);
});
2018-11-16 20:52:20 -07:00
2018-11-21 13:02:38 -07:00
it('ignore parent brackets 2', () => {
const tokens = analyze('(foo https://example.com/foo)');
assert.deepStrictEqual(tokens, [
2018-11-21 13:02:38 -07:00
text('(foo '),
leaf('url', { url: 'https://example.com/foo' }),
2018-11-21 13:02:38 -07:00
text(')')
]);
2018-11-21 13:02:38 -07:00
});
2018-11-16 20:52:20 -07:00
it('ignore parent brackets with internal brackets', () => {
const tokens = analyze('(https://example.com/foo(bar))');
assert.deepStrictEqual(tokens, [
text('('),
leaf('url', { url: 'https://example.com/foo(bar)' }),
text(')')
]);
2018-11-16 20:52:20 -07:00
});
2017-03-17 10:16:32 -06:00
});
2018-11-21 13:02:38 -07:00
describe('link', () => {
it('simple', () => {
const tokens = analyze('[foo](https://example.com)');
assert.deepStrictEqual(tokens, [
tree('link', [
2018-11-21 13:02:38 -07:00
text('foo')
], { url: 'https://example.com', silent: false })
]);
2018-11-21 13:02:38 -07:00
});
2018-11-24 21:21:39 -07:00
it('simple (with silent flag)', () => {
const tokens = analyze('?[foo](https://example.com)');
assert.deepStrictEqual(tokens, [
tree('link', [
2018-11-24 21:21:39 -07:00
text('foo')
], { url: 'https://example.com', silent: true })
]);
2018-11-24 21:21:39 -07:00
});
2018-11-21 13:02:38 -07:00
it('in text', () => {
const tokens = analyze('before[foo](https://example.com)after');
assert.deepStrictEqual(tokens, [
2018-11-21 13:02:38 -07:00
text('before'),
tree('link', [
2018-11-21 13:02:38 -07:00
text('foo')
], { url: 'https://example.com', silent: false }),
text('after'),
]);
2018-11-21 13:02:38 -07:00
});
2018-11-21 13:04:45 -07:00
it('with brackets', () => {
const tokens = analyze('[foo](https://example.com/foo(bar))');
assert.deepStrictEqual(tokens, [
tree('link', [
2018-11-21 13:04:45 -07:00
text('foo')
], { url: 'https://example.com/foo(bar)', silent: false })
]);
2018-11-21 13:04:45 -07:00
});
it('with parent brackets', () => {
const tokens = analyze('([foo](https://example.com/foo(bar)))');
assert.deepStrictEqual(tokens, [
2018-11-21 13:04:45 -07:00
text('('),
tree('link', [
2018-11-21 13:04:45 -07:00
text('foo')
], { url: 'https://example.com/foo(bar)', silent: false }),
text(')')
]);
2018-11-21 13:04:45 -07:00
});
2017-02-28 22:29:02 -07:00
});
2017-02-28 20:15:45 -07:00
2017-02-28 22:29:02 -07:00
it('emoji', () => {
2018-11-03 07:35:24 -06:00
const tokens1 = analyze(':cat:');
assert.deepStrictEqual(tokens1, [
leaf('emoji', { name: 'cat' })
]);
2018-11-03 07:35:24 -06:00
const tokens2 = analyze(':cat::cat::cat:');
assert.deepStrictEqual(tokens2, [
leaf('emoji', { name: 'cat' }),
leaf('emoji', { name: 'cat' }),
leaf('emoji', { name: 'cat' })
]);
2018-11-05 04:14:49 -07:00
const tokens3 = analyze('🍎');
assert.deepStrictEqual(tokens3, [
leaf('emoji', { emoji: '🍎' })
]);
2017-02-28 22:29:02 -07:00
});
2017-02-11 09:01:35 -07:00
describe('block code', () => {
it('simple', () => {
const tokens = analyze('```\nvar x = "Strawberry Pasta";\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'var x = "Strawberry Pasta";', lang: null })
]);
});
it('can specify language', () => {
const tokens = analyze('``` json\n{ "x": 42 }\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: '{ "x": 42 }', lang: 'json' })
]);
});
it('require line break before "```"', () => {
const tokens = analyze('before```\nfoo\n```');
assert.deepStrictEqual(tokens, [
text('before'),
leaf('inlineCode', { code: '`' }),
text('\nfoo\n'),
leaf('inlineCode', { code: '`' })
]);
});
it('series', () => {
const tokens = analyze('```\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', () => {
const tokens = analyze('```\naaa```bbb\n```');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'aaa```bbb', lang: null })
]);
});
it('trim after line break', () => {
const tokens = analyze('```\nfoo\n```\nbar');
assert.deepStrictEqual(tokens, [
leaf('blockCode', { code: 'foo', lang: null }),
text('bar')
]);
});
2017-02-28 22:29:02 -07:00
});
2018-11-20 20:55:15 -07:00
describe('inline code', () => {
it('simple', () => {
const tokens = analyze('`var x = "Strawberry Pasta";`');
assert.deepStrictEqual(tokens, [
leaf('inlineCode', { code: 'var x = "Strawberry Pasta";' })
]);
2018-11-20 20:55:15 -07:00
});
it('disallow line break', () => {
const tokens = analyze('`foo\nbar`');
assert.deepStrictEqual(tokens, [
2018-11-20 20:55:15 -07:00
text('`foo\nbar`')
]);
2018-11-20 20:55:15 -07:00
});
it('disallow ´', () => {
const tokens = analyze('`foo´bar`');
assert.deepStrictEqual(tokens, [
2018-11-20 20:55:15 -07:00
text('`foo´bar`')
]);
2018-11-20 20:55:15 -07:00
});
2017-02-28 22:29:02 -07:00
});
2018-06-23 04:31:28 -06:00
2018-11-16 01:03:52 -07:00
it('math', () => {
2018-11-16 20:52:20 -07:00
const fomula = 'x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}';
2018-11-16 08:31:49 -07:00
const text = `\\(${fomula}\\)`;
2018-11-16 01:03:52 -07:00
const tokens = analyze(text);
assert.deepStrictEqual(tokens, [
leaf('math', { formula: fomula })
]);
2018-11-16 01:03:52 -07:00
});
2018-06-23 04:31:28 -06:00
it('search', () => {
const tokens1 = analyze('a b c 検索');
assert.deepStrictEqual(tokens1, [
leaf('search', { content: 'a b c 検索', query: 'a b c' })
]);
2018-06-23 04:31:28 -06:00
const tokens2 = analyze('a b c Search');
assert.deepStrictEqual(tokens2, [
leaf('search', { content: 'a b c Search', query: 'a b c' })
]);
2018-06-23 04:31:28 -06:00
const tokens3 = analyze('a b c search');
assert.deepStrictEqual(tokens3, [
leaf('search', { content: 'a b c search', query: 'a b c' })
]);
2018-06-23 04:31:28 -06:00
const tokens4 = analyze('a b c SEARCH');
assert.deepStrictEqual(tokens4, [
leaf('search', { content: 'a b c SEARCH', query: 'a b c' })
]);
2018-06-23 04:31:28 -06:00
});
2018-06-26 03:42:00 -06:00
describe('title', () => {
it('simple', () => {
const tokens = analyze('【foo】');
assert.deepStrictEqual(tokens, [
tree('title', [
text('foo')
], {})
]);
});
2017-03-18 05:05:11 -06:00
it('require line break', () => {
const tokens = analyze('a【foo】');
assert.deepStrictEqual(tokens, [
text('a【foo】')
]);
});
2017-02-28 05:00:59 -07:00
it('with before and after texts', () => {
const tokens = analyze('before\n【foo】\nafter');
assert.deepStrictEqual(tokens, [
2018-11-30 18:40:09 -07:00
text('before\n'),
tree('title', [
text('foo')
], {}),
text('after')
]);
});
2017-02-28 05:00:59 -07:00
});
2018-11-24 21:36:40 -07:00
describe('center', () => {
it('simple', () => {
const tokens = analyze('<center>foo</center>');
assert.deepStrictEqual(tokens, [
tree('center', [
2018-11-24 21:36:40 -07:00
text('foo')
], {}),
]);
2018-11-24 21:36:40 -07:00
});
});
describe('strike', () => {
it('simple', () => {
const tokens = analyze('~~foo~~');
assert.deepStrictEqual(tokens, [
tree('strike', [
text('foo')
], {}),
]);
});
});
2018-12-05 01:39:26 -07:00
describe('italic', () => {
2019-01-20 01:52:11 -07:00
it('<i>', () => {
const tokens = analyze('<i>foo</i>');
assert.deepStrictEqual(tokens, [
tree('italic', [
text('foo')
], {}),
]);
});
it('underscore', () => {
const tokens = analyze('_foo_');
assert.deepStrictEqual(tokens, [
tree('italic', [
2018-12-05 01:39:26 -07:00
text('foo')
], {}),
]);
2018-12-05 01:39:26 -07:00
});
it('simple with asterix', () => {
const tokens = analyze('*foo*');
assert.deepStrictEqual(tokens, [
tree('italic', [
text('foo')
], {}),
]);
});
it('exlude emotes', () => {
const tokens = analyze('*.*');
assert.deepStrictEqual(tokens, [
text("*.*"),
]);
});
it('mixed', () => {
const tokens = analyze('_foo*');
assert.deepStrictEqual(tokens, [
text('_foo*'),
]);
});
it('mixed', () => {
const tokens = analyze('*foo_');
assert.deepStrictEqual(tokens, [
text('*foo_'),
]);
});
2019-01-20 01:53:08 -07:00
});
2017-02-28 05:00:59 -07:00
});
2018-09-16 11:45:30 -06:00
describe('toHtml', () => {
it('br', () => {
const input = 'foo\nbar\nbaz';
const output = '<p><span>foo<br>bar<br>baz</span></p>';
2018-09-16 11:45:30 -06:00
assert.equal(toHtml(analyze(input)), output);
});
});
it('code block with quote', () => {
const tokens = analyze('> foo\n```\nbar\n```');
assert.deepStrictEqual(tokens, [
tree('quote', [
text('foo')
], {}),
leaf('blockCode', { code: 'bar', lang: null })
]);
});
it('quote between two code blocks', () => {
const tokens = analyze('```\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 })
]);
});
2016-12-29 21:28:56 -07:00
});