Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure 'less' to show formatted markdown files?

I would like to have less display *.md markdown files with some formatting -- like I know less can, for manpages, etc. I am running Ubuntu 12.04.

I am as far as putting a user defined filter into .lessfilter:

#!/bin/sh
case "$1" in
  *.md)
    fn=/tmp/$1.$$.html
    markdown "$1" | html2txt > $fn  ### LOSES FORMATTING
    cat $fn                         ### TO STDOUT???
    ;;
  *)
  # We don't handle this format
  exit 1
esac
# No further processing by lesspipe necessary
exit 0

So, the main questions are:

  • How can I pass some basic formatting information to less as well, instead of losing it with html2txt
  • Is it correct to just print the new content to stdout? Or could I just write the *.html to file disk and let less handle that html at its own digression (seeing the html-extension and acting on it?)
like image 568
towi Avatar asked Mar 19 '13 10:03

towi


People also ask

How do you display Markdown in HTML?

The first step to convert Markdown to HTML is to open your Markdown document in Atom. Then toggle the Markdown preview by pressing the CTRL+SHIFT+M keyboard shortcut. Another way to toggle the Markdown preview is from Packages —> Markdown Preview —> Toggle Preview.

Can a browser display Markdown?

Markdown applications use something called a Markdown processor (also commonly referred to as a “parser” or an “implementation”) to take the Markdown-formatted text and output it to HTML format. At that point, your document can be viewed in a web browser or combined with a style sheet and printed.

How do you Markdown a preview in Linux?

If you haven't seen any markdown document yet, just head over to GitHub and enter any open-source project. The README file almost certainly will be written using Markdown. Basically, you use some 'code' before your text and your text will be formatted for display accordingly.


2 Answers

Take a look at Pandoc. It can convert files from markdown format to groff man pages which you can then view in man.

Your .lessfilter script would be:

case "$1" in
  *.md)
    pandoc -s -f markdown -t man "$1" | man -l -
    ;;

Alternatively, convert it to html using the markdown command and then use the lynx browser to view it, but this didn't work too well for me.

case "$1" in
  *.md)
    markdown "$1" | lynx -stdin
    ;;

And, yes, the lessfilter script must write to stdout.

like image 152
dogbane Avatar answered Oct 13 '22 07:10

dogbane


Dogbane's answer is great, but if you use groff -T utf8 -man instead of man -l to do the formatting, then the bold, italic, etc. come through. As seen here: https://stackoverflow.com/a/20197316/2674930.

like image 24
Jeremy Muhlich Avatar answered Oct 13 '22 08:10

Jeremy Muhlich