remove fnNameList option for more consistency

This commit is contained in:
syuilo 2023-01-14 08:56:48 +09:00
parent 2a3498b8f2
commit f916d5bd8f
6 changed files with 1 additions and 52 deletions

View file

@ -9,32 +9,6 @@ console.log(JSON.stringify(nodes));
// => [{"type":"text","props":{"text":"hello "}},{"type":"fn","props":{"name":"tada","args":{}},"children":[{"type":"text","props":{"text":"world"}}]}]
```
### 利用可能なMFM関数やカスタム絵文字のリストを設定する
MFM関数の名前をホワイトリストに登録して、登録されたMFM関数以外を通常のテキストードとして解釈するように設定できます。
デフォルトではすべてのMFM関数名を受け入れるように設定されています。
例:
```ts
const nodes = mfm.parse('hello $[tada world]', { fnNameList: ['tada', 'spin'] });
console.log(JSON.stringify(nodes));
// => [{"type":"text","props":{"text":"hello "}},{"type":"fn","props":{"name":"tada","args":{}},"children":[{"type":"text","props":{"text":"world"}}]}]
```
```ts
const nodes = mfm.parse('hello $[pope world]', { fnNameList: ['tada', 'spin'] });
console.log(JSON.stringify(nodes));
// => [{"type":"text","props":{"text":"hello $[pope world]"}}]
```
同様に、カスタム絵文字の名前もホワイトリスト制にできます。
例:
```ts
const nodes = mfm.parse(':bap:', { emojiCodeList: ['polarbear', 'bap'] });
console.log(JSON.stringify(nodes));
// => [{"type":"emojiCode","props":{"name":"bap"}}]
```
### 最大のネストの深さを変更する
デフォルトで20に設定されています。

View file

@ -242,7 +242,6 @@ export type NodeType<T extends MfmNode['type']> = T extends 'quote' ? MfmQuote :
// @public (undocumented)
export function parse(input: string, opts?: Partial<{
fnNameList: string[];
nestLimit: number;
}>): MfmNode[];

View file

@ -5,9 +5,8 @@ import { MfmNode, MfmSimpleNode } from './node';
/**
* Generates a MfmNode tree from the MFM string.
*/
export function parse(input: string, opts: Partial<{ fnNameList: string[]; nestLimit: number; }> = {}): MfmNode[] {
export function parse(input: string, opts: Partial<{ nestLimit: number; }> = {}): MfmNode[] {
const nodes = fullParser(input, {
fnNameList: opts.fnNameList,
nestLimit: opts.nestLimit,
});
return nodes;

View file

@ -4,14 +4,12 @@ import { mergeText } from './util';
import * as P from './core';
export type FullParserOpts = {
fnNameList?: string[];
nestLimit?: number;
};
export function fullParser(input: string, opts: FullParserOpts): M.MfmNode[] {
const result = language.fullParser.handler(input, 0, {
nestLimit: (opts.nestLimit != null) ? opts.nestLimit : 20,
fnNameList: opts.fnNameList,
depth: 0,
linkLabel: false,
trace: false,

View file

@ -444,9 +444,6 @@ export const language = P.createLanguage({
if (!result.success) {
return result;
}
if (state.fnNameList != null && !state.fnNameList.includes(result.value)) {
return P.failure();
}
return P.success(result.index, result.value);
});
const arg: P.Parser<ArgPair> = P.seq([

View file

@ -1218,24 +1218,6 @@ hoge`;
];
assert.deepStrictEqual(mfm.parse(input), output);
});
it('exists name in the fnNameList', () => {
const input = '$[spin.speed=1.1s text]';
const output = [
FN('spin', { speed: '1.1s' }, [
TEXT('text')
])
];
assert.deepStrictEqual(mfm.parse(input, { fnNameList: ['tada', 'spin'] }), output);
});
it('not exists name in the fnNameList', () => {
const input = '$[pope.speed=1.1s text]';
const output = [
TEXT('$[pope.speed=1.1s text]')
];
assert.deepStrictEqual(mfm.parse(input, { fnNameList: ['tada', 'spin'] }), output);
});
});
describe('plain', () => {