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

markdown-stream-utils

v1.6.0

Published

Utility functions for processing markdown files using object mode streams.

Downloads

7,314

Readme

markdown-stream-utils

Utility functions for processing markdown files using object mode streams. Used by markdown-styles and by ghost-render.

Changelog

v1.6.0: Updated all dependencies to latest versions.

v1.2.0: Updated highlight.js and other dependencies, thanks @omnibs!

v1.1.0: made highlight apply the syntax highlighting for the specific language if available in highlight.js. Added md.marked and added opts to md.convertMd().

API

Getting started

All of the markdown-stream-utils functions expect to receive objects representing each markdown file. The files should have three properties:

  • path: a path to the original filename
  • stat: fs.stats object
  • contents: the contents of the file as a string

Here's a full example of using markdown-stream-utils, with some helpers from [pipe-iterators](https://github.com/mixu/pipe-iterators):

var pi = require('pipe-iterators'),
    md = require('markdown-stream-utils');

pi.fromArray([ __dirname + '/foo.md', __dirname + '/bar.md' ])
  .pipe(pi.thru.obj(function(file, enc, onDone) {
    var stat = fs.statSync(file);
    if (stat.isFile()) {
      this.push({
        path: file,
        stat: stat,
        contents: fs.readFileSync(file).toString()
      });
    }
    onDone();
  }))
  .pipe(md.parseHeader())
  .pipe(md.parseMd())
  .pipe(md.highlightJS())
  .pipe(md.convertMd())
  .pipe(pi.toArray(function(results) {
    console.log(results);
  }));

parseHeader()

.pipe(md.parseHeader())

Parses header sections in markdown files. For example, given a object with the following content field:

title: Hello world
author: foo
---
# Heading
...

it will augment the existing object with two new fields: title and author with the specified values.

The header section may be written in either JSON or YAML. There must be at least three - characters that separate the header from the rest of the content (on a single line). Headers may also have a beginning delimiter, e.g.:

---
title: Hello world
---
# Heading

The header section will be removed from contents, so that only the markdown content after the --- will be kept in the contents key.

You can customize the contents field as well as the destination of the metadata. To set the contents field name, pass in an options hash with contentsKey. To set the metadata storage key, pass the key name in metadataKey; if this is false (default), the metadata is merged; if it is set then the metadata is stored under a subkey.

parseMd()

pipe(md.parseMd())

Given an object with a contents field, executes marked.lexer() on the contents field. The new value is the lexer tree from marked.

highlight()

pipe(md.highlight())

Iterates over the lexer tree from parseMd, and executes the highlight.js highlighter on each code block.

You can add support for additional languages by passing a custom callback with the signature function(code, lang) {}, which should return either a HTML string containing the highlighted version of the code, or false if you want to run highlight.js on the code block.

Note that you will need a highlight.js CSS style sheet in your final output so that the styling is visible.

annotateMdHeadings()

pipe(md.annotateMdHeadings())

Iterates over the lexer tree from parseMd. Annotates every heading with an id, so that when converted to HTML the headings can be targeted via links. An array all the headings is produced under headings. The value is an array of lexer tokens with an id property.

For example:

# Test
foo

results in the input object being augmented with:

{ headings: [ { id: 'test', text: 'foo', type: 'heading', depth: 1 } ] }

By default, the markdown tokens are read from the contents key on the input object, and written to the headings key.

You can customize the keys used by passing in an options hash. The contentsKey property controls the key from which the lexer tree is read, and the headingsKey controls the key to which the headings are written.

convertMd(opts)

pipe(md.convertMd())

Constructs a new parser using marked default options, overriding with the values from opts where specified (e.g. opts.renderer can be used to override the renderer).

Given an object with a contents field, executes Parser.parse() on the contents field. The new value is the HTMLs from marked.

marked

console.log(md.marked);

A reference to the marked library, in case you need to construct a marked.Renderer for convertMd.