Jun 25, 2018

Markdown cheatsheet, markdown-it plugins

I created this post for those who are unfamiliar with the syntax of Markdown which is a markup language. People who are already familiar with Markdown should also check this post because I use some special plugins for markdown-it that provide additional features.

For safety reasons, you cannot include HTML code into Markdown. By default, HTML is escaped.

Table of Contents


General syntax

  1. Headings
  2. Emphasis
  3. Paragraphs, line breaks
  4. Horizontal rule
  5. Blockquotes
  6. Links
  7. Lists

Special syntax

  1. Add a CSS class and an #id to an element
  2. Table with caption
  3. Image with caption
  4. Code syntax highlighting

General syntax

1. Headings

# H1
## H2
### H3
#### H4
##### H5
###### H6

Alternatively, for H1 and H2, an underline-ish style:

Alt-H1
======

Alt-H2
------

H1

H2

H3

H4

H5
H6

Alternatively, for H1 and H2, an underline-ish style:

Alt-H1

Alt-H2

2. Emphasis

Emphasis, aka italics, with _asterisks_ or _underscores_.

Strong emphasis, aka bold, with **asterisks** or **underscores**.

Combined emphasis with **asterisks and _underscores_**.

Strikethrough uses two tildes. ~~Scratch this.~~

Emphasis, aka italics, with asterisks or underscores.

Strong emphasis, aka bold, with asterisks or underscores.

Combined emphasis with asterisks and underscores.

Strikethrough uses two tildes. Scratch this.

3. Paragraphs, line breaks

My basic recommendation for learning how line breaks work is to experiment and discover -- hit <Enter> once (i.e., insert one newline), then hit it twice (i.e., insert two newlines), see what happens. You'll soon learn to get what you want. "Markdown Toggle" is your friend.

Here are some things to try out:

Here's a line for us to start with.

This line is separated from the one above by two newlines, so it will be a _separate paragraph_.

This line is also a separate paragraph, but...
This line is only separated by a single newline, so it's a separate line in the _same paragraph_.
Here's a line for us to start with.

This line is separated from the one above by two newlines, so it will be a separate paragraph.

This line is also begins a separate paragraph, but...
This line is only separated by a single newline, so it's a separate line in the same paragraph.

Here's a line for us to start with.

This line is separated from the one above by two newlines, so it will be a separate paragraph.

This line is also a separate paragraph, but... This line is only separated by a single newline, so it's a separate line in the same paragraph. Here's a line for us to start with.

This line is separated from the one above by two newlines, so it will be a separate paragraph.

This line is also begins a separate paragraph, but... This line is only separated by a single newline, so it's a separate line in the same paragraph.

4. Horizontal rule

Three or more...

---

Hyphens

---

Asterisks

---

Underscores

Three or more...


Hyphens


Asterisks


Underscores

5. Blockquotes

> Blockquotes are very handy in email to emulate reply text.
> This line is part of the same quote.

Quote break.

> This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can _put_ **Markdown** into a blockquote.

Blockquotes are very handy in email to emulate reply text. This line is part of the same quote.

Quote break.

This is a very long line that will still be quoted properly when it wraps. Oh boy let's keep writing to make sure this is long enough to actually wrap for everyone. Oh, you can put Markdown into a blockquote.

Some ways to use links:

[I'm an inline-style link](https://www.google.com)

[I'm an inline-style link with title](https://www.google.com "Google's Homepage")

[I'm a reference-style link][arbitrary case-insensitive reference text]

[I'm a relative reference to a repository file](../blob/master/LICENSE)

[You can use numbers for reference-style link definitions][1]

Or leave it empty and use the [link text itself].

URLs in angle brackets will automatically get turned into links:  
<http://www.example.com>.

Some text to show that the reference links can follow later.

[arbitrary case-insensitive reference text]: https://www.mozilla.org
[1]: http://slashdot.org
[link text itself]: http://www.reddit.com

I'm an inline-style link

I'm an inline-style link with title

I'm a reference-style link

You can use numbers for reference-style link definitions

Or leave it empty and use the link text itself.

URLs in angle brackets will automatically get turned into links:
http://www.example.com.

Some text to show that the reference links can follow later.

7. Lists

(In this example, leading and trailing spaces are shown with with dots: ⋅)

1. First ordered list item
2. Another item
   ⋅⋅\* Unordered sub-list.
3. Actual numbers don't matter, just that it's a number
   ⋅⋅1. Ordered sub-list
4. And another item.

⋅⋅⋅You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).

⋅⋅⋅To have a line break without a paragraph, you will need to use two trailing spaces.⋅⋅
⋅⋅⋅Note that this line is separate, but within the same paragraph.⋅⋅
⋅⋅⋅(This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)

- Unordered list can use asterisks

* Or minuses

- Or pluses
  1. First ordered list item
  2. Another item
  1. Actual numbers don't matter, just that it's a number

  2. Ordered sub-list

  3. And another item.

    You can have properly indented paragraphs within list items. Notice the blank line above, and the leading spaces (at least one, but we'll use three here to also align the raw Markdown).

    To have a line break without a paragraph, you will need to use two trailing spaces.
    Note that this line is separate, but within the same paragraph.
    (This is contrary to the typical GFM line break behaviour, where trailing spaces are not required.)

Special syntax

1. Add a CSS class and an #id to an element

One example add underline to a link (by default, Bootstrap 3 does not use underline for the links - I might change this behaviour though):

[link](https://highlightjs.org/){.underline}

And the result is: link

It is very important that you should not use whitespace, otherwise it will not work!

Add an id to an element: ## Special syntax{id=special}. It converts to <h2 id="special">Special syntax</h2>.

2. Table with caption

| Year | Column 1 | Column 2 |
| ---- | -------- | -------- |
| 1993 | 60       | 70       |
| 2007 | 145      | 300      |
| 2010 | 455      | 700      |
| 2018 | 1024     | 576      |

[Table 1: Three columns filled with some numbers]

And the result:

Table 1: Three columns filled with some numbers
Year Column 1 Column 2
1993 60 70
2007 145 300
2010 455 700
2018 1024 576

3. Image with caption

Syntax: ![caption text](link-to-image)

Example: ![Figure 1. Interest in static site generators over time (Google Trends)](/assets/images/uploads/trend.png)

Figure 1. Interest in static site generators over time (Google Trends)

4. Code

```javascript

var a = 20

function foo (a) {
  console.log(a)
}

foo(a)
```

var a = 20

function foo (a) {
  console.log(a)
}

foo(a)

To have proper syntax highlighting, you have to integrate the highlight.js library.

Source of the general syntax part