Last Updated: February 25, 2016
·
2.589K
· losinggeneration

mdcat: Markdown to text

I realize that I'm more-and-more using cat to look at Markdown files. This means the files are sometimes poorly formatted and have a lot of extra Markdown specifics included. I decided to do a simple shell function to create a markdown cat (mdcat). To create this I rely on two programs: markdown (I use the one from discount, though any program that takes in a files as arguments and outputs HTML should work) and html2text. The function ends up being extremely short:

function mdcat()
{
    cat $* | markdown | html2text
}

function mdmore()
{
    mdcat $* | more
}

function mdless()
{
    mdcat $* | less
}

That's right, one line is all that's needed. So this ends up being a convenience function to save me typing more than anything else.

Limitations (see below)

It's worth noting that this isn't exactly ideal. Piping into mdcat doesn't work, for instance. So things like this that you'd expect to work don't.

grep "Some text" README.md | mdcat

Thanks to @cbojar for the suggestion which removes the limitation I had above. Also, thanks to @kloetzl on Twitter for pointing out the shortened version of mdcat.

Implementation note

Like most things, this isn't the only way. Another way this could also be accomplished is using pandoc and a shell alias:

alias mdcat="pandoc -f markdown -t plain"

1 Response
Add your response

If you're willing to have a few lines more, you can do a conditional based on the arg count:

function mdcat() {
    if [ $# = 0 ]; then
        markdown | html2text
    else
        markdown < $* | html2text
    fi
}

Then it will work with piped input.

over 1 year ago ·