From 56002e86ec2740ae7e2eae83fd1ae019ae9f92ce Mon Sep 17 00:00:00 2001 From: marihachi Date: Tue, 10 Jan 2023 23:16:33 +0900 Subject: [PATCH] add emojiCodeList option --- etc/mfm-js.api.md | 1 + src/api.ts | 3 ++- src/internal/index.ts | 2 ++ src/internal/parser.ts | 14 ++++++++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/etc/mfm-js.api.md b/etc/mfm-js.api.md index 67cd37d..84667c4 100644 --- a/etc/mfm-js.api.md +++ b/etc/mfm-js.api.md @@ -243,6 +243,7 @@ export type NodeType = T extends 'quote' ? MfmQuote : // @public (undocumented) export function parse(input: string, opts?: Partial<{ fnNameList: string[]; + emojiCodeList: string[]; nestLimit: number; }>): MfmNode[]; diff --git a/src/api.ts b/src/api.ts index f3194a8..3e8244e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -5,9 +5,10 @@ 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<{ fnNameList: string[]; emojiCodeList: string[]; nestLimit: number; }> = {}): MfmNode[] { const nodes = fullParser(input, { fnNameList: opts.fnNameList, + emojiCodeList: opts.emojiCodeList, nestLimit: opts.nestLimit, }); return nodes; diff --git a/src/internal/index.ts b/src/internal/index.ts index 86eb7d8..0ae7223 100644 --- a/src/internal/index.ts +++ b/src/internal/index.ts @@ -5,6 +5,7 @@ import * as P from './core'; export type FullParserOpts = { fnNameList?: string[]; + emojiCodeList?: string[]; nestLimit?: number; }; @@ -12,6 +13,7 @@ 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, + emojiCodeList: opts.emojiCodeList, depth: 0, linkLabel: false, trace: false, diff --git a/src/internal/parser.ts b/src/internal/parser.ts index 3d1a04f..dd76352 100644 --- a/src/internal/parser.ts +++ b/src/internal/parser.ts @@ -628,13 +628,23 @@ export const language = P.createLanguage({ emojiCode: r => { const side = P.notMatch(P.regexp(/[a-z0-9]/i)); const mark = P.str(':'); - return P.seq([ + const parser = P.seq([ P.alt([P.lineBegin, side]), mark, P.regexp(/[a-z0-9_+-]+/i), mark, P.alt([P.lineEnd, side]), - ], 2).map(name => M.EMOJI_CODE(name as string)); + ], 2); + return new P.Parser((input, index, state) => { + const result = parser.handler(input, index, state); + if (!result.success) { + return P.failure(); + } + if (state.emojiCodeList != null && !state.emojiCodeList.includes(result.value)) { + return P.failure(); + } + return P.success(result.index, M.EMOJI_CODE(result.value as string)); + }); }, link: r => {