summaryrefslogtreecommitdiff
path: root/source/_posts/creating_an_ebook_with_markdown.md
blob: 29519bbb5ed50e7a6df54ede95e5bc2831c749b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
---
extends: _layouts.post
section: content
title: Creating an Ebook With Markdown
date: 2020-03-30
description: Use Markdown to Create an Ebook That Can Be Read on Various Devices
tags: [Markdown]
---

[Pandoc](https://pandoc.org/) is a great tool for converting a file in one markup format into another. This means we can use it to convert a file written in [Markdown](https://en.wikipedia.org/wiki/Markdown) into an [EPUB](https://en.wikipedia.org/wiki/EPUB) file that is supported by many e-readers.

Lets start by writting a very simple markdown file called `example_ebook.md`.

```markdown
---
title:
- type: main
  text: Example Ebook
- type: subtitle
  text: An Ebook created from a Markdown file
creator:
- role: author
  text: David Sadler
publisher: Published by myself
---

This is an introduction.

# Chapter 1

This is the first paragraph of chapter 1.

This is the second paragraph of chapter 1.

Below is a list.

- Item One
- Item Two
- Item Three

# Chapter 2

This is the first paragraph of chapter 2.

This is the second paragraph of chapter 2.

# Chapter 3

This is the first paragraph of chapter 3.

This is the second paragraph of chapter 3.
```

Note that the file begins with a [YAML metadata block](https://pandoc.org/MANUAL.html#extension-yaml_metadata_block) that starts and ends with three hyphens (`---`). This allows you to specify [EPUB metadata](https://pandoc.org/MANUAL.html#epub-metadata) such as the title and author.

Converting this to EPUB is done by running pandoc.

```shell
$ pandoc example_ebook.md -t epub3 --toc -o example_ebook.epub
```

There are several options that need to be passed to pandoc.

- `example_ebook.md` — This argument is the file that you are converting.
- `-t epub3` — Set the output format to be [EPUB v3 book](https://www.w3.org/community/epub3/).
- `--toc` — Include a table of contents in the output document. This will be derived from the H1 headers in the markdown.
- `-o example_ebook.epub` — Tell pandoc to output the conversion to the named file instead of *stdout*.

You can now copy the file `example_ebook.epub` to any device that supports the format or use one of the many software readers such as [Calibre](https://calibre-ebook.com/). However, if you wish to read this on a Kindle device you will need to convert it to the [Mobi](https://en.wikipedia.org/wiki/Comparison_of_e-book_formats#Mobipocket) format.

Amazon provides a command line tool called [KindleGen](https://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211) that can convert our EPUB file into the Mobi format. After downloading the tool just run it as shown below.

```shell
$ kindlegen example_ebook.epub
```

This will create a file called `example_ebook.mobi` that you can copy to your Kindle to read.

There is a [zip file](/assets/files/example_ebook.zip) available that contains all the example files.