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

mdast-excerpt

v0.0.10

Published

Build an excerpt from a markdown AST

Downloads

1,120

Readme

mdast-excerpt

Build Status

mdast-excerpt allows you to get an excerpt from a markdown post. The code is "borrowed" from gatsby-transformer-remark, which makes it possible to use excerpt generation in other contexts outside of GatsbyJS. If you want to know how the result looks like, check Using Excerpts at GatsbyJS.

Input is a full markdown AST, output a pruned AST with the desired excerpt length and intact markup.

This plugin becomes useful if the excerpt would stop in the middle of marked up text:

This is a **bold text** -> This is a **bold te...

when truncated by string length. With mdast-excerpt, then markup is truncated correctly:

This is a **bold text** -> This is a bold te...

My personal favourite is using mdast-excerpt in combination with react-markdown (see futher below for usage).

Install

npm install mdast-excerpt

Use

const remark = require("remark")
const html = require("remark-html")
const excerptAst = require("mdast-excerpt")

// fake the plugin
const asExcerpt = options => node => excerptAst(node, options || {})

const input = "abc abc **def ghi**"
remark()
  .use(asExcerpt, { omission: " Read More", pruneLength: 14 })
  .use(html)
  .processSync(input)

Usage with react-markdown

You can use the astPlugins prop to supply the excerptAst transformer:

import React from "react"
import ReactDOM from "react-dom"

import ReactMarkdown from "react-markdown"
import excerptAst from "mdast-excerpt"

const MarkdownExcerpt = ({ source, pruneLength = 14 }) => (
  <ReactMarkdown
    source={source}
    astPlugins={[ast => excerptAst(ast, { pruneLength })]}
  />
)

const App = () => {
  const input = "abc abc **def ghi**"
  return <MarkdownExcerpt source={input} pruneLength={13} />
}

React.ReactDOM.render(<App />, document.getElementById("root"))

API

You can call excerptAst as follows:

excerptAst(tree, { pruneLength: 1, truncate: false, omission:})

The options are

  • pruneLength: max length of the excerpt (default 140)
  • truncate: truncate on exact length (true) or stop at whole words (default false)
  • excerptSeparator: supply an excerpt separator that is used to detect the excerpt. A common choice would be <!-- end -->. If the separator is not found, excerptAst falls back to pruneLength (default undefined).
  • omission: the omission text to add after the excerpt (default )

Contributors