2018-10-15 23:37:21 +02:00
|
|
|
|
/*
|
|
|
|
|
* Tests of MFM
|
|
|
|
|
*/
|
|
|
|
|
|
2018-05-18 02:21:19 +02:00
|
|
|
|
import * as assert from 'assert';
|
2017-02-11 17:03:57 +01:00
|
|
|
|
|
2018-06-20 18:21:57 +02:00
|
|
|
|
import analyze from '../src/mfm/parse';
|
2018-09-16 19:45:30 +02:00
|
|
|
|
import toHtml from '../src/mfm/html';
|
2018-11-20 21:11:00 +01:00
|
|
|
|
|
|
|
|
|
function _node(name: string, children: any[], props: any) {
|
|
|
|
|
return children ? { name, children, props } : { name, props };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function node(name: string, props?: any) {
|
|
|
|
|
return _node(name, null, props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function nodeWithChildren(name: string, children: any[], props?: any) {
|
|
|
|
|
return _node(name, children, props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function text(text: string) {
|
|
|
|
|
return node('text', { text });
|
|
|
|
|
}
|
2016-12-30 05:28:56 +01:00
|
|
|
|
|
|
|
|
|
describe('Text', () => {
|
2018-01-21 07:49:31 +01:00
|
|
|
|
it('can be analyzed', () => {
|
2018-04-08 18:10:04 +02:00
|
|
|
|
const tokens = analyze('@himawari @hima_sub@namori.net お腹ペコい :cat: #yryr');
|
2016-12-30 05:28:56 +01:00
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('mention', { acct: '@himawari', canonical: '@himawari', username: 'himawari', host: null }),
|
|
|
|
|
text(' '),
|
|
|
|
|
node('mention', { acct: '@hima_sub@namori.net', canonical: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' }),
|
|
|
|
|
text(' お腹ペコい '),
|
|
|
|
|
node('emoji', { name: 'cat' }),
|
|
|
|
|
text(' '),
|
|
|
|
|
node('hashtag', { hashtag: 'yryr' }),
|
2016-12-30 05:28:56 +01:00
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2017-03-01 06:29:02 +01:00
|
|
|
|
describe('elements', () => {
|
2018-11-20 21:11:00 +01:00
|
|
|
|
describe('bold', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('**foo**');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('bold', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with other texts', () => {
|
|
|
|
|
const tokens = analyze('bar**foo**bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('bar'),
|
|
|
|
|
nodeWithChildren('bold', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('bar'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-03-01 06:29:02 +01:00
|
|
|
|
});
|
2017-02-11 17:01:35 +01:00
|
|
|
|
|
2018-08-03 16:27:37 +02:00
|
|
|
|
it('big', () => {
|
|
|
|
|
const tokens = analyze('***Strawberry*** Pasta');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
nodeWithChildren('big', [
|
|
|
|
|
text('Strawberry')
|
|
|
|
|
]),
|
|
|
|
|
text(' Pasta'),
|
2018-08-03 16:27:37 +02:00
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
describe('motion', () => {
|
|
|
|
|
it('by triple brackets', () => {
|
|
|
|
|
const tokens = analyze('(((foo)))');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('motion', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-08-05 16:56:08 +02:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
it('by triple brackets (with other texts)', () => {
|
|
|
|
|
const tokens = analyze('bar(((foo)))bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('bar'),
|
|
|
|
|
nodeWithChildren('motion', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('bar'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('by <motion> tag', () => {
|
|
|
|
|
const tokens = analyze('<motion>foo</motion>');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('motion', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('by <motion> tag (with other texts)', () => {
|
|
|
|
|
const tokens = analyze('bar<motion>foo</motion>bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('bar'),
|
|
|
|
|
nodeWithChildren('motion', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('bar'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-08-05 05:33:51 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-09-30 07:46:18 +02:00
|
|
|
|
describe('mention', () => {
|
|
|
|
|
it('local', () => {
|
2018-11-20 21:11:00 +01:00
|
|
|
|
const tokens = analyze('@himawari foo');
|
2018-09-30 07:46:18 +02:00
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('mention', { acct: '@himawari', canonical: '@himawari', username: 'himawari', host: null }),
|
|
|
|
|
text(' foo')
|
2018-09-30 07:46:18 +02:00
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-02-11 17:01:35 +01:00
|
|
|
|
|
2018-09-30 07:46:18 +02:00
|
|
|
|
it('remote', () => {
|
2018-11-20 21:11:00 +01:00
|
|
|
|
const tokens = analyze('@hima_sub@namori.net foo');
|
2018-09-30 07:46:18 +02:00
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('mention', { acct: '@hima_sub@namori.net', canonical: '@hima_sub@namori.net', username: 'hima_sub', host: 'namori.net' }),
|
|
|
|
|
text(' foo')
|
2018-10-14 09:56:19 +02:00
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('remote punycode', () => {
|
2018-11-20 21:11:00 +01:00
|
|
|
|
const tokens = analyze('@hima_sub@xn--q9j5bya.xn--zckzah foo');
|
2018-10-14 09:56:19 +02:00
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('mention', { acct: '@hima_sub@xn--q9j5bya.xn--zckzah', canonical: '@hima_sub@なもり.テスト', username: 'hima_sub', host: 'xn--q9j5bya.xn--zckzah' }),
|
|
|
|
|
text(' foo')
|
2018-09-30 07:46:18 +02:00
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-11-16 13:57:19 +01:00
|
|
|
|
|
2018-09-30 07:46:18 +02:00
|
|
|
|
it('ignore', () => {
|
|
|
|
|
const tokens = analyze('idolm@ster');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
text('idolm@ster')
|
2018-09-30 07:46:18 +02:00
|
|
|
|
], tokens);
|
|
|
|
|
|
|
|
|
|
const tokens2 = analyze('@a\n@b\n@c');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('mention', { acct: '@a', canonical: '@a', username: 'a', host: null }),
|
|
|
|
|
text('\n'),
|
|
|
|
|
node('mention', { acct: '@b', canonical: '@b', username: 'b', host: null }),
|
|
|
|
|
text('\n'),
|
|
|
|
|
node('mention', { acct: '@c', canonical: '@c', username: 'c', host: null })
|
2018-09-30 07:46:18 +02:00
|
|
|
|
], tokens2);
|
|
|
|
|
|
|
|
|
|
const tokens3 = analyze('**x**@a');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
nodeWithChildren('bold', [
|
|
|
|
|
text('x')
|
|
|
|
|
]),
|
|
|
|
|
node('mention', { acct: '@a', canonical: '@a', username: 'a', host: null })
|
2018-09-30 07:46:18 +02:00
|
|
|
|
], tokens3);
|
|
|
|
|
});
|
2018-04-08 18:10:04 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-11-21 00:30:29 +01:00
|
|
|
|
describe('hashtag', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('#alice');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('hashtag', { hashtag: 'alice' })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-09-17 15:51:10 +02:00
|
|
|
|
|
2018-11-21 00:30:29 +01:00
|
|
|
|
it('after line break', () => {
|
|
|
|
|
const tokens = analyze('foo\n#alice');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('foo\n'),
|
|
|
|
|
node('hashtag', { hashtag: 'alice' })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with text', () => {
|
|
|
|
|
const tokens = analyze('Strawberry Pasta #alice');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('Strawberry Pasta '),
|
|
|
|
|
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);
|
|
|
|
|
});
|
2018-11-23 08:02:17 +01:00
|
|
|
|
|
2018-11-24 09:18:11 +01:00
|
|
|
|
it('allow including number', () => {
|
|
|
|
|
const tokens = analyze('#foo123');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('hashtag', { hashtag: 'foo123' }),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-24 20:44:42 +01:00
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-23 08:02:17 +01:00
|
|
|
|
it('disallow number only', () => {
|
|
|
|
|
const tokens = analyze('#123');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('#123'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-11-24 20:44:42 +01:00
|
|
|
|
|
|
|
|
|
it('disallow number only (with brackets)', () => {
|
|
|
|
|
const tokens = analyze('(#123)');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('(#123)'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-03-01 06:29:02 +01:00
|
|
|
|
});
|
2017-02-11 17:01:35 +01:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
describe('quote', () => {
|
|
|
|
|
it('basic', () => {
|
|
|
|
|
const tokens1 = analyze('> foo');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
])
|
|
|
|
|
], tokens1);
|
2018-09-19 23:27:41 +02:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
const tokens2 = analyze('>foo');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
])
|
|
|
|
|
], tokens2);
|
|
|
|
|
});
|
2018-09-21 01:33:24 +02:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
it('series', () => {
|
|
|
|
|
const tokens = analyze('> foo\n\n> bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('bar')
|
|
|
|
|
]),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-09-21 01:33:24 +02:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
it('trailing line break', () => {
|
|
|
|
|
const tokens1 = analyze('> foo\n');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
], tokens1);
|
2018-10-29 11:09:24 +01:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
const tokens2 = analyze('> foo\n\n');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('\n')
|
|
|
|
|
], tokens2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('multiline', () => {
|
|
|
|
|
const tokens1 = analyze('>foo\n>bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo\nbar')
|
|
|
|
|
])
|
|
|
|
|
], tokens1);
|
|
|
|
|
|
|
|
|
|
const tokens2 = analyze('> foo\n> bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo\nbar')
|
|
|
|
|
])
|
|
|
|
|
], tokens2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('multiline with trailing line break', () => {
|
|
|
|
|
const tokens1 = analyze('> foo\n> bar\n');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo\nbar')
|
|
|
|
|
]),
|
|
|
|
|
], tokens1);
|
|
|
|
|
|
|
|
|
|
const tokens2 = analyze('> foo\n> bar\n\n');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo\nbar')
|
|
|
|
|
]),
|
|
|
|
|
text('\n')
|
|
|
|
|
], tokens2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with before and after texts', () => {
|
|
|
|
|
const tokens = analyze('before\n> foo\nafter');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('before'),
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('after'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('require line break before ">"', () => {
|
|
|
|
|
const tokens = analyze('foo>bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('foo>bar'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('nested', () => {
|
|
|
|
|
const tokens = analyze('>> foo\n> bar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('bar')
|
|
|
|
|
])
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('trim line breaks', () => {
|
|
|
|
|
const tokens = analyze('foo\n\n>a\n>>b\n>>\n>>>\n>>>c\n>>>\n>d\n\n');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('foo\n'),
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('a'),
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('b\n'),
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('\nc\n')
|
|
|
|
|
])
|
|
|
|
|
]),
|
|
|
|
|
text('d')
|
|
|
|
|
]),
|
|
|
|
|
text('\n'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-09-19 23:27:41 +02:00
|
|
|
|
});
|
|
|
|
|
|
2018-11-16 13:57:19 +01:00
|
|
|
|
describe('url', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('https://example.com');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('url', { url: 'https://example.com' })
|
|
|
|
|
], tokens);
|
2018-11-16 13:57:19 +01:00
|
|
|
|
});
|
2018-11-16 13:30:01 +01:00
|
|
|
|
|
2018-11-17 04:52:20 +01:00
|
|
|
|
it('ignore trailing period', () => {
|
2018-11-16 13:57:19 +01:00
|
|
|
|
const tokens = analyze('https://example.com.');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('url', { url: 'https://example.com' }),
|
|
|
|
|
text('.')
|
|
|
|
|
], tokens);
|
2018-11-16 13:57:19 +01:00
|
|
|
|
});
|
2018-11-16 13:30:01 +01:00
|
|
|
|
|
2018-11-16 13:57:19 +01:00
|
|
|
|
it('with comma', () => {
|
|
|
|
|
const tokens = analyze('https://example.com/foo?bar=a,b');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('url', { url: 'https://example.com/foo?bar=a,b' })
|
|
|
|
|
], tokens);
|
2018-11-16 13:57:19 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('ignore trailing comma', () => {
|
|
|
|
|
const tokens = analyze('https://example.com/foo, bar');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('url', { url: 'https://example.com/foo' }),
|
|
|
|
|
text(', bar')
|
|
|
|
|
], tokens);
|
2018-11-16 13:57:19 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with brackets', () => {
|
|
|
|
|
const tokens = analyze('https://example.com/foo(bar)');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('url', { url: 'https://example.com/foo(bar)' })
|
|
|
|
|
], tokens);
|
2018-11-16 13:57:19 +01:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('ignore parent brackets', () => {
|
|
|
|
|
const tokens = analyze('(https://example.com/foo)');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('('),
|
|
|
|
|
node('url', { url: 'https://example.com/foo' }),
|
|
|
|
|
text(')')
|
|
|
|
|
], tokens);
|
2018-11-16 13:57:19 +01:00
|
|
|
|
});
|
2018-11-17 04:52:20 +01:00
|
|
|
|
|
2018-11-21 21:02:38 +01:00
|
|
|
|
it('ignore parent brackets 2', () => {
|
|
|
|
|
const tokens = analyze('(foo https://example.com/foo)');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('(foo '),
|
|
|
|
|
node('url', { url: 'https://example.com/foo' }),
|
|
|
|
|
text(')')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-17 04:52:20 +01:00
|
|
|
|
it('ignore parent brackets with internal brackets', () => {
|
|
|
|
|
const tokens = analyze('(https://example.com/foo(bar))');
|
2018-11-20 21:11:00 +01:00
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('('),
|
|
|
|
|
node('url', { url: 'https://example.com/foo(bar)' }),
|
|
|
|
|
text(')')
|
|
|
|
|
], tokens);
|
2018-11-17 04:52:20 +01:00
|
|
|
|
});
|
2017-03-17 17:16:32 +01:00
|
|
|
|
});
|
|
|
|
|
|
2018-11-21 21:02:38 +01:00
|
|
|
|
describe('link', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('[foo](https://example.com)');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('link', [
|
|
|
|
|
text('foo')
|
|
|
|
|
], { url: 'https://example.com', silent: false })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-25 05:21:39 +01:00
|
|
|
|
it('simple (with silent flag)', () => {
|
|
|
|
|
const tokens = analyze('?[foo](https://example.com)');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('link', [
|
|
|
|
|
text('foo')
|
|
|
|
|
], { url: 'https://example.com', silent: true })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2018-11-21 21:02:38 +01:00
|
|
|
|
it('in text', () => {
|
|
|
|
|
const tokens = analyze('before[foo](https://example.com)after');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('before'),
|
|
|
|
|
nodeWithChildren('link', [
|
|
|
|
|
text('foo')
|
|
|
|
|
], { url: 'https://example.com', silent: false }),
|
|
|
|
|
text('after'),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2018-11-21 21:04:45 +01:00
|
|
|
|
|
|
|
|
|
it('with brackets', () => {
|
|
|
|
|
const tokens = analyze('[foo](https://example.com/foo(bar))');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('link', [
|
|
|
|
|
text('foo')
|
|
|
|
|
], { url: 'https://example.com/foo(bar)', silent: false })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('with parent brackets', () => {
|
|
|
|
|
const tokens = analyze('([foo](https://example.com/foo(bar)))');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('('),
|
|
|
|
|
nodeWithChildren('link', [
|
|
|
|
|
text('foo')
|
|
|
|
|
], { url: 'https://example.com/foo(bar)', silent: false }),
|
|
|
|
|
text(')')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-03-01 06:29:02 +01:00
|
|
|
|
});
|
2017-03-01 04:15:45 +01:00
|
|
|
|
|
2017-03-01 06:29:02 +01:00
|
|
|
|
it('emoji', () => {
|
2018-11-03 14:35:24 +01:00
|
|
|
|
const tokens1 = analyze(':cat:');
|
2017-03-01 06:29:02 +01:00
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('emoji', { name: 'cat' })
|
2018-11-03 14:35:24 +01:00
|
|
|
|
], tokens1);
|
|
|
|
|
|
|
|
|
|
const tokens2 = analyze(':cat::cat::cat:');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('emoji', { name: 'cat' }),
|
|
|
|
|
node('emoji', { name: 'cat' }),
|
|
|
|
|
node('emoji', { name: 'cat' })
|
2018-11-03 14:35:24 +01:00
|
|
|
|
], tokens2);
|
2018-11-05 12:14:49 +01:00
|
|
|
|
|
|
|
|
|
const tokens3 = analyze('🍎');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('emoji', { emoji: '🍎' })
|
2018-11-05 12:14:49 +01:00
|
|
|
|
], tokens3);
|
2017-03-01 06:29:02 +01:00
|
|
|
|
});
|
2017-02-11 17:01:35 +01:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
describe('block code', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('```\nvar x = "Strawberry Pasta";\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('blockCode', { code: 'var x = "Strawberry Pasta";', lang: null })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('can specify language', () => {
|
|
|
|
|
const tokens = analyze('``` json\n{ "x": 42 }\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('blockCode', { code: '{ "x": 42 }', lang: 'json' })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('require line break before "```"', () => {
|
|
|
|
|
const tokens = analyze('before```\nfoo\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('before'),
|
|
|
|
|
node('inlineCode', { code: '`' }),
|
|
|
|
|
text('\nfoo\n'),
|
|
|
|
|
node('inlineCode', { code: '`' })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('series', () => {
|
|
|
|
|
const tokens = analyze('```\nfoo\n```\n```\nbar\n```\n```\nbaz\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('blockCode', { code: 'foo', lang: null }),
|
|
|
|
|
node('blockCode', { code: 'bar', lang: null }),
|
|
|
|
|
node('blockCode', { code: 'baz', lang: null }),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('ignore internal marker', () => {
|
|
|
|
|
const tokens = analyze('```\naaa```bbb\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('blockCode', { code: 'aaa```bbb', lang: null })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('trim after line break', () => {
|
|
|
|
|
const tokens = analyze('```\nfoo\n```\nbar');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('blockCode', { code: 'foo', lang: null }),
|
|
|
|
|
text('bar')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-03-01 06:29:02 +01:00
|
|
|
|
});
|
|
|
|
|
|
2018-11-21 04:55:15 +01:00
|
|
|
|
describe('inline code', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('`var x = "Strawberry Pasta";`');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('inlineCode', { code: 'var x = "Strawberry Pasta";' })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('disallow line break', () => {
|
|
|
|
|
const tokens = analyze('`foo\nbar`');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('`foo\nbar`')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('disallow ´', () => {
|
|
|
|
|
const tokens = analyze('`foo´bar`');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('`foo´bar`')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-03-01 06:29:02 +01:00
|
|
|
|
});
|
2018-06-23 12:31:28 +02:00
|
|
|
|
|
2018-11-16 09:03:52 +01:00
|
|
|
|
it('math', () => {
|
2018-11-17 04:52:20 +01:00
|
|
|
|
const fomula = 'x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}';
|
2018-11-16 16:31:49 +01:00
|
|
|
|
const text = `\\(${fomula}\\)`;
|
2018-11-16 09:03:52 +01:00
|
|
|
|
const tokens = analyze(text);
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('math', { formula: fomula })
|
2018-11-16 09:03:52 +01:00
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
2018-06-23 12:31:28 +02:00
|
|
|
|
it('search', () => {
|
|
|
|
|
const tokens1 = analyze('a b c 検索');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('search', { content: 'a b c 検索', query: 'a b c' })
|
2018-06-23 12:31:28 +02:00
|
|
|
|
], tokens1);
|
|
|
|
|
|
|
|
|
|
const tokens2 = analyze('a b c Search');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('search', { content: 'a b c Search', query: 'a b c' })
|
2018-06-23 12:31:28 +02:00
|
|
|
|
], tokens2);
|
|
|
|
|
|
|
|
|
|
const tokens3 = analyze('a b c search');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('search', { content: 'a b c search', query: 'a b c' })
|
2018-06-23 12:31:28 +02:00
|
|
|
|
], tokens3);
|
|
|
|
|
|
|
|
|
|
const tokens4 = analyze('a b c SEARCH');
|
|
|
|
|
assert.deepEqual([
|
2018-11-20 21:11:00 +01:00
|
|
|
|
node('search', { content: 'a b c SEARCH', query: 'a b c' })
|
2018-06-23 12:31:28 +02:00
|
|
|
|
], tokens4);
|
|
|
|
|
});
|
2018-06-26 11:42:00 +02:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
describe('title', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('【foo】');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('title', [
|
|
|
|
|
text('foo')
|
|
|
|
|
])
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-03-18 12:05:11 +01:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
it('require line break', () => {
|
|
|
|
|
const tokens = analyze('a【foo】');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('a【foo】')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-02-28 13:00:59 +01:00
|
|
|
|
|
2018-11-20 21:11:00 +01:00
|
|
|
|
it('with before and after texts', () => {
|
|
|
|
|
const tokens = analyze('before\n【foo】\nafter');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
text('before'),
|
|
|
|
|
nodeWithChildren('title', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
text('after')
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2017-02-28 13:00:59 +01:00
|
|
|
|
});
|
2018-11-25 05:36:40 +01:00
|
|
|
|
|
|
|
|
|
describe('center', () => {
|
|
|
|
|
it('simple', () => {
|
|
|
|
|
const tokens = analyze('<center>foo</center>');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('center', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
});
|
2017-02-28 13:00:59 +01:00
|
|
|
|
});
|
2018-09-16 19:45:30 +02:00
|
|
|
|
|
|
|
|
|
describe('toHtml', () => {
|
|
|
|
|
it('br', () => {
|
|
|
|
|
const input = 'foo\nbar\nbaz';
|
2018-11-20 21:11:00 +01:00
|
|
|
|
const output = '<p><span>foo<br>bar<br>baz</span></p>';
|
2018-09-16 19:45:30 +02:00
|
|
|
|
assert.equal(toHtml(analyze(input)), output);
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-11-20 21:11:00 +01:00
|
|
|
|
|
|
|
|
|
it('code block with quote', () => {
|
|
|
|
|
const tokens = analyze('> foo\n```\nbar\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
node('blockCode', { code: 'bar', lang: null })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('quote between two code blocks', () => {
|
|
|
|
|
const tokens = analyze('```\nbefore\n```\n> foo\n```\nafter\n```');
|
|
|
|
|
assert.deepEqual([
|
|
|
|
|
node('blockCode', { code: 'before', lang: null }),
|
|
|
|
|
nodeWithChildren('quote', [
|
|
|
|
|
text('foo')
|
|
|
|
|
]),
|
|
|
|
|
node('blockCode', { code: 'after', lang: null })
|
|
|
|
|
], tokens);
|
|
|
|
|
});
|
2016-12-30 05:28:56 +01:00
|
|
|
|
});
|