Skip to content
Snippets Groups Projects
Commit d82b1214 authored by marihachi's avatar marihachi
Browse files

Merge branch 'improve-emoji' into develop

parents f59529ff 4dcfbade
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,8 @@ export { ...@@ -46,7 +46,8 @@ export {
MfmCenter, MfmCenter,
// inline // inline
MfmEmoji, MfmUnicodeEmoji,
MfmEmojiCode,
MfmBold, MfmBold,
MfmSmall, MfmSmall,
MfmItalic, MfmItalic,
......
...@@ -45,14 +45,21 @@ export type MfmCenter = { ...@@ -45,14 +45,21 @@ export type MfmCenter = {
children: MfmInline[]; children: MfmInline[];
}; };
export type MfmInline = MfmEmoji | MfmBold | MfmSmall | MfmItalic | MfmStrike | MfmInlineCode | export type MfmInline = MfmUnicodeEmoji | MfmEmojiCode | MfmBold | MfmSmall | MfmItalic | MfmStrike |
MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText; MfmInlineCode | MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText;
export type MfmEmoji = { export type MfmUnicodeEmoji = {
type: 'emoji'; type: 'unicodeEmoji';
props: { props: {
emoji?: string; emoji: string;
name?: string; };
children?: [];
};
export type MfmEmojiCode = {
type: 'emojiCode';
props: {
name: string;
}; };
children?: []; children?: [];
}; };
......
...@@ -42,7 +42,7 @@ fullParser ...@@ -42,7 +42,7 @@ fullParser
= nodes:(&. n:(block / inline) { return n; })* { return mergeText(nodes); } = nodes:(&. n:(block / inline) { return n; })* { return mergeText(nodes); }
plainParser plainParser
= nodes:(&. n:(emoji / text) { return n; })* { return mergeText(nodes); } = nodes:(&. n:(emojiCode / unicodeEmoji / text) { return n; })* { return mergeText(nodes); }
inlineParser inlineParser
= nodes:(&. n:inline { return n; })* { return mergeText(nodes); } = nodes:(&. n:inline { return n; })* { return mergeText(nodes); }
...@@ -135,7 +135,8 @@ center ...@@ -135,7 +135,8 @@ center
// //
inline inline
= emoji = emojiCode
/ unicodeEmoji
/ big / big
/ bold / bold
/ small / small
...@@ -150,25 +151,24 @@ inline ...@@ -150,25 +151,24 @@ inline
/ fn / fn
/ text / text
// inline: emoji // inline: emoji code
emoji emojiCode
= customEmoji / unicodeEmoji = ":" name:emojiCodeName ":"
customEmoji
= ":" name:emojiName ":"
{ {
return createNode('emoji', { name: name }); return createNode('emojiCode', { name: name });
} }
emojiName emojiCodeName
= [a-z0-9_+-]i+ { return text(); } = [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. // NOTE: if the text matches one of the emojis, it will count the length of the emoji sequence and consume it.
unicodeEmoji unicodeEmoji
= &{ return matchUnicodeEmoji(); } (&{ return consumeDynamically(); } .)+ = &{ return matchUnicodeEmoji(); } (&{ return consumeDynamically(); } .)+
{ {
return createNode('emoji', { emoji: text() }); return createNode('unicodeEmoji', { emoji: text() });
} }
// inline: big // inline: big
......
...@@ -60,16 +60,11 @@ export function stringifyNode(node: MfmNode): string { ...@@ -60,16 +60,11 @@ export function stringifyNode(node: MfmNode): string {
return `<center>\n${ stringifyTree(node.children) }\n</center>`; return `<center>\n${ stringifyTree(node.children) }\n</center>`;
} }
// inline // inline
case 'emoji': { case 'emojiCode': {
if (node.props.name) { return `:${ node.props.name }:`;
return `:${ node.props.name }:`; }
} case 'unicodeEmoji': {
else if (node.props.emoji) { return node.props.emoji;
return node.props.emoji;
}
else {
return '';
}
} }
case 'bold': { case 'bold': {
return `**${ stringifyTree(node.children) }**`; return `**${ stringifyTree(node.children) }**`;
......
...@@ -2,7 +2,7 @@ import assert from 'assert'; ...@@ -2,7 +2,7 @@ import assert from 'assert';
import { inspect, parse, parsePlain, toString } from '../built/index'; import { inspect, parse, parsePlain, toString } from '../built/index';
import { createNode } from '../built/util'; import { createNode } from '../built/util';
import { 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'; } from './node';
describe('text', () => { describe('text', () => {
...@@ -210,10 +210,10 @@ describe('center', () => { ...@@ -210,10 +210,10 @@ describe('center', () => {
}); });
}); });
describe('custom emoji', () => { describe('emoji code', () => {
it('basic', () => { it('basic', () => {
const input = ':abc:'; const input = ':abc:';
const output = [CUSTOM_EMOJI('abc')]; const output = [EMOJI_CODE('abc')];
assert.deepStrictEqual(parse(input), output); assert.deepStrictEqual(parse(input), output);
}); });
}); });
......
import { import {
MfmBold, MfmCenter, MfmCodeBlock, MfmEmoji, MfmFn, MfmHashtag, MfmInline, MfmBold, MfmCenter, MfmCodeBlock, MfmEmojiCode, MfmFn, MfmHashtag, MfmInline,
MfmInlineCode, MfmItalic, MfmLink, MfmMathBlock, MfmMathInline, MfmMention, MfmInlineCode, MfmItalic, MfmLink, MfmMathBlock, MfmMathInline, MfmMention,
MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUrl MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUnicodeEmoji, MfmUrl
} from '../built'; } from '../built';
export const QUOTE = (children: MfmNode[]): MfmQuote => { return { type:'quote', children }; }; 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 ...@@ -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 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 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 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 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 } }; }; export const TEXT = (value: string): MfmText => { return { type:'text', props: { text: value } }; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment