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

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

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

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


当前回答

参见https://softwareengineering.stackexchange.com/a/128721/24257。


如果你对我们(Github)如何渲染Markdown文件感兴趣,你可能想看看Redcarpet,我们的Ruby接口到Sundown库。

Ruby-script,使用Redcarpet,将是“命令行工具”,如果你有本地Ruby

其他回答

我的最终解决方案是使用Python Markdown。我自己做了扩建,把栅栏块修好了。

我最近做了你想要的东西,因为我需要从Markdown文件生成文档,GitHub风格非常好。试一试。它是用Node.js编写的。

gfm

这主要是@barry- states对使用Pandoc的回答的后续。如果你用的是Mac电脑,Homebrew也有这个功能:

brew install pandoc

Pandoc通过markdown_github名称支持GFM作为输入格式。

输出到文件

cat foo.md | pandoc -f markdown_github > foo.html

在Lynx中打开

cat foo.md | pandoc -f markdown_github | lynx -stdin # To open in Lynx

在OS X的默认浏览器中打开

cat foo.md | pandoc -f markdown_github > foo.html && open foo.html # To open in the default browser on OS X`

TextMate软件集成

像大多数编辑器所允许的那样,您始终可以将当前选择或当前文档输送到上述其中一个。您还可以轻松地配置环境,以便pandoc替换Markdown包使用的默认Markdown处理器。

首先,创建一个包含以下内容的shell脚本(我将其命名为ghmarkdown):

#!/bin/bash
# Note included, optional --email-obfuscation arg
pandoc -f markdown_github --email-obfuscation=references

然后,您可以将TM_MARKDOWN变量(在Preferences→Variables中)设置为/path/to/ghmarkdown,它将替换默认的Markdown处理器。

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

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

根据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)