transform-markdown-mathmode

Transform LaTeX equations asynchronously in a stream of markdown

transform-markdown-mathmode

Transform LaTeX equations asynchronously in a stream of markdown

Introduction

This module takes a stream of markdownand 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.mdtexthrough 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 machineto 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

Repository

https://github.com/rreusser/transform-markdown-mathmode


上一篇:万万没想到,一个技术方案帮实习生追到了运营妹子!
下一篇:square-parameters

相关推荐

  • 通过正则获取transform偏移的数值

    offsetWidth 获取元素宽offsetLeft 获取元素定位偏移值当元素使用CSS3中的transform(translateX:100px)偏移时,无法用offsetLeft获取偏移值解决办...

    1 个月前
  • 读marked.js源码-markdown是怎么变成html的

    很好奇markdown是怎么变成html的,偶然间看到 github(https://github.com/markedjs/marked)上的这一篇星星✨很高的源码,就来研究研究 下载打开在lib文...

    2 年前
  • 自己撸个 vue markdown loader

    最近,当我把 vueloader 升级到 v15 后发现,自己项目中所使用的一个 vuemarkdownloader 因为兼容问题而没法用了,正当我一筹莫展的时候,无意间看到 vuepress 中使用...

    2 年前
  • 自己动手写一个支持公式和图表的markdown 编辑器

    背景 在公司写周报时经常会用到markdown,并且曾经还为了能够解决团队每个人写完周报之后还要汇总的效率问题专门开发过一款内部使用的周报markdown编辑器,团队成员可以根据项目写相应的周报,最后...

    3 个月前
  • 程序员的专属微信公众号编辑器:定制 Markdown 转 HTML

    效果(Gif) 仓库地址 背景 在程序员的世界里,只要习惯用 Git,写文章必然就是 Markdown 了。 近来几天,重新玩起了微信公众号,最不能忍受的就是那个编辑器,效率很低。

    1 年前
  • 测试transform和left改变位置的帧率区别

    记一次平滑动画测试 如何使动画看起来更加的流畅,平滑?核心概念其实是帧数稳定在60帧左右。这次不讨论通过raf来实现平缓帧动画,讨论不依靠raf把现有的一些动画变得更加流畅,也就是平均帧数接近60帧。

    4 个月前
  • 春季新增开源项目:见过能斗图的 Markdown 编辑器吗

    (https://img.javascriptcn.com/8a1626f48d0cd90dbf0558744e98d3c8) 每月新增开源项目。顾名思义,每月更新一期。

    2 年前
  • 手动更新svg的transform属性后可能出现缩放时跳跃现象

    原因:通常在缩放事件中更新transform属性时会读取d3.event.scale和d3.event.translate中的值得到当前的缩放比例和平移值。当在事件之外更新时,元素虽然实现了指定的换,...

    4 个月前
  • 微信小程序Markdown、HTML解析库(支持wepy)

    Towxml 是一个可将、转为微信小程序(WeiXin Markup Language)的渲染库。 用于解决在微信小程序中、不能直接渲染的问题。 特色 支持代码语法高亮 支持emo...

    2 年前
  • 实现一个掘金Style的markdown目录

    20m 100m...,这是一件很恐怖的事情。另一方面你频繁的使用别人写好的轮子,很少自己思考和实现,长此以往,你的代码能力自然就下降了。所以我经常约束自己的一句话:能不用尽量不用,开发中一个轮子的...

    8 个月前

官方社区

扫码加入 JavaScript 社区