Răsfoiți Sursa

feat: Fix emoji in TOC URL

https://youtrack.weseek.co.jp/issue/GW-7732
- Add method to replace emoji colons with emoji
- Make setTocHtml to async
- Implement colonsToEmoji function
I Komang Mudana 4 ani în urmă
părinte
comite
959729d3e7
1 a modificat fișierele cu 23 adăugiri și 2 ștergeri
  1. 23 2
      packages/app/src/client/services/PageContainer.js

+ 23 - 2
packages/app/src/client/services/PageContainer.js

@@ -17,6 +17,7 @@ import {
 import {
   DrawioInterceptor,
 } from '../util/interceptor/drawio-interceptor';
+import { emojiMartData } from '../util/markdown-it/emoji-mart-data';
 
 const { isTrashPage } = pagePathUtils;
 
@@ -368,12 +369,32 @@ export default class PageContainer extends Container {
     this.setState(newState);
   }
 
-  setTocHtml(tocHtml) {
+  async setTocHtml(tocHtml) {
     if (this.state.tocHtml !== tocHtml) {
-      this.setState({ tocHtml });
+      const tocHtmlWithEmoji = await this.colonsToEmoji(tocHtml);
+      this.setState({ tocHtml: tocHtmlWithEmoji });
     }
   }
 
+  /**
+   *
+   * @param {*} html TOC html string
+   * @returns TOC html with emoji (emoji-mart) in URL
+   */
+  async colonsToEmoji(html) {
+    // Emoji colons matching
+    const colons = ':[a-zA-Z0-9-_+]+:';
+    // Emoji with skin tone matching
+    const skin = ':skin-tone-[2-6]:';
+    const colonsRegex = new RegExp(`(${colons}${skin}|${colons})`, 'g');
+    const emojiData = await emojiMartData();
+    return html.replace(colonsRegex, (index, match) => {
+      const emojiName = match.slice(1, -1);
+      return emojiData[emojiName];
+    });
+
+  }
+
   /**
    * save success handler
    * @param {object} page Page instance