less-style markdown viewer for UNIX systems

I have a Markdown string in JavaScript, and I'd like to display it (with bolding, etc) in a less (or, I suppose, more)-style viewer for the command line.

For example, with a string

"hello\n" + 
"_____\n" + 
"*world*!"

I would like to have output pop up with scrollable content that looks like

hello

world

Is this possible, and if so how?


Solution 1:

Pandoc can convert Markdown to groff man pages.

This (thanks to nenopera's comment):

 pandoc -s -f markdown -t man foo.md | man -l -

should do the trick. The -s option tells it to generate proper headers and footers.

There may be other markdown-to-*roff converters out there; Pandoc just happens to be the first one I found.

Another alternative is the markdown command (apt-get install markdown on Debian systems), which converts Markdown to HTML. For example:

markdown README.md | lynx -stdin

(assuming you have the lynx terminal-based web browser).

Or (thanks to Danny's suggestion) you can do something like this:

markdown README.md > README.html && xdg-open README.html

where xdg-open (on some systems) opens the specified file or URL in the preferred application. This will probably open README.html in your preferred GUI web browser (which isn't exactly "less-style", but it might be useful).

Solution 2:

I tried to write this in a comment above, but I couldn't format my code block correctly. To write a 'less filter', try, for example, saving the following as ~/.lessfilter:

#!/bin/sh

case "$1" in
    *.md)
        extension-handler "$1"
        pandoc -s -f markdown -t man "$1"|groff -T utf8 -man -
        ;;
    *)
        # We don't handle this format.
        exit 1
esac

# No further processing by lesspipe necessary
exit 0

Then, you can type less FILENAME.md and it will be formatted like a manpage.