-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathapi.ts
More file actions
219 lines (181 loc) Β· 6.16 KB
/
Copy pathapi.ts
File metadata and controls
219 lines (181 loc) Β· 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import assert from 'assert';
import * as mfm from '../src/index';
import {
TEXT, CENTER, FN, UNI_EMOJI, MENTION, EMOJI_CODE, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE, MATH_BLOCK, SEARCH, CODE_BLOCK, LINK
} from '../src/index';
describe('API', () => {
describe('toString', () => {
test('basic', () => {
const input =
`before
<center>
Hello $[tada everynyan! π]
I'm @ai, A bot of misskey!
https://github.com/syuilo/ai
</center>
after`;
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('single node', () => {
const input = '$[tada Hello]';
assert.strictEqual(mfm.toString(mfm.parse(input)[0]), '$[tada Hello]');
});
test('quote', () => {
const input = `
> abc
>
> 123
`;
assert.strictEqual(mfm.toString(mfm.parse(input)), '> abc\n> \n> 123');
});
test('search', () => {
const input = 'MFM ζΈγζΉ 123 Search';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('block code', () => {
const input = '```\nabc\n```';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('math block', () => {
const input = '\\[\ny = 2x + 1\n\\]';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('center', () => {
const input = '<center>\nabc\n</center>';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
// test('center (single line)', () => {
// const input = '<center>abc</center>';
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
// });
test('emoji code', () => {
const input = ':abc:';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('unicode emoji', () => {
const input = 'δ»θ΅·γγπ';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('big', () => {
const input = '***abc***';
const output = '$[tada abc]';
assert.strictEqual(mfm.toString(mfm.parse(input)), output);
});
test('bold', () => {
const input = '**abc**';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
// test('bold tag', () => {
// const input = '<b>abc</b>';
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
// });
test('small', () => {
const input = '<small>abc</small>';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
// test('italic', () => {
// const input = '*abc*';
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
// });
test('italic tag', () => {
const input = '<i>abc</i>';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
test('strike', () => {
const input = '~~foo~~';
assert.strictEqual(mfm.toString(mfm.parse(input)), input);
});
// test('strike tag', () => {
// const input = '<s>foo</s>';
// assert.strictEqual(mfm.toString(mfm.parse(input)), input);
// });
test('inline code', () => {
const input = 'AiScript: `#abc = 2`';
assert.strictEqual(mfm.toString(mfm.parse(input)), 'AiScript: `#abc = 2`');
});
test('math inline', () => {
const input = '\\(y = 2x + 3\\)';
assert.strictEqual(mfm.toString(mfm.parse(input)), '\\(y = 2x + 3\\)');
});
test('hashtag', () => {
const input = 'a #misskey b';
assert.strictEqual(mfm.toString(mfm.parse(input)), 'a #misskey b');
});
test('link', () => {
const input = '[Ai](https://github.com/syuilo/ai)';
assert.strictEqual(mfm.toString(mfm.parse(input)), '[Ai](https://github.com/syuilo/ai)');
});
test('bracket link', () => {
const input = '[#θγ‘γγγ‘γ³γ―γ©γ](<https://misskey.io/explore/tags/θγ‘γγγ‘γ³γ―γ©γ>)';
assert.strictEqual(mfm.toString(mfm.parse(input)), '[#θγ‘γγγ‘γ³γ―γ©γ](<https://misskey.io/explore/tags/θγ‘γγγ‘γ³γ―γ©γ>)');
});
test('silent link', () => {
const input = '?[Ai](https://github.com/syuilo/ai)';
assert.strictEqual(mfm.toString(mfm.parse(input)), '?[Ai](https://github.com/syuilo/ai)');
});
test('fn', () => {
const input = '$[tada Hello]';
assert.strictEqual(mfm.toString(mfm.parse(input)), '$[tada Hello]');
});
test('fn with arguments', () => {
const input = '$[spin.speed=1s,alternate Hello]';
assert.strictEqual(mfm.toString(mfm.parse(input)), '$[spin.speed=1s,alternate Hello]');
});
test('plain', () => {
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');
});
test('1 line plain', () => {
const input = 'a\n<plain>Hello</plain>\nb';
assert.strictEqual(mfm.toString(mfm.parse(input)), 'a\n<plain>\nHello\n</plain>\nb');
});
test('preserve url brackets', () => {
const input1 = 'https://github.com/syuilo/ai';
assert.strictEqual(mfm.toString(mfm.parse(input1)), input1);
const input2 = '<https://github.com/syuilo/ai>';
assert.strictEqual(mfm.toString(mfm.parse(input2)), input2);
});
});
describe('inspect', () => {
test('replace text', () => {
const input = 'good morning $[tada everynyan!]';
const result = mfm.parse(input);
mfm.inspect(result, node => {
if (node.type == 'text') {
node.props.text = node.props.text.replace(/good morning/g, 'hello');
}
});
assert.strictEqual(mfm.toString(result), 'hello $[tada everynyan!]');
});
test('replace text (one item)', () => {
const input = 'good morning $[tada everyone!]';
const result = mfm.parse(input);
mfm.inspect(result[1], node => {
if (node.type == 'text') {
node.props.text = node.props.text.replace(/one/g, 'nyan');
}
});
assert.strictEqual(mfm.toString(result), 'good morning $[tada everynyan!]');
});
});
describe('extract', () => {
test('basic', () => {
const nodes = mfm.parse('@hoge @piyo @bebeyo');
const expect = [
MENTION('hoge', null, '@hoge'),
MENTION('piyo', null, '@piyo'),
MENTION('bebeyo', null, '@bebeyo')
];
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'mention'), expect);
});
test('nested', () => {
const nodes = mfm.parse('abc:hoge:$[tada 123 @hoge :foo:]:piyo:');
const expect = [
EMOJI_CODE('hoge'),
EMOJI_CODE('foo'),
EMOJI_CODE('piyo')
];
assert.deepStrictEqual(mfm.extract(nodes, node => node.type == 'emojiCode'), expect);
});
});
});