Markdown Cheat Sheet

The majority of the pages that make up this website are written in markdown (a lightweight markup language created by John Gruber) before being converted to hypertext markup language (HTML) using Hugo (https://gohugo.io).

I used to use Pandoc, a command-line tool designed to convert almost any document type, from or to any other type. For example, HTML to Word (.docx), spreadsheets to portable document format (PDF), or in my case, markdown to HTML.

This article serves as a basic reference guide — a cheat sheet — for using markdown. I’ll also include a few useful Pandoc commands.

Figure 1: The Markdown mark by Dustin Curtis

Markdown elements

The following are the common features or elements of markdown that I use all the time.

Document Title

---
title: 'Document title'
---

Headings

## Heading 2
### Heading 3
#### Heading 4

The title will be converted to heading 1.

[Click here](https://www.crnect.co.uk)

Images

Adding an image is almost the same as adding a hyperlink:

![Alt text](https://www.example.com/image.png)

Pandoc

The Pandoc command I use most often is pandoc --template=pandoc-template.html -o $filename.html $filename.md. In fact, I use it as part of a bash script, to generate multiple HTML documents at once:

#!/bin/bash

rm articles.list # this file will be created by the script

# loop through each directory in articles, and loop through each .md file
for directory in `ls articles`; do
	for mdfile in `ls articles/$directory/*.md`; do
		filename=$(echo $mdfile | awk -F "." '{print $1}');
		echo $filename;
		pandoc --template=pandoc-template.html -o $filename.html $filename.md
		# get article title
		title=$(head --lines=2 $mdfile | awk -F "'" '{print $2}');
		echo $title,$filename.html >> articles.list
	done;
done;

# create list of links and redirect to articles.md
cat articles.list | awk -F "," '{print "* ["$1"]("$2")"}' > articles.md;

# combine home-top.md and articles.md in home.md
cat home-top.md articles.md > home.md

# create the home page:
pandoc --template=pandoc-template.html -o home.html home.md;

# WARNING! copy entire directory to /var/www/html/
# This is used for test purposes only

sudo cp -r * /var/www/html/