An open API service indexing awesome lists of open source software.

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

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


Discuss on Mastodon

```

Example using a custom template:

```html


Discuss on Mastodon

```

Example using a more complex custom template:

```html


Reposts


Replies


Favourites




View original post from avatar
on


Discuss on Mastodon

```

## 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 `

`:

```html


Reposts


Replies


Favourites


```

You can also use different templates on the same page by using the `template` attribute to target `` elements with a specific `id`:

```html


Discuss on Mastodon

```

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
avatar
on




```

_Note that for `` and `` elements the value won't be applied to it's content if the string being returned starts with `http` and instead will be applied to the `href` and `src` attributes respectively._

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)