Writing Hypertext Fiction in Markdown
Remember Choose Your Own Adventure books? I fondly remember finding new ways to get myself killed as I explored Aztec ruins or fought off aliens. Death or adventure waited just a few pages away and I was the one calling all the shots.
Introducing my son to Hypertext Fiction has rekindled my interest. I wondered how difficult it would be to throw something together to let me easily write CYOA-style books my kid could read on a kindle. I love markdown, so a toolchain built around it was definitely in order.
As it turns out, Pandoc fits the bill perfectly. You can write a story in markdown and easily export it to EPUB. From there you’re just a quick step through ebook-convert (via calibre’s commandline tools) to a well-formed .mobi file that reads beautifully on a kindle.
Here’s a quick example markdown story:
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
From the top, we have percent signs before the title and publishing date which Pandoc uses for the title page.
Then each chapter/section begins with an h1 header which has an id specified. This id is what we’ll use in our links to let a reader choose where to go next.
If you don’t specify a link, Pandoc will dasherize your header text, but it is probably easier to be specific since you need to reference it in your link choices anyway.
Save that as story.md and run the following to get your epub and mobi versions:
pandoc -o story.epub story.md && /usr/bin/ebook-convert story.epub story.mobi
BONUS: ebook-convert even complains if one of your links points to an invalid destination.
Here’s a preview as seen in Kindle Previewer
Now, get writing!