Skip to content

Commit 2514482

Browse files
philwebbrwinch
authored andcommitted
Support package specific javadoc-location attributes
Closes gh-18
1 parent 8157a35 commit 2514482

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

README.adoc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,27 @@ For example, you can set `javadoc-location` to `xref:api:java` if you publish ja
597597

598598
NOTE: document attributes can be set in your Antora playback under the https://docs.antora.org/antora/latest/playbook/asciidoc-attributes/#attributes-key[attributes key].
599599

600-
You can also override locations on a per-link basis.
600+
===== Package Specific Locations
601+
602+
You can use package specific `javadoc-location` document attributes if you want to support multiple locations.
603+
To set a package specific location, add a `javadoc-location-<package>` document attribute.
604+
The `<package>` should be the java package name with all `.` characters replaced with `-`.
605+
606+
For example, the following will use different locations for `com.example.core` links:
607+
608+
[,asciidoc]
609+
----
610+
= Example
611+
:javadoc-location-com-example-core: {url-example-javadoc}
612+
613+
Please see javadoc:com.example.core.util.MyUtils[]
614+
----
615+
616+
===== Link Specific Locations
617+
618+
You can override locations on a per-link basis by including the location directly in the link.
619+
The embedded link must end with a `/`.
620+
601621
For example:
602622

603623
[,asciidoc]

lib/javadoc-extension.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ function parseTarget (target) {
2929
const reference = lastSlash !== -1 ? target.substring(lastSlash + 1, target.length) : target
3030
const lastHash = reference.lastIndexOf('#')
3131
const classReference = lastHash !== -1 ? reference.substring(0, lastHash) : reference
32+
const lastDot = classReference.lastIndexOf('.')
33+
const packageReference = classReference.substring(0, lastDot)
3234
const anchor = lastHash !== -1 ? reference.substring(lastHash + 1, reference.length) : undefined
33-
return { location, classReference, anchor }
35+
return { location, packageReference, classReference, anchor }
3436
}
3537

3638
function process (document, target, attrs) {
@@ -45,11 +47,23 @@ function process (document, target, attrs) {
4547
}
4648

4749
function createLinkMarkup (document, target, description, attributes) {
48-
const location = target.location || document.getAttribute('javadoc-location', 'xref:attachment$api/java')
50+
const location = target.location || getTargetLocation(document, target)
4951
const text = `${link(location, target)}[${description},role=apiref]`
5052
return { text, opts: { attributes } }
5153
}
5254

55+
function getTargetLocation (document, target) {
56+
let name = target.packageReference
57+
let lastDot = name.lastIndexOf('.')
58+
while (lastDot > 0) {
59+
const location = document.getAttribute('javadoc-location-' + name.replaceAll('.', '-'))
60+
if (location) return location
61+
name = name.substring(0, lastDot)
62+
lastDot = name.lastIndexOf('.')
63+
}
64+
return document.getAttribute('javadoc-location', 'xref:attachment$api/java')
65+
}
66+
5367
function link (location, target) {
5468
let link = location
5569
link = !link.endsWith('/') ? link + '/' : link

test/javadoc-extension-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,34 @@ describe('javadoc-extension', () => {
131131
)
132132
})
133133

134+
it('should convert with specified location when has package specific javadoc-location attributes', () => {
135+
const input = heredoc`
136+
= Page Title
137+
:javadoc-location: xref:api:java
138+
:javadoc-location-com-example: xref:api:e
139+
:javadoc-location-com-example-one: xref:api:e1
140+
:javadoc-location-com-example-one-two: xref:api:e12
141+
142+
javadoc:org.example.MyClass1[]
143+
javadoc:com.example.one.two.three.MyClass2[]
144+
javadoc:com.example.one.three.three.MyClass3[]
145+
javadoc:com.example.test.MyClass4[]
146+
`
147+
const actual = run(input)
148+
expect(actual).to.include(
149+
'<a href="https://docs.example.com/api/java/org/example/MyClass1.html" class="xref page apiref"><code>MyClass1</code></a>'
150+
)
151+
expect(actual).to.include(
152+
'<a href="https://docs.example.com/api/e12/com/example/one/two/three/MyClass2.html" class="xref page apiref"><code>MyClass2</code></a>'
153+
)
154+
expect(actual).to.include(
155+
'<a href="https://docs.example.com/api/e1/com/example/one/three/three/MyClass3.html" class="xref page apiref"><code>MyClass3</code></a>'
156+
)
157+
expect(actual).to.include(
158+
'<a href="https://docs.example.com/api/e/com/example/test/MyClass4.html" class="xref page apiref"><code>MyClass4</code></a>'
159+
)
160+
})
161+
134162
it('should convert with specified location when has xref location in macro', () => {
135163
const input = heredoc`
136164
= Page Title

0 commit comments

Comments
 (0)