From a977dd1bf53091830062071889ebf3542d85a772 Mon Sep 17 00:00:00 2001 From: Subhadeep Sengupta <92149645+subhadeep-sengupta@users.noreply.github.com> Date: Sat, 28 Sep 2024 11:49:18 +0530 Subject: [PATCH] feat: Added reddit links as embeddable (#8099) feat: #8063 Added reddit links as embeddable Co-authored-by: Aakansha Doshi --- packages/excalidraw/element/embeddable.ts | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/excalidraw/element/embeddable.ts b/packages/excalidraw/element/embeddable.ts index e262c79337..eada31a5be 100644 --- a/packages/excalidraw/element/embeddable.ts +++ b/packages/excalidraw/element/embeddable.ts @@ -45,6 +45,12 @@ const RE_GENERIC_EMBED = const RE_GIPHY = /giphy.com\/(?:clips|embed|gifs)\/[a-zA-Z0-9]*?-?([a-zA-Z0-9]+)(?:[^a-zA-Z0-9]|$)/; +const RE_REDDIT = + /^(?:http(?:s)?:\/\/)?(?:www\.)?reddit\.com\/r\/([a-zA-Z0-9_]+)\/comments\/([a-zA-Z0-9_]+)\/([a-zA-Z0-9_]+)\/?(?:\?[^#\s]*)?(?:#[^\s]*)?$/; + +const RE_REDDIT_EMBED = + /^ { @@ -218,6 +226,24 @@ export const getEmbedLink = ( return ret; } + if (RE_REDDIT.test(link)) { + const [, page, postId, title] = link.match(RE_REDDIT)!; + const safeURL = sanitizeHTMLAttribute( + `https://reddit.com/r/${page}/comments/${postId}/${title}`, + ); + const ret: IframeDataWithSandbox = { + type: "document", + srcdoc: (theme: string) => + createSrcDoc( + `

`, + ), + intrinsicSize: { w: 480, h: 480 }, + sandbox: { allowSameOrigin }, + }; + embeddedLinkCache.set(originalLink, ret); + return ret; + } + if (RE_GH_GIST.test(link)) { const [, user, gistId] = link.match(RE_GH_GIST)!; const safeURL = sanitizeHTMLAttribute( @@ -361,6 +387,11 @@ export const maybeParseEmbedSrc = (str: string): string => { return twitterMatch[1]; } + const redditMatch = str.match(RE_REDDIT_EMBED); + if (redditMatch && redditMatch.length === 2) { + return redditMatch[1]; + } + const gistMatch = str.match(RE_GH_GIST_EMBED); if (gistMatch && gistMatch.length === 2) { return gistMatch[1];