diff --git a/packages/@atjson/offset-annotations/src/utils/social-urls.ts b/packages/@atjson/offset-annotations/src/utils/social-urls.ts index 46e01f029..baa8d6663 100644 --- a/packages/@atjson/offset-annotations/src/utils/social-urls.ts +++ b/packages/@atjson/offset-annotations/src/utils/social-urls.ts @@ -185,47 +185,41 @@ function normalizePinterestURL(url: IUrl) { // Facebook URLs // - www.facebook.com/:user/:type/:id // - www.facebook.com/:user/:type/:context-id/:id -function isFacebookPostURL(url: IUrl) { - return ( - url.host === "www.facebook.com" && - (!!url.pathname.match(/^\/[^\/]+\/[^\/]+\/[^\/]+/) || - !!url.pathname.match(/^\/[^\/]+\/[^\/]+\/[^\/]+\/[^\/]+/)) - ); -} - -// Facebook embed urls // - www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2F{user}}%2Fposts%2F{id}" -function isFacebookEmbedURL(url: IUrl) { +// - www.facebook.com/plugins/video.php?href=https%3A%2F%2Fwww.facebook.com%2F{user}}%2Fposts%2F{id}" +function isFacebookURL(url: IUrl) { let href = getSearchParam(url.searchParams, "href"); try { + if (isFacebookPluginURL(url) && href) { + url = new URL(href); + } return ( url.host === "www.facebook.com" && - url.pathname === "/plugins/post.php" && - !!href && - isFacebookPostURL(new URL(href)) + (!!url.pathname.match(/^\/[^\/]+\/[^\/]+\/[^\/]+/) || + !!url.pathname.match(/^\/[^\/]+\/[^\/]+\/[^\/]+\/[^\/]+/)) ); } catch { return false; } } -function isFacebookURL(url: IUrl) { - return isFacebookPostURL(url) || isFacebookEmbedURL(url); +function isFacebookPluginURL(url: IUrl) { + return ( + url.host === "www.facebook.com" && + (url.pathname === "/plugins/post.php" || + url.pathname === "/plugins/video.php") + ); } function normalizeFacebookURL(url: IUrl) { - if (isFacebookEmbedURL(url)) { + if (isFacebookPluginURL(url)) { url = new URL(getSearchParam(url.searchParams, "href") as string); } - let parts = without(url.pathname.split("/"), ""); - let id = parts.pop(); - let username = parts.shift(); - return { attributes: { - url: `https://www.facebook.com/${username}/posts/${id}`, + url: `https://www.facebook.com${url.pathname}`, }, Class: FacebookEmbed, }; diff --git a/packages/@atjson/source-url/test/url.test.ts b/packages/@atjson/source-url/test/url.test.ts index 28e0526fe..826edc69d 100644 --- a/packages/@atjson/source-url/test/url.test.ts +++ b/packages/@atjson/source-url/test/url.test.ts @@ -352,7 +352,7 @@ describe("url-source", () => { selfClosing: false, type: "facebook-embed", attributes: { - url: "https://www.facebook.com/Vogue/posts/10156453076157279", + url: text.replace(/\?.*/, ""), }, }, ], @@ -364,7 +364,7 @@ describe("url-source", () => { describe("videos", () => { test.each([ "https://www.facebook.com/Vogue/videos/vb.42933792278/258591818132754/?type=2&theater", - "https://www.facebook.com/Vogue/posts/258591818132754", + "https://www.facebook.com/Vogue/videos/258591818132754", ])("%s", (text) => { let url = URLSource.fromRaw(text).convertTo(OffsetSource); expect(serialize(url, { withStableIds: true })).toEqual({ @@ -376,7 +376,7 @@ describe("url-source", () => { selfClosing: false, type: "facebook-embed", attributes: { - url: "https://www.facebook.com/Vogue/posts/258591818132754", + url: text.replace(/\?.*/, ""), }, }, ],