From 11c5dc0cf6e53897f3757c4a8de18400961798dc Mon Sep 17 00:00:00 2001 From: Alejandro Gomez Date: Fri, 6 Jan 2023 21:38:51 +0100 Subject: [PATCH] refactor youtube link handling --- src/Const.js | 7 +++- src/Text.js | 90 ++++++++++++++++++++++++-------------------- src/element/Note.css | 6 ++- src/pages/Login.js | 1 - 4 files changed, 61 insertions(+), 43 deletions(-) diff --git a/src/Const.js b/src/Const.js index cc7c7ed56..13ba72ee5 100644 --- a/src/Const.js +++ b/src/Const.js @@ -56,4 +56,9 @@ export const MentionRegex = /(#\[\d+\])/gi; /** * Simple lightning invoice regex */ -export const InvoiceRegex = /(lnbc\w+)/i; \ No newline at end of file +export const InvoiceRegex = /(lnbc\w+)/i; + +/** + * YouTube URL regex + */ +export const YoutubeUrlRegex = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/ diff --git a/src/Text.js b/src/Text.js index 8107d3d64..2dc7723a9 100644 --- a/src/Text.js +++ b/src/Text.js @@ -1,53 +1,63 @@ import { Link } from "react-router-dom"; import Invoice from "./element/Invoice"; -import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex } from "./Const"; +import { UrlRegex, FileExtensionRegex, MentionRegex, InvoiceRegex, YoutubeUrlRegex } from "./Const"; import { eventLink, profileLink } from "./Util"; +function transformHttpLink(a) { + try { + const url = new URL(a); + const vParam = url.searchParams.get('v') + const youtubeId = YoutubeUrlRegex.test(a) && RegExp.$1 + const extension = FileExtensionRegex.test(url.pathname.toLowerCase()) && RegExp.$1 + if (extension) { + switch (extension) { + case "gif": + case "jpg": + case "jpeg": + case "png": + case "bmp": + case "webp": { + return ; + } + case "mp4": + case "mov": + case "mkv": + case "avi": + case "m4v": { + return