less-style markdown viewer for UNIX systems

后端 未结 9 1430
刺人心
刺人心 2020-12-22 21:39

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 comman

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-22 22:24

    This is an alias that encapsulates a function:

    alias mdless='_mdless() { if [ -n "$1" ] ; then if [ -f "$1" ] ; then cat <(echo ".TH $1 7 `date --iso-8601` Dr.Beco Markdown") <(pandoc -t man $1) | groff -K utf8 -t -T utf8 -man 2>/dev/null | less ; fi ; fi ;}; _mdless '
    

    Explanation

    • alias mdless='...' : creates an alias for mdless
    • _mdless() {...}; : creates a temporary function to be called afterwards
    • _mdless : at the end, call it (the function above)

    Inside the function:

    • if [ -n "$1" ] ; then : if the first argument is not null then...
    • if [ -f "$1" ] ; then : also, if the file exists and is regular then...
    • cat arg1 arg2 | groff ... : cat sends this two arguments concatenated to groff; the arguments being:
      • arg1: <(echo ".TH $1 7date --iso-8601Dr.Beco Markdown") : something that starts the file and groff will understand as the header and footer notes. This substitutes the empty header from -s key on pandoc.
      • arg2: <(pandoc -t man $1) : the file itself, filtered by pandoc, outputing the man style of file $1
    • | groff -K utf8 -t -T utf8 -man 2>/dev/null : piping the resulting concatenated file to groff:
      • -K utf8 so groff understands the input file code
      • -t so it displays correctly tables in the file
      • -T utf8 so it output in the correct format
      • -man so it uses the MACRO package to outputs the file in man format
      • 2>/dev/null to ignore errors (after all, its a raw file being transformed in man by hand, we don't care the errors as long as we can see the file in a not-so-much-ugly format).
    • | less : finally, shows the file paginating it with less (I've tried to avoid this pipe by using groffer instead of groff, but groffer is not as robust as less and some files hangs it or do not show at all. So, let it go through one more pipe, what the heck!

    Add it to your ~/.bash_aliases (or alike)

提交回复
热议问题