Skip to content

Commit

Permalink
feat: Add parseHtml
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo committed Jan 6, 2025
1 parent 9cd9aae commit daf3e8a
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 283 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
-->

## Unreleased
### Features
- Add parseHtml

## 0.24.0
### Features
- Supports Unicode 15.0 emoji
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ https://github.com/syuilo/ai
// Generate a MFM tree from the full MFM text.
const mfmTree = mfm.parse(inputText);

// Generate a MFM tree from the html MFM text.
const htmlMfmTree = mfm.parseHtml(inputText);

// Generate a MFM tree from the simple MFM text.
const simpleMfmTree = mfm.parseSimple('I like the hot soup :soup:​');

Expand Down Expand Up @@ -62,6 +65,11 @@ full parser:
npm run parse
```

html parser:
```
npm run parse-html
```

simple parser:
```
npm run parse-simple
Expand Down
21 changes: 21 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ console.log(JSON.stringify(nodes));
// => [{"type":"bold","children":[{"type":"text","props":{"text":"<s>cannot nest</s>"}}]}]
```

## parseHtml API
入力文字列からノードツリーを生成します。
MFM構文のうち、HTML、メンション、ハッシュタグ、URL、絵文字コード、Unicode絵文字を利用可能です。

例:
```ts
const nodes = mfm.parseHtml('hello @syuilo');
console.log(JSON.stringify(nodes));
// => [{"type":"text","props":{"text":"hello "}},{"type":"mention","props":{"username":"syuilo","host":null,"acct":"@syuilo"}}]
```

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

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

## parseSimple API
入力文字列からノードツリーを生成します。
絵文字コードとUnicode絵文字を利用可能です。
Expand Down
Loading

0 comments on commit daf3e8a

Please sign in to comment.