我想知道是否有一个命令行实用程序可以将GitHub风味Markdown文件并将其呈现为HTML。

我正在使用GitHub维基创建网站内容。我已经在服务器上克隆了存储库,然后想把它处理成常规HTML。对我来说,重要的是,在GitHub上出现的内容正是我的网站应该如何寻找的。我也非常喜欢使用带~~~的围栏块,所以我宁愿不使用标准Markdown语法。

我已经看了一下JavaScript的实时预览,我想我可以把它挂钩到Node.js,但他们说它已经被弃用了。我已经查看了红地毯存储库,但它看起来不像有命令行界面。

我选择了自己的解决方案,但是,由于这里没有一个解决方案明显比其他解决方案更好,所以我将不选择答案。


当前回答

根据Jim Lim的回答,我安装了GitHub Markdown gem。这包括一个名为gfm的脚本,它接受命令行上的文件名,并将等效的HTML写入标准输出。我稍微修改了一下,把文件保存到磁盘上,然后用launchy打开标准浏览器:

#!/usr/bin/env ruby

HELP = <<-help
  Usage: gfm [--readme | --plaintext] [<file>]
  Convert a GitHub-Flavored Markdown file to HTML and write to standard output.
  With no <file> or when <file> is '-', read Markdown source text from standard input.
  With `--readme`, the files are parsed like README.md files in GitHub.com. By default,
  the files are parsed with all the GFM extensions.
help

if ARGV.include?('--help')
  puts HELP
  exit 0
end

root = File.expand_path('../../', __FILE__)
$:.unshift File.expand_path('lib', root)

require 'github/markdown'
require 'tempfile'
require 'launchy'

mode = :gfm
mode = :markdown if ARGV.delete('--readme')
mode = :plaintext if ARGV.delete('--plaintext')

outputFilePath = File.join(Dir.tmpdir, File.basename(ARGF.path))  + ".html"

File.open(outputFilePath, "w") do |outputFile |
    outputFile.write(GitHub::Markdown.to_html(ARGF.read, mode))
end

outputFileUri = 'file:///' + outputFilePath

Launchy.open(outputFileUri)

其他回答

我使用Pandoc选项——from=gfm为GitHub调味Markdown如下:

$ pandoc my_file.md   --from=gfm -t html -o my_file.html

带浏览器的Pandoc对我来说很好。

用法:cat README。Md | pandoc -f markdown_github |浏览器

安装(假设你使用的是Mac OSX):

安装pandoc $ brew安装浏览器

Debian/Ubuntu: apt-get安装pandoc浏览器

我找到了一个网站,可以帮你做到这一点:http://tmpvar.com/markdown.html。粘贴你的Markdown,它会为你显示它。它似乎工作得很好!

然而,它似乎不能处理代码的语法高亮显示选项;也就是说,~~~ruby特性不起作用。它只打印‘ruby’。

GitHub有一个Markdown API,你可以使用。

使用标记。它支持GitHub调味Markdown,可以作为Node.js模块和从命令行使用。

一个例子是:

$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>