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

@halkeye/gatsby-transformer-rehype

v1.1.0

Published

Gatsby transformer plugin for HTML using the Rehype library and ecosystem

Downloads

817

Readme

gatsby-transformer-rehype

Released under MIT license. gatsby-transformer-rehype npm package version. PRs welcome!

This is an HTML to HTML transformer. It parses HTML files and GraphQL HTML nodes using rehype. This package is heavily inspired by gatsby-transformer-remark, the difference being that the content source is HTML instead of Remark.

The general idea of this package is to convert an input HTML fragment into an HAST syntax tree, that is put into the HtmlAst object. HtmlAst is passed down to all plugins provided in the options. Plugins are allowed to mutate HtmlAst and thereby provide requested transformations on the original HTML. Finally gatsby-transformer-rehype parses the HtmlAst back to regular HTML.

This plugin also creates a tableOfContents field. The table of contents is generated by analyzing all headlines from level one to six withing the given HTML blob.

Install

yarn add gatsby-transformer-rehype

How to use

// In your gatsby-config.js
plugins: [
  {
    resolve: `gatsby-transformer-rehype`,
    options: {
      // Condition for selecting an existing GrapghQL node (optional)
      // If not set, the transformer operates on file nodes.
      filter: node => node.internal.type === `GhostPost`,
      // Only needed when using filter (optional, default: node.html)
      // Source location of the html to be transformed
      source: node => node.html,
      // Additional fields of the sourced node can be added here (optional)
      // These fields are then available on the htmlNode on `htmlNode.context`
      contextFields: [],
      // Fragment mode (optional, default: true)
      fragment: true,
      // Space mode (optional, default: `html`)
      space: `html`,
      // EmitParseErrors mode (optional, default: false)
      emitParseErrors: false,
      // Verbose mode (optional, default: false)
      verbose: false,
      // Plugins configs (optional but most likely you need one)
      plugins: [],
    },
  },
],

The filter option allows you to transform HTML nodes that come from other GraphQL nodes. In conjunction with the source option, you can also define a different location of your source html. If your HTML is sourced in from files, mediaType must be set to text/html.

The following parts of options are passed down to rehype as options:

  • options.fragment
  • options.space
  • options.emitParseErrors
  • options.verbose

The details of the rehype options above can be found in the rehype-parse documentation.

This transformer is most useful when combined with Gatsby rehype plugins which you can install to customize how HTML is transformed. The following gatsby-rehype-* plugins are currently available:

If you are missing a plugin, consider collaborating with me to contribute your own. Writing plugins for gatsby-transformer-rehype is easy!

Parsing algorithm

Each HTML file or HTML GraphQL node is parsed into a node of type HtmlRehype.

This plugin adds additional fields to the HtmlRehype GraphQL node including html, htmlAst and internal.content. The latter contains the source HTML. The transformed HTML can be found in html. All transformations should be made on htmlAst which is passed to all sub-plugins. Other Gatsby plugins can also add additional fields.

Table of Contents

Data for the table of contents is generated from the html blob by visiting all h1, h2, ..., h6 elements which is used to generate a tableOfContents field. It contains a JSON array with the following structure:

[
    {   id: String, 
        heading: String, 
        items: [],
    }
]

The id field contains the heading id as given in the h1, h2, ..., h6 elements. The heading field contains the heading title.

The array represents a nested tree, where first level headings are in the root array. If a heading has subheadings they will be placed in items. The elements of items replicate the same structure. The maximum level of nesting is 6.

The generateTableOfContents function is passed to all sub-plugins for on-demand creation. This function takes htmlAst as an argument and returns the tableOfContents tree structure.

How to query

A sample GraphQL query to get HtmlRehype nodes:

{
  allHtmlRehype {
    edges {
      node {
        html
        tableOfContents
      }
    }
  }
}

Access from parents

Your source HTML comes either from a file or from some other HTML GraphQL node. Assuming that you sourced your HTML from the GhostPost node, you can reach your transformed HTML also on the children node:

{
  allGhostPost {
    edges {
      node {
        childHtmlRehype {
            html
            tableOfContents
        }
      }
    }
  }
}

This allows for minimal changes in your original GraphQL queries.

Troubleshooting

gatsby-transformer-rehype hooks into the Gatsby onCreateNode method. This method is only called if a new node is created. If nodes were previously generated, they might have been cached and onCreateNode is not called again. During development, or when adding new plugins to the options, don't forget to call

yarn clean

in order to trigger the transformer again. Please always do a yarn clean before reporting a bug to this project.

Contributions

PRs are welcome! Consider contributing to this project if you are missing feature that is also useful for others. Explore this guide, to get some more ideas.

Copyright & License

Copyright (c) 2020 styxlab - Released under the MIT license.