Skip to content
Snippets Groups Projects
api.md 1.83 KiB
Newer Older
marihachi's avatar
marihachi committed
## parse API
入力文字列からノードツリーを生成します。  
全てのMFM構文を利用可能です。  

例:  
```ts
marihachi's avatar
marihachi committed
const nodes = mfm.parse('hello $[tada world]');
marihachi's avatar
marihachi committed
console.log(JSON.stringify(nodes));
marihachi's avatar
marihachi committed
// => [{"type":"text","props":{"text":"hello "}},{"type":"fn","props":{"name":"tada","args":{}},"children":[{"type":"text","props":{"text":"world"}}]}]
marihachi's avatar
marihachi committed
```

marihachi's avatar
marihachi committed
### 最大のネストの深さを変更する
デフォルトで20に設定されています。  

例:  
```ts
const nodes = mfm.parse('**<s>cannot nest</s>**', { nestLimit: 1 });
console.log(JSON.stringify(nodes));
// => [{"type":"bold","children":[{"type":"text","props":{"text":"<s>cannot nest</s>"}}]}]
marihachi's avatar
marihachi committed
```

## parseSimple API
marihachi's avatar
marihachi committed
入力文字列からノードツリーを生成します。  
絵文字コードとUnicode絵文字を利用可能です。  

例:  
```ts
const nodes = mfm.parseSimple('Hello :surprised_ai:');
marihachi's avatar
marihachi committed
console.log(JSON.stringify(nodes));
marihachi's avatar
marihachi committed
// => [{"type":"text","props":{"text":"Hello "}},{"type":"emojiCode","props":{"name":"surprised_ai"}}]
marihachi's avatar
marihachi committed
```

## toString API
ノードツリーからMFM文字列を生成します。

例:  
```ts
marihachi's avatar
marihachi committed
const nodes = mfm.parse('hello $[tada world]');
marihachi's avatar
marihachi committed
const output = mfm.toString(nodes);
marihachi's avatar
marihachi committed
console.log(output); // => "hello $[tada world]"
marihachi's avatar
marihachi committed
```
※元の文字列と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');
});
```