Skip to content

Commit

Permalink
fix: update facebook embed detection to handle facebook videos
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-evans committed Aug 27, 2024
1 parent 9aa9be4 commit 33fdaf1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
36 changes: 15 additions & 21 deletions packages/@atjson/offset-annotations/src/utils/social-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(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,
};
Expand Down
6 changes: 3 additions & 3 deletions packages/@atjson/source-url/test/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ describe("url-source", () => {
selfClosing: false,
type: "facebook-embed",
attributes: {
url: "https://www.facebook.com/Vogue/posts/10156453076157279",
url: text.replace(/\?.*/, ""),
},
},
],
Expand All @@ -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({
Expand All @@ -376,7 +376,7 @@ describe("url-source", () => {
selfClosing: false,
type: "facebook-embed",
attributes: {
url: "https://www.facebook.com/Vogue/posts/258591818132754",
url: text.replace(/\?.*/, ""),
},
},
],
Expand Down

0 comments on commit 33fdaf1

Please sign in to comment.