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 AsciiDoc:
The section <<anchors>> describes how automatic anchors work.
This cross reference creates a link to the section with the ID anchors.
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 link text since an AsciiDoc processor is not (yet) required to resolve the section title in a separate document.
While the link text for a local (i.e., intradocument) cross reference is optional, the link text for an interdocument cross reference is (currently) required. |
When the AsciiDoc processor 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 (by comparing the xref target to the include targets relative to the outermost document). 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.
Navigating between source files
In certain environments, such as a web interface for a source repository or an editor preview, you might see the generated HTML when you visit the URL of the AsciiDoc source file. If not accounted for, 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 may end 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 when you want to create a source-to-source reference.
The assignment is hidden behind a check for env-name
, where env-name
is an attribute that is only set in an environment where you need to make this type of reference.
= Document Title
ifdef::env-name[: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 README.html.
This configuration is not actually necessary on GitHub, GitLab, or the browser preview extension since those environments automatically set the value of relfilesuffix to match the file extension of the source file.
However, this setting may still be required for other environments, so it’s worth knowing.
|
Mapping references to a different structure
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).