From 09a2ccb01aef5fbdc2dc7da7a1a0df0e9195ead4 Mon Sep 17 00:00:00 2001 From: Joe McGrath Date: Mon, 27 Dec 2021 11:13:40 +0900 Subject: [PATCH] chore(regex config): organize regex config and use primitive types in interface --- .../EmbeddedLinkContent.vue | 20 +++++++------------ config.ts | 8 ++++++++ store/ui/types.ts | 6 +++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/components/ui/Chat/Embeds/EmbeddedLinkContent/EmbeddedLinkContent.vue b/components/ui/Chat/Embeds/EmbeddedLinkContent/EmbeddedLinkContent.vue index 703c11b5cc..e629764696 100644 --- a/components/ui/Chat/Embeds/EmbeddedLinkContent/EmbeddedLinkContent.vue +++ b/components/ui/Chat/Embeds/EmbeddedLinkContent/EmbeddedLinkContent.vue @@ -33,9 +33,7 @@ export default Vue.extend({ */ parseEmbeddableVideoContentFromText(messagetext: string) { // parse incoming text for links - const arrayOfLinks = messagetext.match( - /(\b(https?):\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/gi, - ) + const arrayOfLinks = messagetext.match(this.$Config.regex.link) // this.settings.embeddedLinks is the global set in the networks panel. Without this, it just shows up as a normal link if (arrayOfLinks && this.settings.embeddedLinks === true) { @@ -44,8 +42,8 @@ export default Vue.extend({ Parsing Youtube links, if we need to modify the regex do it in here. */ if ( - link.match(/^https?:\/\/([a-z0-9-]+[.])*youtube.com?/g) || - link.match(/^https?:\/\/([a-z0-9-]+[.])*youtu.be?/g) + link.match(this.$Config.regex.youtube) || + link.match(this.$Config.regex.youtubeShort) ) { let youtubeOutSource: string = '' if (link.includes('youtube')) { @@ -68,7 +66,7 @@ export default Vue.extend({ /* Parsing Vimeo links, if we need to modify the regex do it in here. Right now it parses regular links and links to videos that are in collections */ - if (link.match(/^https?:\/\/([a-z0-9-]+[.])*vimeo.com?/g)) { + if (link.match(this.$Config.regex.vimeo)) { // vimeo makes their video id potentially available in several different places in the url const videoID: string = link .split('/') @@ -83,7 +81,7 @@ export default Vue.extend({ } if ( - link.match(/^https?:\/\/([a-z0-9-]+[.])*facebook.com?/g) && + link.match(this.$Config.regex.facebook) && link.includes('videos') ) { // hacky workaround to make facebook embed more mobile responsive. @@ -102,7 +100,7 @@ export default Vue.extend({ } } - if (link.match(/^https?:\/\/([a-z0-9-]+[.])twitch[.]tv\/?/g)) { + if (link.match(this.$Config.regex.twitch)) { let videoId: string = '' // check to see if it's an individual video if (link.includes('/videos/')) { @@ -126,11 +124,7 @@ export default Vue.extend({ } } - if ( - link.match( - /^https?:\/\/([a-z0-9-]+[.])spotify[.]com\/(playlist|embed)?/g, - ) - ) { + if (link.match(this.$Config.regex.spotify)) { // get type and id // https://open.spotify.com/playlist/46ffmNKBTEakwz0t625bbC?si=e878040ce04c460f => playlist/46ffmNKBTEakwz0t625bbC // https://open.spotify.com/track/3s2RFp5hU6jEvAmfZrnrAi?si=a9bf555ede314a19 => track/3s2RFp5hU6jEvAmfZrnrAi diff --git a/config.ts b/config.ts index b456881e45..93ab6bf00c 100644 --- a/config.ts +++ b/config.ts @@ -105,6 +105,14 @@ export const Config = { isEmoji: /\w*[{Emoji_Presentation}\u200d]+/gu, // Regex to wrap emoji's in spans. Note: Doesn't yet support emoji modifiers emojiWrapper: /[\p{Emoji_Presentation}\u200d]+/gu, + // check for link + link: /(\b(https?):\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/gi, + youtube: /^https?:\/\/([a-z0-9-]+[.])*youtube.com?/g, + youtubeShort: /^https?:\/\/([a-z0-9-]+[.])*youtu.be?/g, + vimeo: /^https?:\/\/([a-z0-9-]+[.])*vimeo.com?/g, + facebook: /^https?:\/\/([a-z0-9-]+[.])*facebook.com?/g, + twitch: /^https?:\/\/([a-z0-9-]+[.])twitch[.]tv\/?/g, + spotify: /^https?:\/\/([a-z0-9-]+[.])spotify[.]com\/(playlist|embed)?/g, }, webrtc: { constraints: { diff --git a/store/ui/types.ts b/store/ui/types.ts index 6c7e4f5d2e..4dc4300a61 100644 --- a/store/ui/types.ts +++ b/store/ui/types.ts @@ -55,14 +55,14 @@ export interface EnhancerInfo { } export interface EmojiUsage { - code: String + code: string count: number - content: String + content: string } export interface RecentGlyph { pack: Glyph - url: String + url: string count: number }