2021-04-03 12:04:53 +00:00
|
|
|
import assert from 'assert';
|
2021-06-26 14:48:46 +00:00
|
|
|
import * as mfm from '../src/index';
|
2021-04-03 12:04:53 +00:00
|
|
|
import {
|
|
|
|
TEXT, CENTER, FN, UNI_EMOJI, MENTION, EMOJI_CODE, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE, MATH_BLOCK, SEARCH, CODE_BLOCK, LINK
|
2021-06-26 14:48:46 +00:00
|
|
|
} from '../src/index';
|
2021-04-03 12:04:53 +00:00
|
|
|
|
|
|
|
describe('API', () => {
|
|
|
|
describe('toString', () => {
|
2023-02-04 00:30:39 +00:00
|
|
|
test('basic', () => {
|
2021-04-03 12:04:53 +00:00
|
|
|
const input =
|
|
|
|
`before
|
|
|
|
<center>
|
2021-04-19 01:27:06 +00:00
|
|
|
Hello $[tada everynyan! 🎉]
|
2021-04-03 12:04:53 +00:00
|
|
|
|
|
|
|
I'm @ai, A bot of misskey!
|
|
|
|
|
|
|
|
https://github.com/syuilo/ai
|
|
|
|
</center>
|
|
|
|
after`;
|
2021-04-10 08:31:29 +00:00
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
2021-04-03 12:04:53 +00:00
|
|
|
});
|
2021-07-23 02:24:04 +00:00
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('single node', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '$[tada Hello]';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)[0]), '$[tada Hello]');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('quote', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = `
|
|
|
|
> abc
|
|
|
|
>
|
|
|
|
> 123
|
|
|
|
`;
|
|
|
|
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), '> abc\n> \n> 123');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('search', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = 'MFM 書き方 123 Search';
|
2021-10-17 05:33:22 +00:00
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('block code', () => {
|
2021-10-17 05:33:22 +00:00
|
|
|
const input = '```\nabc\n```';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('math block', () => {
|
2021-10-17 05:33:22 +00:00
|
|
|
const input = '\\[\ny = 2x + 1\n\\]';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
2021-10-17 05:25:59 +00:00
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('center', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '<center>\nabc\n</center>';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
// test('center (single line)', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
// const input = '<center>abc</center>';
|
|
|
|
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
// });
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('emoji code', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = ':abc:';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('unicode emoji', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '今起きた😇';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('big', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '***abc***';
|
|
|
|
const output = '$[tada abc]';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), output);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('bold', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '**abc**';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
// test('bold tag', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
// const input = '<b>abc</b>';
|
|
|
|
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
// });
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('small', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '<small>abc</small>';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
// test('italic', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
// const input = '*abc*';
|
|
|
|
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
// });
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('italic tag', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '<i>abc</i>';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('strike', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '~~foo~~';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
// test('strike tag', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
// const input = '<s>foo</s>';
|
|
|
|
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
|
|
|
|
// });
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('inline code', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = 'AiScript: `#abc = 2`';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), 'AiScript: `#abc = 2`');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('math inline', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '\\(y = 2x + 3\\)';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), '\\(y = 2x + 3\\)');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('hashtag', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = 'a #misskey b';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), 'a #misskey b');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('link', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '[Ai](https://github.com/syuilo/ai)';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), '[Ai](https://github.com/syuilo/ai)');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('silent link', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '?[Ai](https://github.com/syuilo/ai)';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), '?[Ai](https://github.com/syuilo/ai)');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('fn', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '$[tada Hello]';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), '$[tada Hello]');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('fn with arguments', () => {
|
2021-10-17 05:25:59 +00:00
|
|
|
const input = '$[spin.speed=1s,alternate Hello]';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), '$[spin.speed=1s,alternate Hello]');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('plain', () => {
|
2022-06-07 14:41:09 +00:00
|
|
|
const input = 'a\n<plain>\nHello\nworld\n</plain>\nb';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), 'a\n<plain>\nHello\nworld\n</plain>\nb');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('1 line plain', () => {
|
2022-06-07 14:41:09 +00:00
|
|
|
const input = 'a\n<plain>Hello</plain>\nb';
|
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input)), 'a\n<plain>\nHello\n</plain>\nb');
|
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('preserve url brackets', () => {
|
2021-07-31 06:12:43 +00:00
|
|
|
const input1 = 'https://github.com/syuilo/ai';
|
2021-07-23 02:24:04 +00:00
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input1)), input1);
|
|
|
|
|
2021-07-31 06:12:43 +00:00
|
|
|
const input2 = '<https://github.com/syuilo/ai>';
|
2021-07-23 02:24:04 +00:00
|
|
|
assert.strictEqual(mfm.toString(mfm.parse(input2)), input2);
|
|
|
|
});
|
2021-04-03 12:04:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('inspect', () => {
|
2023-02-04 00:30:39 +00:00
|
|
|
test('replace text', () => {
|
2021-04-19 01:27:06 +00:00
|
|
|
const input = 'good morning $[tada everynyan!]';
|
2021-04-10 08:31:29 +00:00
|
|
|
const result = mfm.parse(input);
|
|
|
|
mfm.inspect(result, node => {
|
2021-04-03 12:04:53 +00:00
|
|
|
if (node.type == 'text') {
|
|
|
|
node.props.text = node.props.text.replace(/good morning/g, 'hello');
|
|
|
|
}
|
|
|
|
});
|
2021-04-19 01:27:06 +00:00
|
|
|
assert.strictEqual(mfm.toString(result), 'hello $[tada everynyan!]');
|
2021-04-03 12:04:53 +00:00
|
|
|
});
|
2021-04-17 02:34:32 +00:00
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('replace text (one item)', () => {
|
2021-04-19 01:27:06 +00:00
|
|
|
const input = 'good morning $[tada everyone!]';
|
2021-04-17 02:34:32 +00:00
|
|
|
const result = mfm.parse(input);
|
|
|
|
mfm.inspect(result[1], node => {
|
|
|
|
if (node.type == 'text') {
|
|
|
|
node.props.text = node.props.text.replace(/one/g, 'nyan');
|
|
|
|
}
|
|
|
|
});
|
2021-04-19 01:27:06 +00:00
|
|
|
assert.strictEqual(mfm.toString(result), 'good morning $[tada everynyan!]');
|
2021-04-17 02:34:32 +00:00
|
|
|
});
|
2021-04-03 12:04:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('extract', () => {
|
2023-02-04 00:30:39 +00:00
|
|
|
test('basic', () => {
|
2021-04-10 08:31:29 +00:00
|
|
|
const nodes = mfm.parse('@hoge @piyo @bebeyo');
|
2021-04-03 12:04:53 +00:00
|
|
|
const expect = [
|
|
|
|
MENTION('hoge', null, '@hoge'),
|
|
|
|
MENTION('piyo', null, '@piyo'),
|
|
|
|
MENTION('bebeyo', null, '@bebeyo')
|
|
|
|
];
|
2021-04-10 10:56:26 +00:00
|
|
|
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'mention'), expect);
|
2021-04-03 12:04:53 +00:00
|
|
|
});
|
|
|
|
|
2023-02-04 00:30:39 +00:00
|
|
|
test('nested', () => {
|
2021-04-19 01:27:06 +00:00
|
|
|
const nodes = mfm.parse('abc:hoge:$[tada 123 @hoge :foo:]:piyo:');
|
2021-04-03 12:04:53 +00:00
|
|
|
const expect = [
|
|
|
|
EMOJI_CODE('hoge'),
|
|
|
|
EMOJI_CODE('foo'),
|
|
|
|
EMOJI_CODE('piyo')
|
|
|
|
];
|
2021-04-10 10:56:26 +00:00
|
|
|
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'emojiCode'), expect);
|
2021-04-03 12:04:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|