npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fedicomments

v1.0.0

Published

An easy way to include comments from the fediverse in a website

Downloads

26

Readme

FediComments

FediComments is a module for easy integration of Mastodon comments into a website. It is based on previous work by Carl Schwan and Daniel Pecos Martínez.

Installation

The module can be installed via npm:

npm install git+https://github.com/weddige/fedicomments.git

Usage

To get started with FediComments, simply add the following code to your site and replace <your mastodon host>, <your mastodon user> and <the id of the toot> with the appropriate values.

<div id="mastodon-comments-list">
  <button id="mastodon-comments-load">Load comments</button>
</div>
<script type="module" type="text/javascript">
  import FediComments from "fedicomments";

  var fedicomments = new FediComments(
    "<your mastodon host>",
    "<your mastodon user>",
    "<the id of the toot>",
    "mastodon-comments-list",
    (toot) => `
<div style="margin-left: calc(2em * ${toot.auxiliary.depth})">
    <div>
        <img src="${toot.account.avatar_static}" height=60 width=60 alt="">
        <a href="${toot.account.url}" rel="nofollow">
            ${toot.account.display_name}</a>
        (<a href="${toot.account.url}" rel="nofollow">
            ${toot.auxiliary.account}</a>)<br>
        <a class="date" href="${toot.url}" rel="nofollow">
            ${toot.created_at.substr(0, 10)}
            ${toot.created_at.substr(11, 8)}</a>
    </div>
    <div>${toot.content}</div>
</div>`
  );
  document.getElementById("mastodon-comments-load").onclick = function () {
    fedicomments.loadComments();
  };
</script>

This will not look pretty, but it will display a button that will load the comments. To adapt the layout of the comments to your website, you need to change the templating string.

The variable toot is a Mastodon status. In addition to all the default values FediComments adds some auxiliary values, that you can use as well:

  • toot.auxiliary.depth is the depth of nested comments
  • toot.auxiliary.account is the full account name (e.g. @[email protected])

An alternative solution is to use Alpine.js. The idea is similar, but the template can be defined as HTML, which allows autocomplete if you are using an IDE.

<div id="mastodon-comments-list" x-data="{ comments: [], commentsLoaded: false }"
    x-on:comments-loaded="comments.push(...fedicomments.comments); commentsLoaded = true">
    <button id="mastodon-comments-load" x-show="!commentsLoaded" x-on:click="fedicomments.loadComments()">
        Load comments
    </button>
    <template x-for="comment in comments">
        <div x-bind:style="`margin-left: calc(2em * ${comment.auxiliary.depth})!important`">
            <div>
                <img x-bind:src="comment.account.avatar_static" height="60" width="60" alt="" />
                <a x-bind:href="comment.account.url" x-html="comment.account.display_name" rel="nofollow"></a>
                <a x-bind:href="comment.account.url" x-text="comment.auxiliary.account" rel="nofollow"></a><br />
                <a x-bind:href="comment.url"
                    x-text="`${comment.created_at.substr(0, 10)} ${comment.created_at.substr(11, 8)}`"
                    rel="nofollow"></a>
            </div>
            <div class="content" x-html="comment.content"></div>
        </div>
    </template>
</div>
<script type="module" type="text/javascript">
    import FediComments from "fedicomments";
    import Alpine from "alpinejs";

    window.fedicomments = new FediComments(
        "<your mastodon host>",
        "<your mastodon user>",
        "<the id of the toot>",
        "mastodon-comments-list"
    );

    Alpine.start();
</script>