From c9758200833cfd52ff4973b7311435c1812098c9 Mon Sep 17 00:00:00 2001 From: marihachi Date: Sun, 28 Mar 2021 13:29:07 +0900 Subject: [PATCH] Isolate node of unicode emoji and custom emoji. --- src/index.ts | 3 ++- src/node.ts | 20 ++++++++++++++------ src/parser.pegjs | 4 ++-- src/util.ts | 15 +++++---------- test/node.ts | 8 ++++---- 5 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/index.ts b/src/index.ts index 16a2afc..63a844a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,7 +46,8 @@ export { MfmCenter, // inline - MfmEmoji, + MfmUnicodeEmoji, + MfmCustomEmoji, MfmBold, MfmSmall, MfmItalic, diff --git a/src/node.ts b/src/node.ts index 0455693..f110857 100644 --- a/src/node.ts +++ b/src/node.ts @@ -45,18 +45,26 @@ export type MfmCenter = { children: MfmInline[]; }; -export type MfmInline = MfmEmoji | MfmBold | MfmSmall | MfmItalic | MfmStrike | MfmInlineCode | - MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText; +export type MfmInline = MfmUnicodeEmoji | MfmCustomEmoji | MfmBold | MfmSmall | MfmItalic | MfmStrike | + MfmInlineCode | MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText; -export type MfmEmoji = { - type: 'emoji'; +export type MfmUnicodeEmoji = { + type: 'unicodeEmoji'; props: { - emoji?: string; - name?: string; + emoji: string; }; children?: []; }; +export type MfmCustomEmoji = { + type: 'customEmoji'; + props: { + name: string; + }; + children?: []; +}; + + export type MfmBold = { type: 'bold'; props?: { }; diff --git a/src/parser.pegjs b/src/parser.pegjs index b1795f3..b24b31a 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -158,7 +158,7 @@ emoji customEmoji = ":" name:emojiName ":" { - return createNode('emoji', { name: name }); + return createNode('customEmoji', { name: name }); } emojiName @@ -168,7 +168,7 @@ emojiName unicodeEmoji = &{ return matchUnicodeEmoji(); } (&{ return consumeDynamically(); } .)+ { - return createNode('emoji', { emoji: text() }); + return createNode('unicodeEmoji', { emoji: text() }); } // inline: big diff --git a/src/util.ts b/src/util.ts index 12a21e8..5c21331 100644 --- a/src/util.ts +++ b/src/util.ts @@ -60,16 +60,11 @@ export function stringifyNode(node: MfmNode): string { return `
\n${ stringifyTree(node.children) }\n
`; } // inline - case 'emoji': { - if (node.props.name) { - return `:${ node.props.name }:`; - } - else if (node.props.emoji) { - return node.props.emoji; - } - else { - return ''; - } + case 'customEmoji': { + return `:${ node.props.name }:`; + } + case 'unicodeEmoji': { + return node.props.emoji; } case 'bold': { return `**${ stringifyTree(node.children) }**`; diff --git a/test/node.ts b/test/node.ts index 15ad6b9..216a771 100644 --- a/test/node.ts +++ b/test/node.ts @@ -1,7 +1,7 @@ import { - MfmBold, MfmCenter, MfmCodeBlock, MfmEmoji, MfmFn, MfmHashtag, MfmInline, + MfmBold, MfmCenter, MfmCodeBlock, MfmCustomEmoji, MfmFn, MfmHashtag, MfmInline, MfmInlineCode, MfmItalic, MfmLink, MfmMathBlock, MfmMathInline, MfmMention, - MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUrl + MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUnicodeEmoji, MfmUrl } from '../built'; export const QUOTE = (children: MfmNode[]): MfmQuote => { return { type:'quote', children }; }; @@ -20,7 +20,7 @@ export const MENTION = (username: string, host: string | null, acct: string): Mf export const HASHTAG = (value: string): MfmHashtag => { return { type:'hashtag', props: { hashtag: value } }; }; export const N_URL = (value: string): MfmUrl => { return { type:'url', props: { url: value } }; }; export const LINK = (silent: boolean, url: string, children: MfmInline[]): MfmLink => { return { type:'link', props: { silent, url }, children }; }; -export const CUSTOM_EMOJI = (name: string): MfmEmoji => { return { type:'emoji', props: { name: name } }; }; +export const CUSTOM_EMOJI = (name: string): MfmCustomEmoji => { return { type:'customEmoji', props: { name: name } }; }; export const FN = (name: string, args: MfmFn['props']['args'], children: MfmFn['children']): MfmFn => { return { type:'fn', props: { name, args }, children }; }; -export const UNI_EMOJI = (value: string): MfmEmoji => { return { type:'emoji', props: { emoji: value } }; }; +export const UNI_EMOJI = (value: string): MfmUnicodeEmoji => { return { type:'unicodeEmoji', props: { emoji: value } }; }; export const TEXT = (value: string): MfmText => { return { type:'text', props: { text: value } }; };