Skip to content

Add Mastodon support#140

Merged
delucis merged 42 commits intodelucis:mainfrom
HiDeoo:hd-feat-mastodon
Jan 5, 2026
Merged

Add Mastodon support#140
delucis merged 42 commits intodelucis:mainfrom
HiDeoo:hd-feat-mastodon

Conversation

@HiDeoo
Copy link
Copy Markdown
Contributor

@HiDeoo HiDeoo commented Aug 16, 2024

Description

This PR adds support for embedding posts from Mastodon.

The content is fetched using the Mastodon API as discussed due to the fact that the Mastodon oEmbed API only returns an iframe and a script tag.

It currently supports the following feature:

  • Rendering toots from Mastodon
  • Rendering toots preview cards with and without images
  • Rendering image, video, gifv and audio media attachments
  • Rendering custom emojis in a toot or in a username
  • Rendering RTL toots

The PR is a draft as it still needs some work to be done and some questions to be answered:

  • Probably tweak the UI a bit as it's mostly a basic copy of the Twitter implementation so far
    • Maybe adding some sort of max width to cards, not sure how it's usually done or if it should be up to users.
  • Figure out if I forgot some features as I'm not a Mastodon user at all
  • Figure out if we want to support loading Mastodon's JavaScript like Twitter
    • This would not be as easy as with Twitter considering Mastodon's JavaScript explicitly expects an iframe with the mastodon-embed class
  • Add changesets

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Aug 16, 2024

🦋 Changeset detected

Latest commit: 05c5288

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@astro-community/astro-embed-integration Minor
@astro-community/astro-embed-mastodon Minor
astro-embed Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@HiDeoo HiDeoo marked this pull request as draft August 16, 2024 14:27
@netlify
Copy link
Copy Markdown

netlify Bot commented Aug 17, 2024

Deploy Preview for astro-embed ready!

Name Link
🔨 Latest commit 94e3d69
🔍 Latest deploy log https://app.netlify.com/sites/astro-embed/deploys/66cc8aba779fed00070ee43d
😎 Deploy Preview https://deploy-preview-140--astro-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Copy Markdown
Owner

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HiDeoo! Still didn’t have time for a full review, so thought I’d at least post my notes from last week before I forget about them.

// Thanks to eleventy-plugin-embed-mastodon
// https://github.com/inframanufaktur/eleventy-plugin-embed-mastodon/blob/f8c1687abf1f88a6351e53e38f91aff0c95a5cb2/.eleventy.js#L46-L47
const urlPattern =
/(?:https:\/\/)?([\w\d-]*?.?[\w\d-]*.[a-z]*\/@[\w\d_]*(?:@[\w\d]*?.?[\w\d]*.[a-z]*)?\/)(\d*)/;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering about how this would work given there are theoretically unlimited Mastodon domain names.

Feels really tricky. For example, https://www.youtube.com/@astrodotbuild/ matches this regular expression, but is not a toot. And I guess there are likely other sites adopting this @username style of URL.

Do you know if there’s a reason for the \d portion at the end here to be optional? I’d guess \d+ instead of \d* would at least mean you get slightly less false positives.

(None of this is relevant to the component version, but it is in the integration usage where in theory this pattern might “steal” a match from something else.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I updated the regex to enforce the last digit altho like you said, it's a bit tricky in this case. May need to think about it more but the only ideas that are coming to mind right now are not having Toot enabled as a service by default with an explanation in the docs or just not supporting Mastodon in the integration. Both of which are not ideal but the fact that the domain can be anything is a bit complex to handle I guess in this case.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to try leaving it enabled and see if it causes issues for people using the integration. The /@handle/<numeric-id> pattern is not so common either, so maybe it will be OK. If not, we could potentially explore supporting something like an “allowed domains” array for Mastodon hosts where people opt in to the origins they want to support parsing as Mastodon post URLs.

Comment thread packages/astro-embed-mastodon/src/MastodonPost.astro
Comment thread packages/astro-embed-mastodon/Toot.astro Outdated
HiDeoo and others added 5 commits August 26, 2024 15:21
@netlify
Copy link
Copy Markdown

netlify Bot commented Jan 3, 2026

Deploy Preview for astro-embed ready!

Name Link
🔨 Latest commit 05c5288
🔍 Latest deploy log https://app.netlify.com/projects/astro-embed/deploys/695c3ced6eee9000084fe097
😎 Deploy Preview https://deploy-preview-140--astro-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@delucis delucis marked this pull request as ready for review January 5, 2026 22:21
Copy link
Copy Markdown
Owner

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for kicking this off so many many moons ago and apologies it took so long to resolve.

I rebuilt the component to look very close to what Mastodon itself offers when using its <iframe> embed, updating it to support multiple media items and quote posts, which are new since this PR was opened.

I decided to take the same approach as the <BaselineStatus> component, which uses declarative <template shadowrootmode="open"> to isolate the component from the site styles, exposing styling hooks via ::part() and custom properties. This should make it fairly robust while sacrificing some flexibility. I guess most people want something that does the basics quickly, so hopefully it’s a reasonable compromise.

One other drawback is that declarative shadow DOM isn’t supported by the testing infra, so we’ll need some follow-up work after this PR to bring back testing. I tested extensively with the demo project though, so should be OK.

Figure out if we want to support loading Mastodon's JavaScript like Twitter

I think we don’t need to. Twitter is a special case because it offers such limited access to data, so we can only do a bare minimum with the unstructured HTML they return. In this case, we can offer a pretty rich experience thanks to the structured data the API returns. I’ve matched the official embed very closely, only skipping things like comment/favourite/repost counts that may be more time sensitiv, and choosing to show the date as a day rather than a local timestamp.

So with that, I’ll approve and get this released for people to try out. Thanks again @HiDeoo! 🌟

@delucis delucis merged commit 30121f2 into delucis:main Jan 5, 2026
3 checks passed
@github-actions github-actions Bot mentioned this pull request Jan 5, 2026
@HiDeoo
Copy link
Copy Markdown
Contributor Author

HiDeoo commented Jan 6, 2026

Wow, I totally forgot about this PR until now.

It looks like you had quite a lot of work to do to get this shipped, sorry about that, you should have pinged me. I'll take a look later at your changes, but based on your description it sounds great 👏

Thanks a lot for picking this up again 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Mastodon embed support/demo

2 participants