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

npf2html

v1.1.6

Published

Converts Tumblr's Neue Post Format to plain HTML

Downloads

255

Readme

npf2html

A simple zero-dependency package for converting Tumblr's Neue Post Format to standard HTML.

This is recommended for use along with tumblr.js, which can interact with the Tumblr API to get the NPF for Tumblr posts.

API

API Docs

This module exports a primary entrypoint function, npf2html, which takes a list of content blocks as provided by the Tumblr API's post.content and returns it as HTML. It's also recommended you pass in post.layout and post.asking_avatar as options to accurately reproduce the structure of the post.

import npf2html from 'npf2html';
// or for CJS:
// const npf2html = require('npf2html');

import * as tumblr from "tumblr.js";

const client = new tumblr.Client({
  consumer_key: "<your key goes here>",
});

const response = await client.blogPosts("the-lonelyshepherd", {
  id: "751572408377507840",
  npf: true
});

const post = response.posts[0];
console.log(npf2html(post.content, {
  layout: post.layout,
  askingAvatar: post.asking_avatar,
}));

Output

This package's goal is not to produce the same HTML as the Tumblr website itself. Instead, it aims to produce reasonably clean, semantic, easy-to-style HTML while following the general presentation guidelines for Tumblr API clients.

In many situations, the output will include npf-... classes to make it easier to style different blocks. However, in cases where the meaning of a given element is always clear from the structure of the HTML, classes may be omitted for cleaner output.

Various components may be omitted, depending on what's sent down by the server.

Audio Block

Most audio blocks look like:

<figure class="npf-block-audio">
  <audio controls src="..."></audio>
  <figcaption>
    <img src="..."> <!-- the "poster" for the audio track -->
    <span class="npf-block-audio-title">...</span> -
    <span class="npf-block-audio-artist">...</span> on
    <span class="npf-block-audio-album">...</span>
    <a class="npf-attribution" href="...">...</a>
  </figcaption>
</figure>

However, audio may also be represented as a plain link:

<figure class="npf-block-audio">
  <a href="...">
    <img src="..."> <!-- the "poster" for the audio track -->
    <span class="npf-block-audio-title">...</span> -
    <span class="npf-block-audio-artist">...</span> on
    <span class="npf-block-audio-album">...</span>
  </a>
  <figcaption>
    <a class="npf-attribution" href="...">...</a>
  </figcaption>
</figure>

Or as an iframe embed:

<figure class="npf-block-audio">
  <iframe src="...."></iframe>
  <figcaption>
    <a class="npf-attribution" href="...">...</a>
  </figcaption>
</figure>

Image Block

Image blocks look like:

<figure class="npf-block-image">
  <a href="...">
    <img src="...">
  </a>
  <figcaption>
    <span class="npf-block-image-caption">...</span>
    <a class="npf-attribution" href="...">blogname</a>
  </figcaption>
</figure>

Link Block

Link blocks look like:

<a class="npf-block-link" href="...">
  <img src="..."> <!-- the "poster" for the link -->
  <h2>...</h2> <!-- the link title, or URL if title is unavailable -->
  <p class="npf-block-link-site">...</p>
  <p class="npf-block-link-author">...</p>
  <p class="npf-block-link-description">...</p>
</a>

Paywall Block

Paywall blocks look like:

<a class="npf-block-paywall ..." href="...">
  <h2>...</h2> <!-- paywall title -->
  <p>...</p> <!-- paywall description or label -->
</a>

There are three types of paywall:

  • An unpaid paywall has the class npf-block-paywall-cta, and has both a title and description.

  • A disabled paywall has the class npf-block-paywall-disabled, and has both a title and description.

  • A paywall for which the viewer has paid has the class npf-block-paywall-divider, and has only text, no title. It may also have a --npf-paywall-color CSS variable set with the divider color.

Poll Block

Poll blocks look like:

<div class="npf-block-poll">
  <h2>...</h2> <!-- poll question -->
  <ul>
    <li>...</li> <!-- poll option -->
    ...
  </ul>
</div>

Polls can't be interacted with outside the Tumblr web interface, so this doesn't include any interactive elements.

Text Block

Text blocks are rendered either as <h1>s, <h2>s, <blockquote>s, <ul>s, <li>s, or <p>s. The latter may have one of the following classes:

  • npf-block-text-quirky indicates text that's rendered in a large cursive font.

  • npf-block-text-quote indicates text that's rendered in a large serif font.

  • npf-block-text-chat indicates text that's rendered in a monospace font.

Text blocks also support the following inline formatting:

  • Plain <strong>, <em>, <small>, <s>, and <a> tags.

  • <a class="npf-inline-mention"> tags, which indicate mentions of other blogs.

  • <span style="color: ..."> for colored text.

Note: Tumblr assumes all text is rendered with whitespace preserved but text wrapped, equivalent to white-space: pre-wrap in CSS. If you don't use this style, it's likely that some posts won't render as the authors intended.

Video Block

Most video blocks look like:

<figure class="npf-block-video">
  <video src="..." poster="..."></video>
  <figcaption>
    <a class="npf-attribution" href="...">...</a>
  </figcaption>
</figure>

However, video may also be represented as a plain link:

<figure class="npf-block-video">
  <a href="...">...</a>
  <figcaption>
    <a class="npf-attribution" href="...">...</a>
  </figcaption>
</figure>

Or as an iframe embed:

<figure class="npf-block-video">
  <iframe src="...."></iframe>
  <figcaption>
    <a class="npf-attribution" href="...">...</a>
  </figcaption>
</figure>

Attribution

Audio, video, and image blocks can all have attributions attached which indicate where they were sourced. These always appear in <figcaption>s beneath the media itself. They're always wrapped in <a class="npf-attribution">, which can have the following additional classes:

  • npf-attribution-post indicates that the media came from a specific Tumblr post. The link's text is the blog's name.

  • npf-attribution-blog indicates that the media came from a Tumblr blog, but not a specific post. The link's text is the blog's name.

  • npf-attribution-link indicates that the media came from an arbitrary URL. The link text is the URL itself.

  • npf-attribution-app indicates that the media came from a third-party app. The link may contain an image representing the app's logo, as well as a text description from the application.

Layouts

There are a number of layouts that can wrap multiple content blocks.

Ask Layout

An ask looks like:

<div class="npf-layout-ask">
  <a href="..."><img></a> <!-- asker avatar -->
  <figure>
    <figcaption>
      <a href="..."><strong>...</strong> asked:</a>
    </figcaption>
    ... <!-- content blocks -->
  </figure>
</div>

An anonymous ask looks like:

<div class="npf-layout-ask">
  <a><img></a> <!-- standard anonymous avatar -->
  <figure>
    <figcaption>
      <strong>Anonyous</strong> asked:
    </figcaption>
    ... <!-- content blocks -->
  </figure>
</div>

Note that the avatar still has an <a> tag despite not having an href, to make the styling more consistent with non-anonymous asks.

Row Layout

A row with multiple images looks like:

<div class="npf-layout-row">
  ... <!-- content blocks -->
</div>

Rows are guaranteed to contain only image blocks. They may have the additional class npf-layout-row-carousel if they're expected to display in carousel display mode.

Truncate Layout

A truncation, also known as a "read more", looks like:

<details class="npf-layout-truncate">
  <summary>Keep reading</summary>
  ... <!-- content blocks -->
</details>