Git Product home page Git Product logo

mastodon-post's Introduction

mastodon-post

A Web Component to display Mastodon posts and their metadata.

Demo | Custom template demo | Further reading

Examples

General usage example:

<script type="module" src="mastodon-post.js"></script>

<mastodon-post>
  <a href="https://mastodon.design/@DavidDarnes/109824258017750161">
    Discuss on Mastodon
  </a>
</mastodon-post>

Example using a custom template:

<script type="module" src="mastodon-post.js"></script>

<template id="mastodon-post-template">
  <blockquote data-key="content"></blockquote>
</template>

<mastodon-post>
  <a href="https://mastodon.design/@DavidDarnes/109824258017750161">
    Discuss on Mastodon
  </a>
</mastodon-post>

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

Installation

You have a few options (choose one of these):

  1. Install via npm: npm install @daviddarnes/mastodon-post
  2. Download the source manually from GitHub into your project.
  3. Skip this step and use the script directly via a 3rd party CDN (not recommended for production use)

Usage

Make sure you include the <script> in your project (choose one of these):

<!-- Host yourself -->
<script type="module" src="mastodon-post.js"></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://www.unpkg.com/@daviddarnes/[email protected]/mastodon-post.js"
></script>
<!-- 3rd party CDN, not recommended for production use -->
<script
  type="module"
  src="https://esm.sh/@daviddarnes/[email protected]"
></script>

Using a custom template

The default template for the component looks like this:

<figure>
  <blockquote data-key="content"></blockquote>
  <figcaption>
    <cite>
      <a data-key="url">
        <span data-key="username"></span>
        @
        <span data-key="hostname"></span>
      </a>
    </cite>
    <dl>
      <dt>Reposts</dt>
      <dd data-key="reblogs_count"></dd>
      <dt>Replies</dt>
      <dd data-key="replies_count"></dd>
      <dt>Favourites</dt>
      <dd data-key="favourites_count"></dd>
    </dl>
  </figcaption>
</figure>

However you can customise the template by using a <template> 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 <dl>:

<template id="mastodon-post-template">
  <dl>
    <dt>Reposts</dt>
    <dd data-key="reblogs_count"></dd>
    <dt>Replies</dt>
    <dd data-key="replies_count"></dd>
    <dt>Favourites</dt>
    <dd data-key="favourites_count"></dd>
  </dl>
</template>

Data is applied using a data-key data attribute. The value of this attribute corresponds to one of the following data points returned by the Mastodon API plus some pieces of data formed within the component itself:

  • content: The post content itself, as a HTML string. E.g. <p>Example content</p>
  • created_at: The time of the post in UTC, e.g. 2023-02-07T15:53:07.042Z
  • edited_at: The time of the post being last edited in UTC, e.g. 2023-02-08T15:53:07.042Z
  • favourites_count: Favourite count
  • hostname: The Mastodon host site, e.g. mastodon.social
  • id: The ID of the post
  • in_reply_to_account_id: The ID of the account being replied to, if it's a reply
  • in_reply_to_id: The ID of the post being replied to, if it's a reply
  • language: The language locale code
  • postId: The post ID
  • reblogs_count: The reblog or boost count
  • replies_count: The replies count
  • sensitive: If the post has been marked as sensitive, boolean
  • spoiler_text: The hidden spoiler text, if applied
  • uri: The post URI
  • url: The original post URL from the anchor in the component
  • username: The username, lifted from the original post URL
  • visibility: The visibility type

For <a> and <img> elements the value won't be applied to it's content if the string being returned starts with http and will be instead be applied to the href and src attributes respectively.

Check out the custom template demo as well as the source code for reference.

Credit

With thanks to the following people:

mastodon-post's People

Contributors

daviddarnes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mastodon-post's Issues

Allow custom templates to access nested data in the returned JSON

Is your feature request related to a problem? Please describe.
When using a custom template you can only access top level data points on the object of data that comes back from the public Mastodon API. This means things like attachments can't be accessed.

Describe the solution you'd like
Allow the use of nested data in data-key data attributes, e.g. data-key="account.display_name"

Additional context
Here's a snippet of code I rustled up which should parse strings into nested key references:

const data = {
  item: {
    nested: {
      key: "value",
      array: ["item 1", "item 2"]
    }
  }
};

const string1 = "item.nested.key";
const string2 = "item.nested.array[1]";

const handleKey = (object, key) => {
  const parsed = parseFloat(key);
  return Number.isNaN(parsed) ? object[key] : object[parsed];
};

const getValue = (string) => {
  let keys = string.split(/\.|\[|\]/g);
  keys = keys.filter((str) => str.length);
  const value = keys.reduce((object, key) => handleKey(object, key), data);
  return value;
};

console.log(getValue(string1), getValue(string2));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.