diff --git a/packages/element/src/embeddable.ts b/packages/element/src/embeddable.ts index dceda3281e..013c76f1cf 100644 --- a/packages/element/src/embeddable.ts +++ b/packages/element/src/embeddable.ts @@ -57,6 +57,11 @@ const RE_REDDIT = const RE_REDDIT_EMBED = /^
/p/) +const RE_INSTAGRAM = + /^(?:https?:\/\/)?(?:www\.)?instagram\.com\/(?:[\w.]+\/)?(p|reels?|tv)\/([\w-]+)/; + const parseYouTubeLikeTimestamp = (url: string): number => { let timeParam: string | null | undefined; @@ -148,6 +153,7 @@ const ALLOWED_DOMAINS = new Set([ "giphy.com", "reddit.com", "forms.microsoft.com", + "instagram.com", ]); const ALLOW_SAME_ORIGIN = new Set([ @@ -163,6 +169,7 @@ const ALLOW_SAME_ORIGIN = new Set([ "stackblitz.com", "reddit.com", "forms.microsoft.com", + "instagram.com", ]); export const createSrcDoc = (body: string) => { @@ -300,6 +307,30 @@ export const getEmbedLink = ( }; } + const instagramLink = link.match(RE_INSTAGRAM); + if (instagramLink) { + const [, postType, shortcode] = instagramLink; + // uncaptioned so the height is predictable (only depends on the media) + // — we can't auto-resize to fit a long caption + link = `https://www.instagram.com/${ + postType === "reels" ? "reel" : postType + }/${shortcode}/embed/`; + // fits portrait (4:5) posts and reels + aspectRatio = { w: 400, h: 720 }; + embeddedLinkCache.set(originalLink, { + link, + intrinsicSize: aspectRatio, + type, + sandbox: { allowSameOrigin }, + }); + return { + link, + intrinsicSize: aspectRatio, + type, + sandbox: { allowSameOrigin }, + }; + } + const valLink = link.match(RE_VALTOWN); if (valLink) { link = diff --git a/packages/element/tests/embeddable.test.ts b/packages/element/tests/embeddable.test.ts index 77a16045ea..e9cc798f09 100644 --- a/packages/element/tests/embeddable.test.ts +++ b/packages/element/tests/embeddable.test.ts @@ -243,3 +243,31 @@ describe("Google Drive video embedding", () => { ).toBe(true); }); }); + +describe("Instagram embedding", () => { + it.each([ + { + url: "https://www.instagram.com/p/BsOGulcndj-/", + expectedLink: "https://www.instagram.com/p/BsOGulcndj-/embed/", + }, + { + url: "https://instagram.com/reel/DSu-vbYDDlf/?igsh=MWQ1ZGUxMzBkMA==", + expectedLink: "https://www.instagram.com/reel/DSu-vbYDDlf/embed/", + }, + { + url: "https://www.instagram.com/reels/DSu-vbYDDlf/", + expectedLink: "https://www.instagram.com/reel/DSu-vbYDDlf/embed/", + }, + { + url: "https://www.instagram.com/world_record_egg/p/BsOGulcndj-/", + expectedLink: "https://www.instagram.com/p/BsOGulcndj-/embed/", + }, + ])("should normalize Instagram link: $url", ({ url, expectedLink }) => { + expect(embeddableURLValidator(url, undefined)).toBe(true); + const result = getEmbedLink(url); + expect(result?.type).toBe("generic"); + if (result?.type === "generic") { + expect(result.link).toBe(expectedLink); + } + }); +}); diff --git a/packages/excalidraw/components/App.clipboard.ts b/packages/excalidraw/components/App.clipboard.ts index c494044c4e..f81e3dc846 100644 --- a/packages/excalidraw/components/App.clipboard.ts +++ b/packages/excalidraw/components/App.clipboard.ts @@ -237,7 +237,14 @@ export class AppClipboard { const nonEmptyLines = normalizeEOL(data.text) .split(/\n+/) .map((s) => s.trim()) - .filter(Boolean); + .filter(Boolean) + // some embed codes (e.g. Instagram) put the provider's loader `, + ); + await waitFor(() => { + expect(h.elements).toEqual([ + expect.objectContaining({ + type: "embeddable", + link: "https://www.instagram.com/p/BsOGulcndj-/?utm_source=ig_embed&utm_campaign=loading", + }), + ]); + }); + }); + it("should leave a paste to the host when its onPaste returns false", async () => { unmountComponent(); const onPaste = vi.fn(() => false);