sfm-js/test/api.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

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', () => {
it('basic', () => {
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
});
});
describe('inspect', () => {
it('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
it('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', () => {
it('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')
];
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'mention'), expect);
2021-04-03 12:04:53 +00:00
});
it('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')
];
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'emojiCode'), expect);
2021-04-03 12:04:53 +00:00
});
});
});