Merge branch 'improve-emoji' into develop

This commit is contained in:
marihachi 2021-03-28 14:13:37 +09:00
commit d82b12146a
6 changed files with 38 additions and 35 deletions

View file

@ -46,7 +46,8 @@ export {
MfmCenter,
// inline
MfmEmoji,
MfmUnicodeEmoji,
MfmEmojiCode,
MfmBold,
MfmSmall,
MfmItalic,

View file

@ -45,14 +45,21 @@ 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 | MfmEmojiCode | 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 MfmEmojiCode = {
type: 'emojiCode';
props: {
name: string;
};
children?: [];
};

View file

@ -42,7 +42,7 @@ fullParser
= nodes:(&. n:(block / inline) { return n; })* { return mergeText(nodes); }
plainParser
= nodes:(&. n:(emoji / text) { return n; })* { return mergeText(nodes); }
= nodes:(&. n:(emojiCode / unicodeEmoji / text) { return n; })* { return mergeText(nodes); }
inlineParser
= nodes:(&. n:inline { return n; })* { return mergeText(nodes); }
@ -135,7 +135,8 @@ center
//
inline
= emoji
= emojiCode
/ unicodeEmoji
/ big
/ bold
/ small
@ -150,25 +151,24 @@ inline
/ fn
/ text
// inline: emoji
// inline: emoji code
emoji
= customEmoji / unicodeEmoji
customEmoji
= ":" name:emojiName ":"
emojiCode
= ":" name:emojiCodeName ":"
{
return createNode('emoji', { name: name });
return createNode('emojiCode', { name: name });
}
emojiName
emojiCodeName
= [a-z0-9_+-]i+ { return text(); }
// inline: unicode emoji
// NOTE: if the text matches one of the emojis, it will count the length of the emoji sequence and consume it.
unicodeEmoji
= &{ return matchUnicodeEmoji(); } (&{ return consumeDynamically(); } .)+
{
return createNode('emoji', { emoji: text() });
return createNode('unicodeEmoji', { emoji: text() });
}
// inline: big

View file

@ -60,16 +60,11 @@ export function stringifyNode(node: MfmNode): string {
return `<center>\n${ stringifyTree(node.children) }\n</center>`;
}
// inline
case 'emoji': {
if (node.props.name) {
return `:${ node.props.name }:`;
}
else if (node.props.emoji) {
return node.props.emoji;
}
else {
return '';
}
case 'emojiCode': {
return `:${ node.props.name }:`;
}
case 'unicodeEmoji': {
return node.props.emoji;
}
case 'bold': {
return `**${ stringifyTree(node.children) }**`;

View file

@ -2,7 +2,7 @@ import assert from 'assert';
import { inspect, parse, parsePlain, toString } from '../built/index';
import { createNode } from '../built/util';
import {
TEXT, CENTER, FN, UNI_EMOJI, MENTION, CUSTOM_EMOJI, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE, MATH_BLOCK, SEARCH, CODE_BLOCK
TEXT, CENTER, FN, UNI_EMOJI, MENTION, EMOJI_CODE, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE, MATH_BLOCK, SEARCH, CODE_BLOCK
} from './node';
describe('text', () => {
@ -210,10 +210,10 @@ describe('center', () => {
});
});
describe('custom emoji', () => {
describe('emoji code', () => {
it('basic', () => {
const input = ':abc:';
const output = [CUSTOM_EMOJI('abc')];
const output = [EMOJI_CODE('abc')];
assert.deepStrictEqual(parse(input), output);
});
});

View file

@ -1,7 +1,7 @@
import {
MfmBold, MfmCenter, MfmCodeBlock, MfmEmoji, MfmFn, MfmHashtag, MfmInline,
MfmBold, MfmCenter, MfmCodeBlock, MfmEmojiCode, 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 EMOJI_CODE = (name: string): MfmEmojiCode => { return { type:'emojiCode', 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 } }; };