Document to Document Cross References

The inline xref macro can also link to IDs in other AsciiDoc documents. This eliminates the need to use direct links between documents that are coupled to a particular converter (e.g., HTML links). It also captures the intent of the author to establish a reference to a section in another document.

Here’s how a cross reference is normally defined in Asciidoctor:

The section <<anchors>> describes how automatic anchors work.

This cross reference creates a link to the section with the ID images.

Let’s assume the cross reference is defined in the document document-a.adoc. If the target section is in a separate document, document-b.adoc, the author may be tempted to write:

Refer to link:document-b.html#section-b[Section B] for more information.

However, this link is coupled to HTML output. What’s worse, if document-b.adoc is included in the same document as document-a.adoc, the link will refer to a document that doesn’t even exist!

These problems can be alleviated by using an inter-document xref:

Refer to xref:document-b.adoc#section-b[Section B] for more information.

The ID of the target is now placed behind a hash symbol (#). Preceding the hash is the name of the reference document (the file extension is optional). We’ve also added a label since Asciidoctor cannot (yet) resolve the section title in a separate document.

When Asciidoctor generates the link for this cross reference, it first checks to see if document-b.adoc is included in the same document as document-a.doc. If not, it will generate a link to document-b.html, intelligently substituting the original file extension with the file extension of the output file.

<a href="document-b.html#section-b">Section B</a>

If document-b.adoc is included in the same document as document-a.doc, then the document will be dropped in the link target and look like the output of a normal cross reference:

<a href="#section-b">Section B</a>

Now you can create inter-document cross references without the headache.

In certain environments, such as a web interface for a source repository like or the browser preview extensions, you see the generated HTML when you visit the URL of the AsciiDoc source file. This has consequences for inter-document cross references.

Since the default suffix for inter-document cross references in the html5 backend is .html, the resulting link created in these environments ends up pointing to non-existent HTML files. In this case, you need to change the inter-document cross references to refer to other AsciiDoc source files instead.

The file extension chosen for inter-document cross references is controlled by the relfilesuffix attribute. By default, this attribute is not set and the value of the outfilesuffix is used instead. If you want to change the file extension that gets used, you can do so by setting the relfilesuffix attribute.

The following example demonstrates how to use the relfilesuffix attribute to control the file extension for inter-document cross references on GitHub and the browser preview extension.

= Document Title
ifdef::env-github,env-browser[:relfilesuffix: .adoc]

See the xref:README.adoc[README].

We could also write the link as link:README{relfilesuffix}[README].

The links in the generated document will now point to README.adoc instead of the default, README.html.

This configuration is not actually necessary on GitHub and GitLab since those environments now set the value of relfilesuffix to match the file extension of the source file. However, it may still be required in other GitHub-like environments, so it’s worth noting.

While relfilesuffix gives you control over the end of the resolved path for an inter-document cross reference, the relfileprefix attribute gives you control over the beginning of the path. When resolving the path of an inter-document cross reference, if the relfileprefix attribute is set, the value of this attribute gets prepended to the path. Let’s look at an example of when these two attributes are used together.

A common practice in website architecture is to move files into their own folder to make the path format agnostic (called “indexify”). For example, the path filename.html becomes filename (which targets filename/index.html). However, this is problematic for inter-document cross references. Any cross reference that resolves to the path filename.html is now invalid since the file has moved to a subfolder (and thus no longer a sibling of the referencing document).

To solve this problem, you can define the following two attributes:

:relfileprefix: ../
:relfilesuffix: /

Now, the cross reference <<filename.adoc,link text>> will resolve to ../filename instead of filename.html. Since this change is specific to the website architecture described, you want to be sure to only set these attributes in that particular environment (either using an ifdef directive or via the API).