https://www.penned.blog/creating-ebook-epub-file-from-markdown/
Creating an eBook-friendly EPUB file from Markdown Posted by Ben on September 18, 2017 This is part two of a series explaining how to publish your book using Markdown.
PART ONE: How to write your book in Markdown
PART TWO: (This post) Creating an eBook-friendly EPUB file from Markdown
PART THREE: Creating a Kindle MOBI file from your EPUB file
In part one, we covered how to write your book in markdown. Next, we’ll walk through how to create the files you need for Kindle, iBooks, and just about every other publishing platform out there.
If you’ve never done this before, you’ll need to understand a few basic concepts.
Markdown is a writing format, not a publishing format. In order to publish our book, we’ll need to convert to a format that includes all the extra stuff necessary for our book including table of contents, cover image, and metadata (which is just a fancy word for key attributes of your book like author, title, publisher, etc.). There are two main ebook formats – EPUB, and MOBI. EPUB is used by iBooks and others, and is as close to a ‘standard’ ebook format as it gets. MOBI on the other hand is required by Amazon for their Kindle. While you can upload an EPUB file to Kindle Direct Publishing, they will just convert it to a MOBI anyway. Personally I prefer creating the MOBI myself so I can be 100% sure that the formatting is what I want.
Creating your EPUB and MOBI files is just one step. The most important step is reviewing and confirming that your formatting is correct. For that, I’ll share some previewer tools that you can use. It’s also great for doing a full read of your book, and also sharing with friends and family pre-publishing.
Now on to the fun!
The steps below assume you have a Mac. If you have a PC, the steps will be similar but there are some details that will vary. Look for a PC specific guide in the future. Also, all the tools below are free. Yay!
Creating an EPUB file
For this step, we’re going to use Pandoc (a file converter tool) to to create the EPUB file. After that, we’ll use the EPUB file to create a MOBI file (the format we’ll want to use for Kindle).
We’re also going to use an app on our Mac called Terminal which is a command line interface. If you’ve never used a command line interface before, it may seem a little challenging, but it’s basically just a text box where you type commands.
Install Pandoc Open Terminal by pressing Command Space on your keyboard and typing Terminal
Navigate to the directory where your Markdown file lives. The easiest way to do this in Terminal is:
Type cd and then press Space on your keyboard Open Finder and drag the directory where your Markdown file lives into Terminal. This will automatically paste the directory name for you. Press Enter Run Pandoc to generate your EPUB file Type pandoc INPUTNAME.md -o OUTPUTNAME.epub where INPUTNAME is the filename of your book, and OUTPUTNAME is the name that you want the EPUB file to use. Typically you want the output name to be the same as the input name, with the exception of the filename extension (e.g. ‘epub’ vs ‘md’). Press Enter Your EPUB file will be generated and saved to the same directory as your Markdown file. If you would like to generate a table of contents and include a cover image, you can add a few switches, like this: pandoc -o OUTPUTNAME.epub INPUTNAME.md --toc --toc-depth=2 --epub-cover-image=COVERIMAGE.png For more switches and other useful documentation see Creating an ebook with Pandoc.
Previewing the EPUB file
On a Mac, it’s as simple as launching the EPUB file. It will automagically open in iBooks.
On a PC, it’s another story. Here are a few options for previewing your book:
Upload to Google Play books. I’ve noticed some issues though with how Google Play formats line breaks coming from Markdown. Download Sumatra PDF.
But Ben, what about […]
Table of contents? Pandoc will automatically generate a table of contents based on your header 1-3 tags (# etc) if you specify the switch --toc when generating your epub file. (To limit the toc level, use the switch --toc-depth=x. Personally I use --toc-depth=2.) Chapters and page breaks? Pandoc will use your header 1 tags as chapters, and insert a page break between each.
Ebook metadata like title, author, etc.? Include a metadata block at the top of your markdown file like so:
title: "My Book: A Story With Words" author: "John Smith"