Is there a command line utility for rendering GitHub flavored Markdown?

后端 未结 25 1568
一个人的身影
一个人的身影 2020-11-28 16:53

I\'m wondering if there is a command line utility for taking a GitHub flavored Markdown file and rendering it to HTML.

I\'m using a GitHub wiki to create website con

相关标签:
25条回答
  • 2020-11-28 17:48

    Maybe this might help:

    gem install github-markdown
    

    No documentation exists, but I got it from the gollum documentation. Looking at rubydoc.info, it looks like you can use:

    require 'github/markdown'  
    puts GitHub::Markdown.render_gfm('your markdown string')
    

    in your Ruby code. You can wrap that easily in a script to turn it into a command line utility:

    #!/usr/bin/env ruby
    
    # render.rb
    require 'github/markdown'
    
    puts GitHub::Markdown.render_gfm File.read(ARGV[0])
    

    Execute it with ./render.rb path/to/my/markdown/file.md. Note that this is not safe for use in production without sanitization.

    0 讨论(0)
  • 2020-11-28 17:48

    This is mostly a follow-on to @barry-staes's answer for using Pandoc. Homebrew has it as well, if you're on a Mac:

    brew install pandoc
    

    Pandoc supports GFM as an input format via the markdown_github name.

    Output to file

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

    Open in Lynx

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

    Open in the default browser on OS X

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

    TextMate Integration

    You can always pipe the current selection or current document to one of the above, as most editors allow you to do. You can also easily configure the environment so that pandoc replaces the default Markdown processor used by the Markdown bundle.

    First, create a shell script with the following contents (I'll call it ghmarkdown):

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

    You can then set the TM_MARKDOWN variable (in Preferences→Variables) to /path/to/ghmarkdown, and it will replace the default Markdown processor.

    0 讨论(0)
  • 2020-11-28 17:48

    I recently made what you want, because I was in need to generate documentation from Markdown files and the GitHub style is pretty nice. Try it. It is written in Node.js.

    gfm

    0 讨论(0)
  • 2020-11-28 17:54

    I found a website that will do this for you: http://tmpvar.com/markdown.html. Paste in your Markdown, and it'll display it for you. It seems to work just fine!

    However, it doesn't seem to handle the syntax highlighting option for code; that is, the ~~~ruby feature doesn't work. It just prints 'ruby'.

    0 讨论(0)
  • 2020-11-28 17:56

    Building on this comment I wrote a one-liner to hit the Github Markdown API using curl and jq.

    Paste this bash function onto the command line or into your ~/.bash_profile:

    mdsee(){ 
        HTMLFILE="$(mktemp -u).html"
        cat "$1" | \
          jq --slurp --raw-input '{"text": "\(.)", "mode": "markdown"}' | \
          curl -s --data @- https://api.github.com/markdown > "$HTMLFILE"
        echo $HTMLFILE
        open "$HTMLFILE"
    }
    

    And then to see the rendered HTML in-browser run:

    mdsee readme.md
    

    Replace open "$HTMLFILE" with lynx "$HTMLFILE" if you need a pure terminal solution.

    0 讨论(0)
  • 2020-11-28 17:57

    There is a really nice and simple tool for browsing GFM Markdown documents:

    GFMS - Github Flavored Markdown Server

    It's simple and lightweight (no configuration needed) HTTP server you can start in any directory containing markdown files to browse them.

    Features:

    • Full GFM Markdown support
    • Source code syntax highlighting
    • Browsing files and directories
    • Nice looking output (and configurable CSS stylesheets)
    • Export to PDF
    0 讨论(0)
提交回复
热议问题