Newer
Older
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
## parse API
入力文字列からノードツリーを生成します。
全てのMFM構文を利用可能です。
例:
```ts
const nodes = mfm.parse('hello [tada world]');
console.log(JSON.stringify(nodes));
// => "[{"type":"text","props":{"text":"hello "}},{"type":"fn","props":{"name":"tada","args":{}},"children":[{"type":"text","props":{"text":"world"}}]}]"
```
## parsePlain API
入力文字列からノードツリーを生成します。
絵文字コードとUnicode絵文字を利用可能です。
例:
```ts
const nodes = mfm.parsePlain('Hello :surprised_ai:');
console.log(JSON.stringify(nodes));
// => "[{"type":"text","props":{"text":"Hello "}},{"type":"emojiCode","props":{"name":"surprised_ai"}}]"
```
## toString API
ノードツリーからMFM文字列を生成します。
例:
```ts
const nodes = mfm.parse('hello [tada world]');
const output = mfm.toString(nodes);
console.log(output); // => "hello [tada world]"
```
※元の文字列とtoString APIで出力される文字列の同一性は保障されません。
## inspect API
ノードツリーの全ノードに指定された関数を適用します。
例:
```ts
mfm.inspect(nodes, node => {
if (node.type == 'text') {
node.props.text = node.props.text.replace(/Good morning/g, 'Hello');
}
});
```
## extract API
ブール値を返す関数を渡してノードを抽出します。
このAPIはノードツリーを再帰的に探索します。
例:
```ts
mfm.extract(nodes, (node) => {
return (node.type === 'emojiCode');
});
```