https://github.com/daviddarnes/mastodon-post
A Web Component to display Mastodon posts and their metadata
https://github.com/daviddarnes/mastodon-post
component components customelement customelements webcomponent webcomponents
Last synced: 7 months ago
JSON representation
A Web Component to display Mastodon posts and their metadata
- Host: GitHub
- URL: https://github.com/daviddarnes/mastodon-post
- Owner: daviddarnes
- License: mit
- Created: 2024-02-18T00:01:44.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-22T09:30:57.000Z (almost 2 years ago)
- Last Synced: 2025-06-13T07:06:03.055Z (8 months ago)
- Topics: component, components, customelement, customelements, webcomponent, webcomponents
- Language: HTML
- Homepage: https://daviddarnes.github.io/mastodon-post/demo.html
- Size: 42 KB
- Stars: 37
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# `mastodon-post`
A Web Component to display Mastodon posts and their metadata.
**[Demo](https://daviddarnes.github.io/mastodon-post/demo.html)** | **[Custom template demo](https://daviddarnes.github.io/mastodon-post/demo-custom-template.html)** | **[Further reading](https://darn.es/mastodon-post-web-component/)**
## Examples
General usage example:
```html
```
Example using a custom template:
```html
```
Example using a more complex custom template:
```html
- Reposts
- Replies
- Favourites
View original post from
on
```
## Features
This Web Component allows you to:
- Turn a regular Mastodon post link into a quoted Mastodon post
- Surface the post metadata alongside the post, e.g. reply count, reblog count, favourite count
- Use a custom template for all instances of the component on the page using a `data-key="name"` data attributes
- Use a custom template for specific instances using the `template` attribute
- Retrieve nested data using the `data-key` attribute and typical JavaScript key referencing, e.g. `data-key="account.display_name"` or `data-key="media_attachments[0]preview_url"`
## Installation
You have a few options (choose one of these):
1. Install via [npm](https://www.npmjs.com/package/@daviddarnes/mastodon-post): `npm install @daviddarnes/mastodon-post`
1. [Download the source manually from GitHub](https://github.com/daviddarnes/mastodon-post/releases) into your project.
1. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)
### Usage
Make sure you include the `` in your project (choose one of these):
```html
<!-- Host yourself -->
<script type="module" src="mastodon-post.js">
```
```html
```
```html
```
### Using a custom template
The default template for the component looks like this:
```html
@
- Reposts
- Replies
- Favourites
```
However you can customise the template by using a `` element with an `id` of `mastodon-post-template`, which will be used for every instance of the component on the page. Here's an example which just exposes the vanity metrics of the Mastodon post as a `
- `:
- Reposts
- Replies
- Favourites
```html
```
You can also use different templates on the same page by using the `template` attribute to target `` elements with a specific `id`:
```html
```
Data is applied using a `data-key` data attribute. The value of this attribute should correspond to a data point within a [Mastodon public status API response](https://docs.joinmastodon.org/methods/statuses/). The official Mastodon documentation has [an example of a status response here](https://docs.joinmastodon.org/methods/statuses/#200-ok-1). The `data-key` attribute also allows you to target nested data using typical JavaScript dot notation:
```html
View original post from
on
```
Check out the [custom template demo](https://daviddarnes.github.io/mastodon-post/demo-custom-template.html) as well as [the source code](https://github.com/daviddarnes/mastodon-post/blob/main/demo-custom-template.html) for reference.
## Credit
With thanks to the following people:
- [Zach Leatherman](https://zachleat.com) for inspiring this [Web Component repo template](https://github.com/daviddarnes/component-template)