mirror of
https://activitypub.software/TransFem-org/sfm-js
synced 2024-11-25 07:25:13 +00:00
Supports white list for function syntax (#77)
* support name list for FN syntax * use default parameter Co-authored-by: Johann150 <johann.galle@protonmail.com> Co-authored-by: Johann150 <johann.galle@protonmail.com>
This commit is contained in:
parent
703f82215d
commit
00e89d23d7
4 changed files with 32 additions and 4 deletions
|
@ -231,7 +231,9 @@ export const N_URL: (value: string, brackets?: boolean | undefined) => NodeType<
|
||||||
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 'text' ? MfmText : never;
|
||||||
|
|
||||||
// @public (undocumented)
|
// @public (undocumented)
|
||||||
export function parse(input: string): MfmNode[];
|
export function parse(input: string, opts?: Partial<{
|
||||||
|
fnNameList: string[];
|
||||||
|
}>): MfmNode[];
|
||||||
|
|
||||||
// Warning: (ae-forgotten-export) The symbol "MfmPlainNode" needs to be exported by the entry point index.d.ts
|
// Warning: (ae-forgotten-export) The symbol "MfmPlainNode" needs to be exported by the entry point index.d.ts
|
||||||
//
|
//
|
||||||
|
|
|
@ -8,8 +8,8 @@ const parser: peg.Parser = require('./internal/parser');
|
||||||
/**
|
/**
|
||||||
* Generates a MfmNode tree from the MFM string.
|
* Generates a MfmNode tree from the MFM string.
|
||||||
*/
|
*/
|
||||||
export function parse(input: string): MfmNode[] {
|
export function parse(input: string, opts: Partial<{ fnNameList: string[]; }> = {}): MfmNode[] {
|
||||||
const nodes = parser.parse(input, { startRule: 'fullParser' });
|
const nodes = parser.parse(input, { startRule: 'fullParser', fnNameList: opts.fnNameList });
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,14 @@
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ensureFnName(name) {
|
||||||
|
if (!options.fnNameList) return true;
|
||||||
|
if (!Array.isArray(options.fnNameList)) {
|
||||||
|
error("options.fnNameList must be an array.");
|
||||||
|
}
|
||||||
|
return options.fnNameList.includes(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -414,7 +422,7 @@ linkUrl
|
||||||
// inline: fn
|
// inline: fn
|
||||||
|
|
||||||
fn
|
fn
|
||||||
= "$[" name:$([a-z0-9_]i)+ args:fnArgs? _ content:(!("]") @inline)+ "]"
|
= "$[" name:$([a-z0-9_]i)+ &{ return ensureFnName(name); } args:fnArgs? _ content:(!("]") @inline)+ "]"
|
||||||
{
|
{
|
||||||
args = args || {};
|
args = args || {};
|
||||||
return FN(name, args, mergeText(content));
|
return FN(name, args, mergeText(content));
|
||||||
|
|
|
@ -1050,6 +1050,24 @@ hoge`;
|
||||||
];
|
];
|
||||||
assert.deepStrictEqual(mfm.parse(input), output);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('composite', () => {
|
it('composite', () => {
|
||||||
|
|
Loading…
Reference in a new issue