Kindle xRef link bug


When I first started generating Mobi files (for the Kindle) in addition to my ePub workflow, I came up with a list of annoying formatting issues that I just accepted and chalked up as “Kindle Formatting Limitations”. ie:

  • No embedded fonts
  • Extremely limited stylesheet options
  • Formatting being ignored when linking from one section to another
  • Page elements being pushed to previous page when linking from one section to another
  • And so on

However, the item highlighted in green (in the above list) bugged me the most because it just didn’t seem right.

After digging much deeper into the issue, I finally found the cause, followed by a solution.
(I use InDesign CS5.5 for the majority of my ePub/Mobi workflows, so that is what I will be referencing in the following examples.)

My workflow:

It seems that the Kindle will choke if an “Anchor” id is placed (inline) with the referenced line of text:

<h1 id="toc_marker-2"><a id="Anchor"/>CHAPTER 2</h1>

This example references the id in “Green” above:

Jumping to Chapter 2 from the TOC link causes no formatting issues on the Kindle

And this example references the id in “Red” above:

Jumping to Chapter 2 from an in-text cross-reference causes the Kindle to ignore formatting for the destination line

Why are there 2 different “id” locations?

InDesign CS5.5 creates ePub “id”s based on 2 types of sources:

  1. In the ePub Export Settings Dialog, you choose which InDesign TOC preset to use in order to create the .ncx file for the ePub

    When InDesign creates these links within the ePub files, it places the “id”s inside the opening html tag:

    <h1 id="toc_marker-2">

    When run through Kindlegen to create the Mobi file, these links work just fine on the Kindle.

  2. When you create internal Hyperlinks and Cross-references within InDesign, they export to ePub links as “id”s wrapped in <a> tags, and then placed in-between the corresponding HTML opening/closing tags:
    <h1 id="toc_marker-2"><a id="Anchor"/>CHAPTER 2</h1>

    When run through Kindlegen to create the Mobi file, these links cause the “loss of formatting” issue (illustrated above).

**It is worth noting that both of these examples of “id” locations are perfectly valid HTML and ePub. The Kindle is the only eBook reader/device (that I am aware of) that does not play nicely with both.

How to fix this on the Kindle?

After some testing, I found that simply moving the “bad id” outside of the HTML tag, solves the issue:

From this:

<h1 id="toc_marker-2"><a id="Anchor"/>CHAPTER 2</h1>

To this:

<a id="Anchor"/><h1 id="toc_marker-2">CHAPTER 2</h1>

How I accomplish this using GREP

I use either Oxygen or TextWrangler (on my Mac) to do my ePub/Mobi post-processing.

  • Oxygen is my preferred program because it does not require cracking open the ePub ahead of time. And it also has built-in ePub 2 validation. However, it is painfully slow when trying to apply batch fixes on large ePub files (hundreds or thousands of internal ePub HTML files), which I do a lot of.
  • I use TextWrangler to run my batch fixes on these larger projects. You need to crack the ePub file open to do so, but it is extremely fast and worth the extra step. And it is also free.

Here is the GREP pattern that I would use to fix the example in this post:


(<h\d/?[^\>]+class="/?[^\>]+">)(<a id="Anchor"/>)



The above pattern splits the search pattern into 2 slices and then flips them.

It can (and should) be re-written to account for other HTML tags (ie. <p>, <pre>, <div>, etc.) and multiple “Anchor id”s (ie. “Anchor-1”, “Anchor-12”, etc.).
Basically, it can be re-written to account for your specific situations.

I hope this is helpful to you all.



ePub Samples

Here is a small sampling of some books that I recently converted to ePub.

Note: All of these books were initially designed and laid-out (using Adobe InDesign CS4 or CS5.5) for the purpose of traditional printing. Using those same files, I exported to ePub, created custom stylesheets (CSS), and cleaned up the code using Oxygen.

The very nature of the ePub format is to allow enough flexibility so that the books can be read on any size/type of device/reader that can read ePubs. Because of this, the files may look slightly different when compared from one device to another. However, great care has been taken so that the content looks good and is legible on all ePub-compatible readers/devices.

With that said, these ePubs have been optimized for the iPad (since it is by far the most popular ePub reading device). So, the sample images that you are about to see are screen grabs from the iPad.


Art Space Tokyo

Ashley Rawlings & Craig Mod, Pre/Post
(Adobe InDesign CS5.5)

AST Table of Contents

Art Space Tokyo cover and TOC (Horizontal view)

AST chapter start

Sample chapter start with Title and Map (Vertical view)


Map zoomed in to fit the screen (Horizontal view). (You can double-tap images in iBooks in order to zoom in)

AST Interview

Start of Interview section with intros (Vertical view)

AST Interview

Sample Interview section (Horizontal view)


Google SketchUp Cookbook

Bonnie Roskes, O’Reilly Media
(Adobe InDesign CS4)

GSC Table of Contents

Google Sketchup Cookbook cover and TOC (Horizontal view)

GSC spread

Sample spread with images and Note (Horizontal view)

GSC spread

Sample spread with image (Horizontal view)

GSC sample page

Sample page with image and sidebar (Vertical view)


The Geek Atlas

John Graham-Cumming, O’Reilly Media
(Adobe InDesign CS4)

GA Table of Contents

Geek Atlas Table of Contents (Vertical view)

GA page

Section start with title and map (Vertical view)

GA spread

Sample spread with sidebar and image (Horizontal view)


Building the Perfect PC, 3e

Robert Bruce Thompson & Barbara Fritchman Thompson, O’Reilly Media
(Adobe InDesign CS4)

BPpc Table of Contents

Building the Perfect PC cover and TOC (Horizontal view)

BPpc sample spread

Chapter start (Horizontal view)

BPpc sample spread

Start of Hardware Design Criteria section (Horizontal view)

BPpc sample spread

Sample spread with Warning (Horizontal view)

BPpc sample spread

Sample spread with images (Horizontal view)

BPpc sample page

Sample page with table (Vertical view)