Forráskód Böngészése

Added anchorme to make chat messages with links clickable

joker-x 3 éve
szülő
commit
7692a02ab7
3 módosított fájl, 27 hozzáadás és 4 törlés
  1. 20 4
      app/index.js
  2. 6 0
      package-lock.json
  3. 1 0
      package.json

+ 20 - 4
app/index.js

@@ -9,6 +9,7 @@ import audioContext from 'audio-context'
 import ko from 'knockout'
 import _dompurify from 'dompurify'
 import keyboardjs from 'keyboardjs'
+import anchorme from 'anchorme'
 
 import { ContinuousVoiceHandler, PushToTalkVoiceHandler, VADVoiceHandler, initVoice } from './voice'
 import {initialize as localizationInitialize, translate} from './loc';
@@ -17,10 +18,25 @@ const dompurify = _dompurify(window)
 
 function sanitize (html) {
   return dompurify.sanitize(html, {
-    ALLOWED_TAGS: ['br', 'b', 'i', 'u', 'a', 'span', 'p']
+    ALLOWED_TAGS: ['br', 'b', 'i', 'u', 'a', 'span', 'p', 'img', 'center']
   })
 }
 
+const anchormeOptions = {
+  // force target _blank attribute
+  attributes: {
+    target: "_blank"
+  },
+  // force https protocol except email
+  protocol: function(s) {
+    if (anchorme.validate.email(s)) {
+      return "mailto:";
+    } else {
+      return "https://";
+    }
+  }
+}
+
 function openContextMenu (event, contextMenu, target) {
   contextMenu.posX(event.clientX)
   contextMenu.posY(event.clientY)
@@ -405,7 +421,7 @@ class GlobalBindings {
             type: 'chat-message',
             user: sender.__ui,
             channel: channels.length > 0,
-            message: sanitize(message)
+            message: anchorme({input: sanitize(message), options: anchormeOptions})
           })
         })
 
@@ -779,13 +795,13 @@ class GlobalBindings {
         if (target.users) { // Channel
           this.log.push({
             type: 'chat-message-self',
-            message: sanitize(message),
+            message: anchorme({input: sanitize(message), options: anchormeOptions}),
             channel: target
           })
         } else { // User
           this.log.push({
             type: 'chat-message-self',
-            message: sanitize(message),
+            message: anchorme({input: sanitize(message), options: anchormeOptions}),
             user: target
           })
         }

+ 6 - 0
package-lock.json

@@ -1181,6 +1181,12 @@
         "audio-frequency-to-index": "^2.0.0"
       }
     },
+    "anchorme": {
+      "version": "2.1.2",
+      "resolved": "https://registry.npmjs.org/anchorme/-/anchorme-2.1.2.tgz",
+      "integrity": "sha512-2iPY3kxDDZvtRzauqKDb4v7a5sTF4GZ+esQTY8nGYvmhAtGTeFPMn4cRnvyWS1qmtPTP0Mv8hyLOp9l3ZzWMKg==",
+      "dev": true
+    },
     "ansi-regex": {
       "version": "2.1.1",
       "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz",

+ 1 - 0
package.json

@@ -21,6 +21,7 @@
     "@babel/plugin-transform-runtime": "^7.9.0",
     "@babel/preset-env": "^7.9.0",
     "@babel/runtime": "^7.9.2",
+    "anchorme": "^2.1.2",
     "audio-buffer-utils": "^5.1.2",
     "audio-context": "^1.0.3",
     "babel-loader": "^8.1.0",