From 2756d9a8db2a49db36dc87d31d85283bb7a992f9 Mon Sep 17 00:00:00 2001
From: eug-vs <eugene@eug-vs.xyz>
Date: Fri, 23 Sep 2022 13:21:23 +0300
Subject: feat: add remark-gemoji plugin

---
 src/emojiPlugin.tsx | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

(limited to 'src/emojiPlugin.tsx')

diff --git a/src/emojiPlugin.tsx b/src/emojiPlugin.tsx
index a408ed7..e23ac37 100644
--- a/src/emojiPlugin.tsx
+++ b/src/emojiPlugin.tsx
@@ -16,26 +16,28 @@ const emojiPlugin = (emojiFileNames: string[]) => () => {
       const emoji = match[0]?.replaceAll(':', '') || '';
       const src = emojiFileNames.find(fileName => fileName.match(`${emoji}.*`));
 
-      const beforeNode = {
-        type: 'text',
-        value: text.slice(0, match.index),
-      }
-
-      const emojiNode = {
-        type: 'element',
-        tagName: 'emoji',
-        children: [{
+      if (src) {
+        const beforeNode = {
           type: 'text',
-          value: src,
-        }],
-      }
+          value: text.slice(0, match.index),
+        }
+
+        const emojiNode = {
+          type: 'element',
+          tagName: 'emoji',
+          children: [{
+            type: 'text',
+            value: src,
+          }],
+        }
+
+        const afterNode = {
+          type: 'text',
+          value: text.slice(match.index + emoji?.length + 2),
+        }
 
-      const afterNode = {
-        type: 'text',
-        value: text.slice(match.index + emoji?.length + 2),
+        parent.children.splice(nodeIndex, 1, ...[beforeNode, emojiNode, afterNode])
       }
-
-      parent.children.splice(nodeIndex, 1, ...[beforeNode, emojiNode, afterNode])
     }
 
   }
-- 
cgit v1.2.3

