Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • TransFem-org/sfm-js
  • dakkar/sfm-js
  • septicake/sfm-js-docs
3 results
Show changes
Commits on Source (125)
Showing with 3991 additions and 5541 deletions
......@@ -4,3 +4,5 @@ node_modules
/.eslintrc.js
/jest.config.ts
parser.js
/test
/test-d
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
plugins: [
'@typescript-eslint',
],
......@@ -32,20 +36,28 @@ module.exports = {
'space-before-blocks': ['error', 'always'],
'object-curly-spacing': ['error', 'always'],
'nonblock-statement-body-position': ['error', 'beside'],
'eqeqeq': ['error', 'always', { 'null': 'ignore' }],
'no-multiple-empty-lines': ['error', { 'max': 1 }],
'no-multi-spaces': ['error'],
'no-var': ['error'],
'prefer-arrow-callback': ['error'],
'no-throw-literal': ['error'],
'no-param-reassign': ['warn'],
'no-constant-condition': ['warn'],
'no-constant-condition': ['warn', {
checkLoops: false,
}],
'no-empty-pattern': ['warn'],
'@typescript-eslint/no-unnecessary-condition': ['error'],
'@typescript-eslint/no-unnecessary-condition': ['warn', {
allowConstantLoopConditions: true,
}],
'@typescript-eslint/no-inferrable-types': ['warn'],
'@typescript-eslint/no-non-null-assertion': ['warn'],
'@typescript-eslint/explicit-function-return-type': ['warn'],
'@typescript-eslint/no-misused-promises': ['error', {
'checksVoidReturn': false,
}],
'@typescript-eslint/no-unused-vars': ['error', {
"argsIgnorePattern": "^_",
}]
},
};
name: Test
on: [push, pull_request]
jobs:
test:
runs-on: docker
steps:
- name: Checkout
uses: actions/checkout@v4.1.1
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20.5.1
- name: Install dependencies
run: npm i
- name: Build
run: npm run build
- name: Test
run: npm test
env:
CI: true
contact_links:
- name: 👪 Misskey Forum
url: https://forum.misskey.io/
about: Ask questions and share knowledge
- name: 💬 Misskey official Discord
url: https://discord.gg/Wp8gVStHW3
about: Chat freely about Misskey
<!-- ℹ お読みください
PRありがとうございます! PRを作成する前に、コントリビューションガイドをご確認ください:
https://github.com/misskey-dev/misskey.js/blob/develop/CONTRIBUTING.md
-->
<!-- ℹ README
Thank you for your PR! Before creating a PR, please check the contribution guide:
https://github.com/misskey-dev/misskey.js/blob/develop/docs/CONTRIBUTING.en.md
-->
# What
<!-- このPRで何をしたのか? どう変わるのか? -->
<!-- What did you do with this PR? How will it change things? -->
# Why
<!-- なぜそうするのか? どういう意図なのか? 何が困っているのか? -->
<!-- Why do you do it? What are your intentions? What is the problem? -->
# Additional info (optional)
<!-- テスト観点など -->
<!-- Test perspective, etc -->
name: API report
on: [push, pull_request]
jobs:
report:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16.5.0
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Check files
run: ls built
- name: API report
run: npm run api-prod
- name: Show report
if: always()
run: cat temp/mfm-js.api.md
name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 16.5.0
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Test and coverage
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.5.0]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Test
run: npm test
env:
CI: true
- name: Upload Coverage
uses: codecov/codecov-action@v1
image: node:latest
stages:
- testing
- deploy
test:
stage: testing
script:
- npm i
- npm run build
- npm run test
publish-npm:
stage: deploy
needs: ['test']
rules:
- if: $CI_COMMIT_BRANCH == "stable"
changes:
paths:
- package.json
script:
- echo "@transfem-org:registry=https://${CI_SERVER_HOST}/api/v4/projects/2/packages/npm/" > .npmrc
- echo "//${CI_SERVER_HOST}/api/v4/projects/2/packages/npm/:_authToken=${CI_JOB_TOKEN}" >> .npmrc
- npm i
- npm run build
- npm publish
......@@ -11,15 +11,72 @@
-->
## 0.x.x (unreleased)
## 0.24.0
### Features
- Supports Unicode 15.0 emoji
## 0.23.3
- tweak fn parsing
- fnNameList option removed
- emojiCodeList option removed
## 0.23.2
### Features
- Supports whitelisting of emoji code names. (#130)
## 0.23.1
### Improvements
- improve emoji code parsing
## 0.23.0
### Features
- Add Plain syntax (#101)
### Improvements
- The parser is now implemented in TypeScript! 🎉 (#92)
- Disable all syntax when nesting limited (#90)
### Changes
- Rename existing plain series (#113):
- parsePlain -> parseSimple
- MfmPlainNode -> MfmSimpleNode
### Bugfixes
- Fix a bug that allows line breaks in link label (#115)
## 0.22.1
### Improvements
- Removes a unnecessary built file
## 0.22.0
### Features
- Unicode emoji supports Unicode 14.0 emoji (#109)
### Improvements
- `()` pair is available on outside the hashtag (#111)
- Changes specs the center tag and strike (#108, 100fb0b)
- Improves link label parsing (#107)
### Bugfixes
- If there is a `[]` pair before the link, it will be mistakenly recognized as a part of link label. (#104)
## 0.21.0
### Features
- Supports nestLimit option. (#87, #91)
### Improvements
- Improve generation of brackets property of url node.
### Bugfixes
- Fix the Link node of the enclosed in `<>`. (#84)
- Fix parsing of the link label.
## 0.20.0
npm: https://www.npmjs.com/package/mfm-js/v/0.20.0
### Features
- Add tag syntaxes of bold `<b></b>` and strikethrough `<s></s>`. (#76)
- Supports whitelisting of MFM function names. (#77)
......@@ -35,22 +92,16 @@ npm: https://www.npmjs.com/package/mfm-js/v/0.20.0
## 0.19.0
npm: https://www.npmjs.com/package/mfm-js/v/0.19.0
### Improvements
- Ignores a blank line after quote lines. (#61)
## 0.18.0
npm: https://www.npmjs.com/package/mfm-js/v/0.18.0
### Improvements
- Twemoji v13.1 is supported.
## 0.17.0
npm: https://www.npmjs.com/package/mfm-js/v/0.17.0
### Improvements
- Improves syntax of inline code.
- Improves syntax of url.
......
# mfm.js
An MFM parser implementation with PEG.js.
[Try it out!](https://runkit.com/npm/mfm-js)
[![Test](https://github.com/misskey-dev/mfm.js/actions/workflows/test.yml/badge.svg)](https://github.com/misskey-dev/mfm.js/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/misskey-dev/mfm.js/branch/develop/graph/badge.svg?token=irAWFiHK8T)](https://codecov.io/gh/misskey-dev/mfm.js)
[![NPM](https://nodei.co/npm/mfm-js.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/mfm-js)
# sfm.js
An SFM parser implementation with fixes based on MFM
## Installation
```
npm i mfm-js
npm config set @transfem-org:registry https://activitypub.software/api/v4/packages/npm/
npm i @transfem-org/sfm-js
```
## Usage
......@@ -17,7 +12,7 @@ Please see [docs](./docs/index.md) for the detail.
TypeScript:
```ts
import * as mfm from 'mfm-js';
import * as mfm from '@transfem-org/sfm-js';
const inputText =
`<center>
......@@ -28,11 +23,11 @@ I'm @ai, A bot of misskey!
https://github.com/syuilo/ai
</center>`;
// Generate a MFM tree from the MFM text.
// Generate a MFM tree from the full MFM text.
const mfmTree = mfm.parse(inputText);
// Generate a MFM tree from the MFM plain text.
const plainMfmTree = mfm.parsePlain('I like the hot soup :soup:​');
// Generate a MFM tree from the simple MFM text.
const simpleMfmTree = mfm.parseSimple('I like the hot soup :soup:​');
// Reverse to a MFM text from the MFM tree.
const text = mfm.toString(mfmTree);
......@@ -42,12 +37,12 @@ const text = mfm.toString(mfmTree);
## Develop
### 1. Clone
```
git clone https://github.com/misskey-dev/mfm.js.git
git clone https://activitypub.software/TransFem-org/sfm-js.git
```
### 2. Install packages
```
cd mfm.js
cd sfm.js
npm i
```
......@@ -62,9 +57,9 @@ full parser:
npm run parse
```
plain parser:
simple parser:
```
npm run parse-plain
npm run parse-simple
```
## License
......
......@@ -28,3 +28,30 @@ Thank you for your PR! Before creating a PR, please check the following:
Thanks for your cooperation 🤗
## Testing with Sharkey
Let's say you have Sharkey checked out at `~/src/Sharkey`, and this
repository at `~/src/sfm-js`. You have made some modifications to this
code, and want to run Sharkey with your modifications.
```shell
cd ~/src/Sharkey
pnpm --filter=backend add ../../../sfm-js
pnpm --filter=frontend add ../../../sfm-js
```
this will replace the "real" `@transfem-org/sfm-js` in the
dependencies, with your local version. Remember to *not* commit the
changes to `package.json` that this causes! (`pnpm link` should also
work, but I couldn't figure it out)
Then you'll need to tell Vite to handle that package specially. Edit
`packages/frontend/vite.config.local-dev.ts` and add, before `define`:
```json
optimizeDeps: {
include: ['@transfem-org/sfm-js'],
},
```
Now you can `pnpm dev` and test your changes.
......@@ -6,35 +6,28 @@
```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"}}]}]"
// => [{"type":"text","props":{"text":"hello "}},{"type":"fn","props":{"name":"tada","args":{}},"children":[{"type":"text","props":{"text":"world"}}]}]
```
### 利用可能なMFM関数のリストを設定する
MFM関数の名前をホワイトリストに登録して、登録されたMFM関数以外を通常のテキストノードとして解釈するように設定できます。
デフォルトではすべてのMFM関数名を受け入れるように設定されています。
### 最大のネストの深さを変更する
デフォルトで20に設定されています。
例:
```ts
const nodes = mfm.parse('hello $[tada world]', { fnNameList: ['tada', 'spin'] });
const nodes = mfm.parse('**<s>cannot nest</s>**', { nestLimit: 1 });
console.log(JSON.stringify(nodes));
// => "[{"type":"text","props":{"text":"hello "}},{"type":"fn","props":{"name":"tada","args":{}},"children":[{"type":"text","props":{"text":"world"}}]}]"
// => [{"type":"bold","children":[{"type":"text","props":{"text":"<s>cannot nest</s>"}}]}]
```
```ts
const nodes = mfm.parse('hello $[pope world]', { fnNameList: ['tada', 'spin'] });
console.log(JSON.stringify(nodes));
// => "[{"type":"text","props":{"text":"hello $[pope world]"}}]"
```
## parsePlain API
## parseSimple API
入力文字列からノードツリーを生成します。
絵文字コードとUnicode絵文字を利用可能です。
例:
```ts
const nodes = mfm.parsePlain('Hello :surprised_ai:');
const nodes = mfm.parseSimple('Hello :surprised_ai:');
console.log(JSON.stringify(nodes));
// => "[{"type":"text","props":{"text":"Hello "}},{"type":"emojiCode","props":{"name":"surprised_ai"}}]"
// => [{"type":"text","props":{"text":"Hello "}},{"type":"emojiCode","props":{"name":"surprised_ai"}}]
```
## toString API
......
......@@ -160,7 +160,6 @@ abc
## 詳細
- `<center>`は行頭でなければならない。
- `</center>`は行末でなければならない。
- 中身を空にすることはできない。
- 内容には再度InlineParserを適用する。
## ノード
......@@ -185,7 +184,8 @@ abc
## 詳細
- 内容には再度InlineParserを適用する。
- 内容には改行も含めることが可能です。
- 内容にはすべての文字、改行が使用できる。
- 内容を空にすることはできない。
## ノード
```js
......@@ -223,6 +223,7 @@ __bold__
## 詳細
- 内容には再度InlineParserを適用する。
- 内容を空にすることはできない。
構文1,3のみ:
- 内容にはすべての文字、改行が使用できる。
......@@ -251,7 +252,8 @@ __bold__
## 詳細
- 内容には再度InlineParserを適用する。
- 内容には改行も含めることが可能です。
- 内容を空にすることはできない。
- 内容にはすべての文字、改行が使用できる。
## ノード
```js
......@@ -285,14 +287,15 @@ _italic_
## 詳細
- 内容には再度InlineParserを適用する。
- 内容を空にすることはできない。
構文1のみ:
- 内容には改行も含めることが可能です
- 内容にはすべての文字、改行が使用できる
構文2,3のみ:
※1つ目の`*``_`を開始記号と呼ぶ。
- 内容には`[a-z0-9 \t]i`にマッチする文字が使用できる。
- 開始記号の前の文字が(無い、改行、半角スペース、[a-zA-Z0-9]に一致しない)のいずれかの時にイタリック文字として判定される。
- 開始記号の前の文字が`[a-z0-9]i`に一致しない時にイタリック文字として判定される。
## ノード
```js
......@@ -321,12 +324,13 @@ _italic_
## 詳細
- 内容には再度InlineParserを適用する。
- 内容を空にすることはできない。
構文1のみ:
- 内容には`~`、改行以外の文字を使用できる。
構文2のみ:
- 内容には改行以外の文字を使用できる。
- 内容にはすべての文字、改行が使用できる。
## ノード
```js
......@@ -348,6 +352,7 @@ _italic_
```
## 詳細
- 内容を空にすることはできない。
- 内容には改行を含めることができない。
- 内容には「´」を含めることができない。
......@@ -371,6 +376,7 @@ _italic_
```
## 詳細
- 内容を空にすることはできない。
- 内容には改行を含めることができない。
## ノード
......@@ -396,7 +402,7 @@ _italic_
```
## 詳細
- 最初の`@`の前の文字が(改行、スペース、無し、[a-zA-Z0-9]に一致しない)のいずれかの場合にメンションとして認識する。
- 最初の`@`の前の文字が`[a-z0-9]i`に一致しない場合にメンションとして認識する。
### ユーザ名
- 1文字以上。
......@@ -441,10 +447,11 @@ _italic_
```
## 詳細
- 内容を空にすることはできない。
- 内容には半角スペース、全角スペース、改行、タブ文字を含めることができない。
- 内容には`.` `,` `!` `?` `'` `"` `#` `:` `/` `【` `】` `<` `>` を含めることができない。
- 括弧は対になっている時のみ内容に含めることができる。対象: `()` `[]` `「」`
- `#`の前の文字が(改行、スペース、無し、[a-zA-Z0-9]に一致しない)のいずれかの場合にハッシュタグとして認識する。
- 内容には`.` `,` `!` `?` `'` `"` `#` `:` `/` `【` `】` `<` `>` `【` `】` `(` `)` `「` `」` `(` `)` を含めることができない。
- 括弧は対になっている時のみ内容に含めることができる。対象: `()` `[]` `「」` `()`
- `#`の前の文字が`[a-z0-9]i`に一致しない場合にハッシュタグとして認識する。
- 内容が数字のみの場合はハッシュタグとして認識しない。
## ノード
......@@ -539,8 +546,7 @@ silent=true
```
## 詳細
- リンクラベルには再度InlineParserを適用する。
- リンクラベルに含まれるURL、リンク、メンションはテキストとしてパースする。
- 表示テキストには再度InlineParserを適用する。ただし、表示テキストではURL、リンク、メンションは使用できない。
## ノード
```js
......@@ -573,6 +579,7 @@ silent=true
```
## 詳細
- 内容を空にすることはできない。
- 内容には[a-z0-9_+-]iにマッチする文字を使用できる。
## ノード
......@@ -609,6 +616,7 @@ $[flip.h,v MisskeyでFediverseの世界が広がります]
## 詳細
- 内容には再度InlineParserを適用する。
- 内容を空にすることはできない。
- 内容には改行も含めることが可能です。
## ノード
......
......@@ -105,7 +105,7 @@ export type MfmHashtag = {
};
// @public (undocumented)
export type MfmInline = MfmUnicodeEmoji | MfmEmojiCode | MfmBold | MfmSmall | MfmItalic | MfmStrike | MfmInlineCode | MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText;
export type MfmInline = MfmUnicodeEmoji | MfmEmojiCode | MfmBold | MfmSmall | MfmItalic | MfmStrike | MfmInlineCode | MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmPlain | MfmText;
// @public (undocumented)
export type MfmInlineCode = {
......@@ -165,6 +165,13 @@ export type MfmMention = {
// @public (undocumented)
export type MfmNode = MfmBlock | MfmInline;
// @public (undocumented)
export type MfmPlain = {
type: 'plain';
props?: Record<string, unknown>;
children: MfmText[];
};
// @public (undocumented)
export type MfmQuote = {
type: 'quote';
......@@ -182,6 +189,9 @@ export type MfmSearch = {
children?: [];
};
// @public (undocumented)
export type MfmSimpleNode = MfmUnicodeEmoji | MfmEmojiCode | MfmText | MfmPlain;
// @public (undocumented)
export type MfmSmall = {
type: 'small';
......@@ -225,20 +235,21 @@ export type MfmUrl = {
};
// @public (undocumented)
export const N_URL: (value: string, brackets?: boolean | undefined) => NodeType<'url'>;
export const N_URL: (value: string, brackets?: boolean) => NodeType<'url'>;
// @public (undocumented)
export type NodeType<T extends MfmNode['type']> = T extends 'quote' ? MfmQuote : T extends 'search' ? MfmSearch : T extends 'blockCode' ? MfmCodeBlock : T extends 'mathBlock' ? MfmMathBlock : T extends 'center' ? MfmCenter : T extends 'unicodeEmoji' ? MfmUnicodeEmoji : T extends 'emojiCode' ? MfmEmojiCode : T extends 'bold' ? MfmBold : T extends 'small' ? MfmSmall : T extends 'italic' ? MfmItalic : T extends 'strike' ? MfmStrike : T extends 'inlineCode' ? MfmInlineCode : T extends 'mathInline' ? MfmMathInline : T extends 'mention' ? MfmMention : T extends 'hashtag' ? MfmHashtag : T extends 'url' ? MfmUrl : T extends 'link' ? MfmLink : T extends 'fn' ? MfmFn : T extends 'text' ? MfmText : never;
export type NodeType<T extends MfmNode['type']> = T extends 'quote' ? MfmQuote : T extends 'search' ? MfmSearch : T extends 'blockCode' ? MfmCodeBlock : T extends 'mathBlock' ? MfmMathBlock : T extends 'center' ? MfmCenter : T extends 'unicodeEmoji' ? MfmUnicodeEmoji : T extends 'emojiCode' ? MfmEmojiCode : T extends 'bold' ? MfmBold : T extends 'small' ? MfmSmall : T extends 'italic' ? MfmItalic : T extends 'strike' ? MfmStrike : T extends 'inlineCode' ? MfmInlineCode : T extends 'mathInline' ? MfmMathInline : T extends 'mention' ? MfmMention : T extends 'hashtag' ? MfmHashtag : T extends 'url' ? MfmUrl : T extends 'link' ? MfmLink : T extends 'fn' ? MfmFn : T extends 'plain' ? MfmPlain : T extends 'text' ? MfmText : never;
// @public (undocumented)
export function parse(input: string, opts?: Partial<{
fnNameList: string[];
nestLimit: number;
}>): MfmNode[];
// Warning: (ae-forgotten-export) The symbol "MfmPlainNode" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export function parsePlain(input: string): MfmPlainNode[];
export function parseSimple(input: string): MfmSimpleNode[];
// @public (undocumented)
export const PLAIN: (text: string) => NodeType<'plain'>;
// @public (undocumented)
export const QUOTE: (children: MfmNode[]) => NodeType<'quote'>;
......
This diff is collapsed.
{
"name": "mfm-js",
"version": "0.20.0",
"description": "An MFM parser implementation with PEG.js",
"name": "@transfem-org/sfm-js",
"version": "0.24.4",
"description": "An SFM parser implementation with fixes based on MFM",
"main": "./built/index.js",
"types": "./built/index.d.ts",
"scripts": {
"build": "npm run tsc && npm run peg",
"build-debug": "npm run tsc && npm run peg-debug",
"peg": "peggy --cache -o src/internal/parser.js --allowed-start-rules fullParser,inlineParser,plainParser src/internal/parser.pegjs && npm run peg-copy",
"peg-debug": "peggy --cache -o src/internal/parser.js --allowed-start-rules fullParser,inlineParser,plainParser --trace src/internal/parser.pegjs && npm run peg-copy",
"peg-copy": "copyfiles -f src/internal/parser.js built/internal/",
"build": "npm run tsc",
"tsc": "tsc",
"tsd": "tsd",
"parse": "node ./built/cli/parse",
"parse-plain": "node ./built/cli/parsePlain",
"parse-simple": "node ./built/cli/parseSimple",
"api": "npx api-extractor run --local --verbose",
"api-prod": "npx api-extractor run --verbose",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
......@@ -22,29 +18,29 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/misskey-dev/mfm.js.git"
"url": "git+https://activitypub.software/TransFem-org/sfm.js.git"
},
"author": "Marihachi",
"author": "Marie",
"license": "MIT",
"devDependencies": {
"@microsoft/api-extractor": "^7.18.4",
"@types/jest": "^26.0.23",
"@types/node": "15.12.4",
"@typescript-eslint/eslint-plugin": "^4.28.5",
"@typescript-eslint/parser": "^4.28.5",
"copyfiles": "^2.4.1",
"eslint": "^7.32.0",
"jest": "^27.0.5",
"peggy": "1.2.0",
"ts-jest": "^27.0.3",
"ts-node": "10.0.0",
"tsd": "^0.17.0",
"typescript": "4.3.4"
"@microsoft/api-extractor": "7.38.5",
"@types/jest": "29.5.11",
"@types/node": "20.10.5",
"@typescript-eslint/eslint-plugin": "6.14.0",
"@typescript-eslint/parser": "6.14.0",
"eslint": "8.56.0",
"jest": "29.7.0",
"ts-jest": "29.1.1",
"ts-node": "10.9.2",
"tsd": "0.30.0",
"tslib": "2.6.2",
"typescript": "5.3.3"
},
"dependencies": {
"twemoji-parser": "13.1.x"
"@twemoji/parser": "15.0.0"
},
"files": [
"built"
"built",
"CHANGELOG.md"
]
}
declare module '@twemoji/parser/dist/lib/regex' {
const regex: RegExp;
export default regex;
}
import peg from 'peggy';
import { MfmNode, MfmPlainNode } from './node';
import { stringifyNode, stringifyTree, inspectOne } from './internal/util';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const parser: peg.Parser = require('./internal/parser');
import { fullParser, simpleParser } from './internal';
import { inspectOne, stringifyNode, stringifyTree } from './internal/util';
import { MfmNode, MfmSimpleNode } from './node';
/**
* Generates a MfmNode tree from the MFM string.
*/
export function parse(input: string, opts: Partial<{ fnNameList: string[]; }> = {}): MfmNode[] {
const nodes = parser.parse(input, { startRule: 'fullParser', fnNameList: opts.fnNameList });
export function parse(input: string, opts: Partial<{ nestLimit: number; }> = {}): MfmNode[] {
const nodes = fullParser(input, {
nestLimit: opts.nestLimit,
});
return nodes;
}
/**
* Generates a MfmNode tree of plain from the MFM string.
* Generates a MfmSimpleNode tree from the MFM string.
*
* "Simple" MFM only recognises text and emojis
*/
export function parsePlain(input: string): MfmPlainNode[] {
const nodes = parser.parse(input, { startRule: 'plainParser' });
export function parseSimple(input: string): MfmSimpleNode[] {
const nodes = simpleParser(input);
return nodes;
}
/**
* Generates a MFM string from the MfmNode tree.
*
* Notice that the result of `toString(parse(someString))` will very
* probably not be identical to `someString`
*/
export function toString(tree: MfmNode[]): string
export function toString(node: MfmNode): string
......@@ -37,6 +41,17 @@ export function toString(node: MfmNode | MfmNode[]): string {
/**
* Inspects the MfmNode tree.
*
* This is the visitor pattern. Your `action` will be called on each
* node of the tree, as a depth-first pre-visit:
*
* ```
* A
* +B
* |+C
* |+D
* +E
* ```
*/
export function inspect(node: MfmNode, action: (node: MfmNode) => void): void
export function inspect(nodes: MfmNode[], action: (node: MfmNode) => void): void
......
......@@ -2,7 +2,7 @@ import { performance } from 'perf_hooks';
import inputLine, { InputCanceledError } from './misc/inputLine';
import { parse } from '..';
async function entryPoint() {
async function entryPoint(): Promise<void> {
console.log('intaractive parser');
while (true) {
......