mirror of
https://github.com/excalidraw/excalidraw.git
synced 2026-11-30 17:11:01 +03:00
feat(editor): support Instagram embeds (#12373)
* feat(editor): support Instagram embeds Instagram post and reel links (/p/, /reel/, /reels/, /tv/, also username-prefixed) embed via instagram.com/<type>/<code>/embed/. Uncaptioned so the height only depends on the media; 400x720 fits portrait posts and reels. Pasting Instagram's embed code works too: its loader <script> sits on a separate line, which made the paste fall back to text. Claude Opus 5.5 (1M context), reasoning: xhigh * fix(editor): match loose script end tags in embed code paste Addresses CodeQL "Bad HTML filtering regexp". Claude Opus 5.5 (1M context), reasoning: xhigh
This commit is contained in:
1 parent
d3a9c2a195
commit
a3fdf471ee
4 files changed
+82
-1
No files matched your search
@@ -57,6 +57,11 @@ const RE_REDDIT =
|
||||
const RE_REDDIT_EMBED =
|
||||
/^<blockquote[\s\S]*?\shref=["'](https?:\/\/(?:www\.)?reddit\.com\/[^"']*)/i;
|
||||
|
||||
// share links can be prefixed with the author's username
|
||||
// (instagram.com/<username>/p/<shortcode>)
|
||||
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 =
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -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 <script>
|
||||
// on a separate line, which isn't an embed by itself
|
||||
.filter(
|
||||
(s) =>
|
||||
!/^<script\b[^>]*>\s*<\/script\b[^>]*>\s*$/i.test(s) ||
|
||||
maybeParseEmbedSrc(s) !== s,
|
||||
);
|
||||
const embbeddableUrls = nonEmptyLines
|
||||
.map((str) => maybeParseEmbedSrc(str))
|
||||
.filter(
|
||||
|
||||
@@ -868,6 +868,21 @@ describe("copy and cut events", () => {
|
||||
});
|
||||
|
||||
describe("paste routing", () => {
|
||||
it("should paste embed code with its loader <script> on a separate line as an embeddable", async () => {
|
||||
pasteWithCtrlCmdV(
|
||||
`<blockquote class="instagram-media" data-instgrm-permalink="https://www.instagram.com/p/BsOGulcndj-/?utm_source=ig_embed&utm_campaign=loading" data-instgrm-version="14"><div style="padding:16px;"> <a href="https://www.instagram.com/p/BsOGulcndj-/?utm_source=ig_embed&utm_campaign=loading" target="_blank">View this post on Instagram</a></div></blockquote>
|
||||
<script async src="//www.instagram.com/embed.js"></script>`,
|
||||
);
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user