From 0b1fe99ec56933f38c8c1e9ad19da6722e837122 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?David=20Sevilla=20Mart=C3=ADn?=
<6401250+datitisev@users.noreply.github.com>
Date: Thu, 15 Oct 2020 18:28:30 -0400
Subject: [PATCH] Add CDN constant & use jsDelivr in all places (#30)
* Add CDN file for twemoji version & assets base URL
* Add /assets/ to the URL so it's not repeated as much
* The previous MaxCDN URL didn't need /assets/ because it was specificly for twemoji
* Remove export of CDN - shouldn't be part of this PR
---
extensions/emoji/js/src/forum/addComposerAutocomplete.js | 5 +++--
extensions/emoji/js/src/forum/cdn.js | 5 +++++
extensions/emoji/js/src/forum/renderEmoji.js | 6 ++++--
3 files changed, 12 insertions(+), 4 deletions(-)
create mode 100644 extensions/emoji/js/src/forum/cdn.js
diff --git a/extensions/emoji/js/src/forum/addComposerAutocomplete.js b/extensions/emoji/js/src/forum/addComposerAutocomplete.js
index b465bb20d..1c64aa678 100644
--- a/extensions/emoji/js/src/forum/addComposerAutocomplete.js
+++ b/extensions/emoji/js/src/forum/addComposerAutocomplete.js
@@ -4,10 +4,11 @@ import emojiMap from 'simple-emoji-map';
import { extend } from 'flarum/extend';
import TextEditor from 'flarum/components/TextEditor';
import TextEditorButton from 'flarum/components/TextEditorButton';
-import getEmojiIconCode from './helpers/getEmojiIconCode';
import KeyboardNavigatable from 'flarum/utils/KeyboardNavigatable';
import AutocompleteDropdown from './fragments/AutocompleteDropdown';
+import getEmojiIconCode from './helpers/getEmojiIconCode';
+import cdn from './cdn';
export default function addComposerAutocomplete() {
const emojiKeys = Object.keys(emojiMap);
@@ -76,7 +77,7 @@ export default function addComposerAutocomplete() {
onmouseenter={function() {
dropdown.setIndex($(this).parent().index() - 1);
}}>
-
+
{name}
);
diff --git a/extensions/emoji/js/src/forum/cdn.js b/extensions/emoji/js/src/forum/cdn.js
new file mode 100644
index 000000000..bba87b58a
--- /dev/null
+++ b/extensions/emoji/js/src/forum/cdn.js
@@ -0,0 +1,5 @@
+import twemoji from 'twemoji';
+
+export const version = /([0-9]+).[0-9]+.[0-9]+/g.exec(twemoji.base)[1];
+
+export default `https://cdn.jsdelivr.net/gh/twitter/twemoji@${version}/assets/`;
diff --git a/extensions/emoji/js/src/forum/renderEmoji.js b/extensions/emoji/js/src/forum/renderEmoji.js
index 378d7cc1c..84cdf28af 100644
--- a/extensions/emoji/js/src/forum/renderEmoji.js
+++ b/extensions/emoji/js/src/forum/renderEmoji.js
@@ -5,12 +5,14 @@ import twemoji from 'twemoji';
import { override } from 'flarum/extend';
import Post from 'flarum/models/Post';
+import base from './cdn';
+
export default function renderEmoji() {
override(Post.prototype, 'contentHtml', function(original) {
const contentHtml = original();
if (this.oldContentHtml !== contentHtml) {
- this.emojifiedContentHtml = twemoji.parse(contentHtml);
+ this.emojifiedContentHtml = twemoji.parse(contentHtml, { base });
this.oldContentHtml = contentHtml;
}
@@ -20,6 +22,6 @@ export default function renderEmoji() {
override(s9e.TextFormatter, 'preview', (original, text, element) => {
original(text, element);
- twemoji.parse(element);
+ twemoji.parse(element, { base });
});
}