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 🙏

© 2026 – Pkg Stats / Ryan Hefner

rehype-extract-article

v1.8.0

Published

Rehype plugin to extract meaningful contents from an HTML document

Downloads

28

Readme

rehype-extract-article

Rehype plugin to extract essential contents from HTML documents, like articles, blogs, ebooks, docs, and wikis.

Install

npm install rehype-extract-article

Use

import { unified } from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeExtractArticle from 'rehype-extract-article'

const html = await fetch('https://github.com/gorango/rehype-extract-article')
const file = { value: html }
const tree = unified().use(rehypeParse).parse(file)
const value = await unified().use(rehypeExtractArticle).run(tree, file)

console.log(value)

Running the above will return sanitized HTML containing only the salient content from the original document:

{
  type: 'root',
  children: [
    { tagName: 'h1', children: [Array], /* ... */ },
    { tagName: 'p', children: [Array], /* ... */ },
    { tagName: 'h2', children: [Array], /* ... */ },
    { tagName: 'pre', children: [Array], /* ... */ },
    { tagName: 'h2', children: [Array], /* ... */ },
    { tagName: 'pre', children: [Array], /* ... */ },
    { tagName: 'p', children: [Array], /* ... */ },
    { tagName: 'pre', children: [Array], /* ... */ },
    /* ... */
  ]
}

Content will always be returned unnested from any unsemantic tags (like divs and sections).

By default, most classes and props get stripped from the original tree. Props with relative URLs get updated with absolute URLs (like src and href). When semantic tags are missing (like <p>s and <figure>s), those nodes will get wrapped.

See the test/ dir for examples and edge-cases, as well as options for hooking into various lifecycles of the plugin.

API

This package exports a single plugin function.

unified().use(rehypeExtractArticle[, options])

The plugin executes a series of traversals to find, sanitize, and format the content. Each stage can be modified using the following options:

options

Configuration (optional).

options.container

To skip the search traversal for finding the container, you can provide your own selector:

  • String selector supported by hast-util-select.
  • Function that receives a hast Node evaluated for truthiness.
.use(rehypeExtractArticle, {
  // container: 'article.post',
  container: (node) => node.tagName === 'article'
      && node.properties?.className?.includes('post'),
})
options.exclude

Exclude specific nodes with one or more selectors containing either:

  • String selector supported by hast-util-select.
  • Function that receives a hast Node evaluated for truthiness.
.use(rehypeExtractArticle, {
  exclude: [
    '.ad-container',
    (node) => node.properties.className?.includes('ad-container'),
  ],
})
options.schema

Schema for hast-util-sanitize, merged with defaults.

If you want to preserve code highlights from the original document, for example:

.use(rehypeExtractArticle, {
  schema: {
    attributes: {
      'pre': ['className'],
      'code': ['className'],
      'span': ['className'],
    },
    tagNames: ['span'],
  },
})
options.url

Join the url string to internal URLs on the page. Applies to nodes with href and src props (like a, img, video).

If rehypeExtractMeta plugin is used before rehypeExtractArticle, the url value from file.data.meta will be used as a fallback. If not, the plugin will try to infer the host from from the meta tags in the tree.

options.target

Add a target prop to all anchor tags. Defaults to "_blank". Setting to null will exclude the prop.

options.rel

Add a rel prop to all anchor tags. Defaults to null.

options.format

A function that executes on each hast Node in the final format stage.

.use(rehypeExtractArticle, {
  format: (node) => {
    if (node.tagName.startsWith('h'))
      node.value = node.value.toUpperCase()
  },
})