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 🙏

© 2025 – Pkg Stats / Ryan Hefner

transform-markdown-mathmode

v0.1.8

Published

Transform LaTeX equations asynchronously in a stream of markdown

Readme

transform-markdown-mathmode

Build Status Dependency Status npm version

Transform LaTeX equations asynchronously in a stream of markdown

Introduction

This module takes a stream of markdown and searches for basic patterns that look like equations, being careful to exclude escaped patterns or code blocks. It doesn't do any rendering itself, but takes transformation functions that may perform some synchronous or asynchronous action and return the result. This makes it easy to render equations, store them somewhere, and insert image tags into the resulting markdown.

Installation

To install, run:

$ npm install transform-markdown-mathmode

Example

This README is generated by passing README.mdtex through the parser. The sample configuration just transforms $...$ into <...> and $$...$$ into <<...>> so it's clear that it's working (FWIW: this line started out with all $'s):

var transformMathmode = require('transform-markdown-mathmode')

process.stdin
  .pipe(
    transformMathmode({
      inline:  function(tex, cb) { cb(  '<' + tex + '>'  ) },
      display: function(tex, cb) { cb( '<<' + tex + '>>' ) }
    })
  )
  .pipe( process.stdout )

// Also, note that equations in code blocks don't get transformed,
// no escaping necessary! Hooray!
//
//   $y = x$
//

This is just invoked with:

$ cat README.mdtex | node example/transform.js > README.md

This transformation isn't particularly interesting, but it means you can perform an arbitrarily complicated asynchronous task each time you encounter an equation.

What it does

As hesitant as I was to parse a stream of markdown (with regexes? Is that even parsing?), this module uses a finite state machine to track whether a couple different types of blocks are open or closed. I suspect it's probably possible (and wayyyy easier) to do this with a couple regexes, but the current complication comes from the need to track open equations across multiple lines of streamed text and the desire to avoid ugly escaping that would make it a total pain in the butt to paste fifty lines of jQuery code into your README. That's really all it's doing through. It's pretty simple, carefully tested, and allows simple escaping for corner cases. Fundamentally though, it's a parser built on regexes, so it's not like a work of art or anything. If there are cases that fail, let me know. If you can do this in five lines of code... I'm not sure I want to know...

Goals:

  • transform equations asynchronously while avoiding a bunch of ugly escaping and other workarounds.
  • learn how to process streams in node.
  • and learn how to write gulp plugins.
  • and avoid typesetting equations in Github READMEs by hand.
  • maybe use it for a static website instead of MathJax. If the baseline positioning is workable. We'll see.

Usage

require('transform-markdown-mathmode')( options )

Create a transform stream that performs the transformation.

  • options: The options hash accepts two options:

    • inline: A function of format function(tex, cb){...} that receives a string of tex (with $ delimiters stripped) and executes a callback containing the transformed inline equation. See above for example.
    • display: A function of format function(tex, cb){...} that receives a string of tex (with $$ delimiters stripped) and executes a callback containing the transformed display equation. See above for example.
  • Returns: The transform stream.

License

(c) 2015 Ricky Reusser. MIT License