Skip to content

Commit

Permalink
Support package specific javadoc-location attributes
Browse files Browse the repository at this point in the history
Closes gh-18
  • Loading branch information
philwebb authored and rwinch committed Sep 4, 2024
1 parent 8157a35 commit 2514482
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
22 changes: 21 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,27 @@ For example, you can set `javadoc-location` to `xref:api:java` if you publish ja

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].

You can also override locations on a per-link basis.
===== Package Specific Locations

You can use package specific `javadoc-location` document attributes if you want to support multiple locations.
To set a package specific location, add a `javadoc-location-<package>` document attribute.
The `<package>` should be the java package name with all `.` characters replaced with `-`.

For example, the following will use different locations for `com.example.core` links:

[,asciidoc]
----
= Example
:javadoc-location-com-example-core: {url-example-javadoc}
Please see javadoc:com.example.core.util.MyUtils[]
----

===== Link Specific Locations

You can override locations on a per-link basis by including the location directly in the link.
The embedded link must end with a `/`.

For example:

[,asciidoc]
Expand Down
18 changes: 16 additions & 2 deletions lib/javadoc-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ function parseTarget (target) {
const reference = lastSlash !== -1 ? target.substring(lastSlash + 1, target.length) : target
const lastHash = reference.lastIndexOf('#')
const classReference = lastHash !== -1 ? reference.substring(0, lastHash) : reference
const lastDot = classReference.lastIndexOf('.')
const packageReference = classReference.substring(0, lastDot)
const anchor = lastHash !== -1 ? reference.substring(lastHash + 1, reference.length) : undefined
return { location, classReference, anchor }
return { location, packageReference, classReference, anchor }
}

function process (document, target, attrs) {
Expand All @@ -45,11 +47,23 @@ function process (document, target, attrs) {
}

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

function getTargetLocation (document, target) {
let name = target.packageReference
let lastDot = name.lastIndexOf('.')
while (lastDot > 0) {
const location = document.getAttribute('javadoc-location-' + name.replaceAll('.', '-'))
if (location) return location
name = name.substring(0, lastDot)
lastDot = name.lastIndexOf('.')
}
return document.getAttribute('javadoc-location', 'xref:attachment$api/java')
}

function link (location, target) {
let link = location
link = !link.endsWith('/') ? link + '/' : link
Expand Down
28 changes: 28 additions & 0 deletions test/javadoc-extension-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ describe('javadoc-extension', () => {
)
})

it('should convert with specified location when has package specific javadoc-location attributes', () => {
const input = heredoc`
= Page Title
:javadoc-location: xref:api:java
:javadoc-location-com-example: xref:api:e
:javadoc-location-com-example-one: xref:api:e1
:javadoc-location-com-example-one-two: xref:api:e12
javadoc:org.example.MyClass1[]
javadoc:com.example.one.two.three.MyClass2[]
javadoc:com.example.one.three.three.MyClass3[]
javadoc:com.example.test.MyClass4[]
`
const actual = run(input)
expect(actual).to.include(
'<a href="https://docs.example.com/api/java/org/example/MyClass1.html" class="xref page apiref"><code>MyClass1</code></a>'
)
expect(actual).to.include(
'<a href="https://docs.example.com/api/e12/com/example/one/two/three/MyClass2.html" class="xref page apiref"><code>MyClass2</code></a>'
)
expect(actual).to.include(
'<a href="https://docs.example.com/api/e1/com/example/one/three/three/MyClass3.html" class="xref page apiref"><code>MyClass3</code></a>'
)
expect(actual).to.include(
'<a href="https://docs.example.com/api/e/com/example/test/MyClass4.html" class="xref page apiref"><code>MyClass4</code></a>'
)
})

it('should convert with specified location when has xref location in macro', () => {
const input = heredoc`
= Page Title
Expand Down

0 comments on commit 2514482

Please sign in to comment.