Skip to content

Commit

Permalink
update twitter url matching (x.com) (#161)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
flo-bit and delucis authored Oct 30, 2024
1 parent f7d6d2c commit a024bf2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/proud-waves-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astro-community/astro-embed-twitter': patch
---

Fixes support for newer `x.com` URLs
5 changes: 5 additions & 0 deletions demo/src/pages/twitter.astro
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import Base from '../layouts/Base.astro';
<Component.Tweet
id="https://twitter.com/astrodotbuild/status/1402352777020395521"
/>

<h2>With x.com URLs</h2>
<Component.Tweet
id="https://x.com/astrodotbuild/status/1851286331491578330"
/>
</Base>

<style>
Expand Down
2 changes: 1 addition & 1 deletion packages/astro-embed-twitter/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Thanks to eleventy-plugin-embed-twitter
// https://github.com/gfscott/eleventy-plugin-embed-twitter/blob/main/lib/extractMatch.js
const urlPattern =
/(?=(\s*))\1(?:<a [^>]*?>)??(?=(\s*))\2(?:https?:)??(?:\/\/)??(?:w{3}\.)??(?:twitter\.com)\/([a-zA-Z0-9_]{1,15})?(?:\/(?:status)\/)(\d+)?(?:[^\s<>]*)(?=(\s*))\5(?:<\/a>)??(?=(\s*))\6/;
/(?=(\s*))\1(?:<a [^>]*?>)??(?=(\s*))\2(?:https?:)??(?:\/\/)??(?:w{3}\.)??(?:twitter\.com|x\.com)\/([a-zA-Z0-9_]{1,15})?(?:\/(?:status)\/)(\d+)?(?:[^\s<>]*)(?=(\s*))\5(?:<\/a>)??(?=(\s*))\6/;

/**
* Return a Tweet URL from a URL if it matches the pattern.
Expand Down
13 changes: 13 additions & 0 deletions tests/astro-embed-twitter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,17 @@ test('it does not crash when we have undefined entities', async () => {
screen.getByText('December 7, 2022');
});

test('it works with x.com URLs', async () => {
const screen = await renderScreen(
'./packages/astro-embed-twitter/Tweet.astro',
{ id: 'https://x.com/astrodotbuild/status/1851286331491578330' }
);
screen.getByText(/⚠️ Be sure to check your kid's candy this year! ⚠️/);
screen.getByText(
/Just found a bloated 5.8 MB JavaScript bundle in this Reese's! 😱/
);
screen.getByText(/@astrodotbuild/);
screen.getByText('October 29, 2024');
});

test.run();

0 comments on commit a024bf2

Please sign in to comment.