Bulk converting Markdown to HTML

illustrations illustrations illustrations illustrations illustrations illustrations illustrations
post-thumb

Published on 20 July 2023 by Andrew Owen (2 minutes)

Converting Markdown to HTML is easy. Just publish it with a static site generator (SSG). But maybe you’re using a cloud-based Markdown documentation solution, and you don’t have a local SSG. Then you probably don’t want to go to the trouble of setting one up just to do the conversion. There are free online converters, but I haven’t found any that do conversion in bulk. There are also plenty of code examples for languages like JavaScript and Python. But what if you just need your Markdown in HTML format now?

The simplest solution is a plug-in for Visual Studio Code called Markdown All in One by Yu Zhang. Install VScode. Click the extensions icon and search for markdown. Install the extension. Now you’re ready to go.

  1. Open the folder containing the Markdown files you want to convert.
  2. Open at least one file.
  3. Press Ctrl+Shift+P (Command+Shift+P on Macs) to open the Command Palette.
  4. In the search bar, start typing Markdown.
  5. Click Markdown All in One: Print documents to HTML.
  6. Select a folder, then press Enter. The folder must be within the project.

But if you prefer to use the command line, you can use Pandoc. Here’s a destructive Bash script (it removes the .md files) I wrote that you can drop into a folder containing Markdown. It supports one level of nested folders.

for subdir in *; do
test -d "$subdir" || continue
echo $subdir
cd "$subdir"

for subdir2 in *; do
test -d "$subdir2" || continue
echo $subdir2
cd "$subdir2"

for g in *.md; do
export FILE2=${g%%.md}
pandoc -f gfm -t html $FILE2.md -o $FILE2.html
rm $FILE2.md
done

cd ..
done

for f in *.md; do
export FILE=${f%%.md}
pandoc -f gfm -t html $FILE.md -o $FILE.html
rm $FILE.md
done

cd ..
done

Thanks to Bill Dyer for alerting me to Yu Zhang’s VScode plug-in.