inter-document-xref.adoc 5.1 KB
Newer Older
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
= 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:

[source]
----
include::example$xref.adoc[tag=base]
----

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

Let's assume the cross reference is defined in the document [.path]_document-a.adoc_.
If the target section is in a separate document, [.path]_document-b.adoc_, the author may be tempted to write:

[source]
----
include::example$xref.adoc[tag=bad]
----

However, this link is coupled to HTML output.
What's worse, if [.path]_document-b.adoc_ is included in the same document as [.path]_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:

[source]
----
include::example$xref.adoc[tag=base-inter]
----

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 [.path]_document-b.adoc_ is included in the same document as [.path]_document-a.doc_.
If not, it will generate a link to [.path]_document-b.html_, intelligently substituting the original file extension with the file extension of the output file.

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

If [.path]_document-b.adoc_ is included in the same document as [.path]_document-a.doc_, then the document will be dropped in the link target and look like the output of a normal cross reference:

[source,html]
----
<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 like GitHub.com 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.

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

74
See the xref:README.adoc[README].
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

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

The links in the generated document will now point to [.path]_README.adoc_ instead of the default, [.path]_README.html_.

TIP: 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 [.path]_filename.html_ becomes [.path]_filename_ (which targets [.path]_filename/index.html_).
However, this is problematic for inter-document cross references.
Any cross reference that resolves to the path [.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:

[source]
----
:relfileprefix: ../
:relfilesuffix: /
----

101
Now, the cross reference `+<<filename.adoc,link text>>+` will resolve to [.path]_../filename_ instead of [.path]_filename.html_.
102
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).