aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-04-05 08:57:10 +0300
committereug-vs <eug-vs@keemail.me>2020-04-05 08:57:10 +0300
commit21cb0da607e4c0b71f847756e2736cf99b143885 (patch)
tree4adac1749c19fc87fe0383c1318807366883ec21
parent0389a542776f8f7fc3bc71a54b0aca7179f6fed3 (diff)
downloadreact-benzin-21cb0da607e4c0b71f847756e2736cf99b143885.tar.gz
feat: parse emoji :fire:
-rw-r--r--src/lib/Markdown/Paragraph.tsx24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/lib/Markdown/Paragraph.tsx b/src/lib/Markdown/Paragraph.tsx
index a473142..19f8382 100644
--- a/src/lib/Markdown/Paragraph.tsx
+++ b/src/lib/Markdown/Paragraph.tsx
@@ -1,11 +1,17 @@
import React from 'react';
import { Link } from '@material-ui/core';
+import axios from 'axios';
interface PropTypes {
data: string;
}
-type InlineRuleName = 'bold' | 'italic' | 'code' | 'strikeThrough';
+interface Emoji {
+ name: string;
+ char: string;
+}
+
+type InlineRuleName = 'bold' | 'italic' | 'code' | 'strikeThrough' | 'emoji';
interface InlineRuleProperties {
enclosure: string;
@@ -31,6 +37,10 @@ const inlineRules: Record<InlineRuleName, InlineRuleProperties>= {
enclosure: '\\*\\*',
style: { fontWeight: 'bold' },
},
+ emoji: {
+ enclosure: ':',
+ style: {},
+ }
};
const inlineRuleNames = Object.keys(inlineRules) as InlineRuleName[];
@@ -54,6 +64,11 @@ inlineRuleNames.forEach(name => {
const splitter = new RegExp(ruleSplitPatterns.join('|'));
+const emojiList: Emoji[] = [];
+axios.get('https://unpkg.com/emojilib@2.4.0/emojis.json').then(response => {
+ Object.keys(response.data).forEach(name => emojiList.push({ name, char: response.data[name].char }));
+});
+
const SyntaxSpan: React.FC<PropTypes> = ({ data }) => {
if (!data) return null;
@@ -69,7 +84,12 @@ const SyntaxSpan: React.FC<PropTypes> = ({ data }) => {
inlineRuleNames.forEach(name => {
const rule = inlineRules[name];
const match = data.match(rule.pattern || '');
- if (match) span = <span style={rule.style}>{match[1]}</span>;
+ if (match) {
+ if (name === 'emoji') {
+ const emoji = emojiList.find(emoji => emoji.name === match[1]);
+ span = <span>{emoji ? emoji.char : ''}</span>;
+ } else span = <span style={rule.style}>{match[1]}</span>;
+ }
});
return span;
}