Lucas Vieites .com Logo
Lucas Vieites .com
«I intend all my puns»

Converting MarkDown to HTML with pandoc

 

pandoc is a universal document converter that supports many formats.

To convert a MarkDown document HTML you can use the following command:

pandoc -s -f markdown -t html5 -o target.html source.md

where:

  • -s: produces a standalone document, as opposed to just a fragment
  • -f: --from indicates the source formats
  • -t: --to indicates the target format
  • -o: --output indicates the target file where the converted document will be written

There are many other useful arguments that you can use to customize the output, for example:

  • -c: -css link to a stylesheet. You can use a full URL or a local path
  • -H: --include-in-header indicates a file to include as a header in the output

Example:

pandoc -s -f markdown  -c "assets/md2html.css" -H "assets/header.html" -t html5 -o mynotes.html my_notes.md

Using pandoc to convert a MarkDown file to HTML with Geany build commands

The Geany IDE provides a handy mechanism to assign custom build commands to specific file types.

Follow these steps to create a new menu item in the Build menu that will generate an HTML document in the same directory where your MarkDown file is.

  1. Open a MarkDown file in Geany
  2. Go to Build > Set Build Commands
  3. Under Markdown commands, click on the first empty button and set the name for the menu item in the dialog window that pops up.
  4. In the Command field, enter the following command:
    • pandoc -s -f markdown -t html5 %f -c ~/assets/md2html.css -H ~/assets/header.html > %e.html

Now you can simply press F8 to generate an HTML version of your MarkDown document. Check the Message Window (View > Show Message Window) for compilation errors.

Find more information about the available options in Geany's Build commands in the project's documentation: User Guide to configuring the Build Menu.

Author:   ✦  Published:   ✦  Updated:   ✦