代码改变世界

Markdown的选择

2015-04-11 08:26  RTdo  阅读(379)  评论(0编辑  收藏  举报

直击现场

我一直在思索用什么格式存储文档比较好。之前一直在用google docs,但是它的格式不公开,上传/下载的时候需要转换格式,转换的时候必然会丢失一些信息。于是后来想,那还是纯本文或者markdown吧。

但是Markdown方言很多,选哪个好?我考察了以下三个。

  1. pandoc . John MacFarlane写的,万能的转换器。对我来说一个缺点是它的是Haskell语言。

  2. commonmark . 这是John MacFarlane搞的对markdown进行标准化的一场伟大尝试。他给出了C语言和js的实现,其中C语言的很容易在windows下编译过去,代码简单易懂。可惜,没人买账。

  3. GitHub Flavored Markdown . 这个大概是现在事实性的标准了。因为代码都托管在github上,所以写文档的时候自然也就顺着 github 的要求来。我没有找到它的开源实现。但是,atom.io是github公司的,而这个编辑器的markdown实现用的是 marked . 我试了下还不错。

于是我就照着例子拿marked写了一个简单的脚本来把markdown文件转换成html

var marked = require('marked');  
var fs = require('fs');  
var inputFileName=process.argv[2];  
var outputFileName=inputFileName.replace('.md','.html');  
console.log("processing "+inputFileName);  
var markdownString=fs.readFileSync(inputFileName,"utf8");  
marked.setOptions({  
  highlight: function (code) {
    return require('highlight.js').highlightAuto(code).value;
  }
});
htmlbody=marked(markdownString);  
html="<!DOCTYPE html><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" >\n"+  
"<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/styles/default.min.css\">\n"+
"</head><body>"+htmlbody+"</body></html>";
fs.writeFileSync(outputFileName,html,{encoding:"utf8"});  

html header中再加上一行mathjax的js链接,就可以在markdown中支持数学公式了。