44

I'm writing some documentation in Markdown, and creating a separate file for each section of the doc. I would like to be able to convert all the files to HTML in one go, but I can't find anyone else who has tried the same thing. I'm on a Mac, so I would think a simple bash script should be able to handle it, but I've never done anything in bash and haven't had any luck. It seems like it should be simple to write something so I could just run:

markdown-batch ./*.markdown

Any ideas?

Bart
  • 19,692
  • 7
  • 68
  • 77
Brock Boland
  • 15,870
  • 11
  • 35
  • 36

6 Answers6

80

Use pandoc — it's a commandline tool that lets you convert from one format to another. This tool supports Markdown to HTML and back.

E.g. to generate HTML from Markdown, run:

pandoc -f markdown index.md > index.html
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    +1 for pandoc... since he's writing it in sections, use pandoc to concatenate the necessary sections into one html file, if necessary. – Mica Sep 19 '09 at 00:31
  • 4
    `for i in /some/directory/*.md; do pandoc -f markdown -t html -s "$i" > "$i".html; done;` To convert all the files inside some folder. – eridani May 27 '17 at 23:04
  • For syntax highlighting of block code: `pandoc -s index.md -o index.html --metadata title="My Title"` | Reference - [Pandoc: Converting markdown to HTML, syntax highlighter](https://stackoverflow.com/questions/12311344/pandoc-converting-markdown-to-html-syntax-highlighter) – datalifenyc Jan 03 '19 at 18:30
  • Note that Pandoc supports several variants of Markdown such as [CommonMark](https://commonmark.org) (`commonmark`), [GitHub-Flavored Markdown](https://help.github.com/articles/github-flavored-markdown/) (`gfm`), etc. – ændrük Nov 21 '21 at 02:54
  • to convert all files in `content` directory `find ./content -name '*.md' | xargs -n 1 bash -c 'pandoc -f markdown -t html -o "${1/.md/.htm}" "${1}"' -` – Putna Jun 30 '23 at 07:25
25

This is how you would do it in Bash.

for i in ./*.markdown; do perl markdown.pl --html4tags $i > ${i%.*}.html; done;

Of course, you need the Markdown script.

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167
  • 12
    This solution is good but `index.md` will become `index.md.html`. If you'd rather have it become `index.html`, use this: `for i in ./*.md; do perl Markdown.pl --html4tags $i > output/${i%.*}.html; done;` – Maxime Apr 13 '12 at 16:10
3

If you have Node.js installed, then you can use the [MdPugToHtml] converter (https://www.npmjs.com/package/md-pug-to-html). It massively converts Markdown to Html. Moreover, it is possible to use Pug templates, but you can use them without templates.

The conversion is performed in the terminal with just one command:

npx md-pug-to-html /home/content

where:

  • npx is an npm command that installs md-pug-to-html at the first launch, and then launches the md-pug-to-html converter.
  • /home/content is a directory with your Markdown files. You may have another one.

The converter has various settings and can be used both in the CLI command line and has an API for use in applications.

There is detailed documentation on the MdPugToHtml converter in English and Russian.

injashkin
  • 11
  • 2
2

You can do this really easily with VS Code. (Well, this is not a command line tool, but proved itself to be super helpful.)

  • Install the Markdown All In One extension by Yu Zhang
  • Open the VS Code Command Palette (Ctrl-Shift-P), and select Markdown All In One: Print documents to HTML (select a source folder)
  • Tip: If you want to make your export portable, you want to change absolute image paths to relative paths by using the following setting in your settings.json (Ctrl-Shift-P -> Preferences: Open Settings (JSON))
    "markdown.extension.print.absoluteImgPath": false

In this way, after conversion, just copy all non-markdown files (images) to the destination folder and the HTML pages are portable.

Niko Föhr
  • 28,336
  • 10
  • 93
  • 96
-2

I use this in a .bat file:

@echo off
for %i in (*.txt) python markdown.py "%i"
Julio César
  • 12,790
  • 10
  • 38
  • 45
-2

// using Bash in mac

for i in *.md; do asciidoc  $i;  done; 
Bruce Zu
  • 507
  • 6
  • 17
  • 4
    This does not answer the question - the question asks about Markdown, not about AsciiDoc (though the two are similar). Also, you must quote the `$i`, otherwise this won't work for files with special characters in the name. – sleske Feb 28 '17 at 10:24