From 2514482182125dbae5ae3d9f2f7b1cde8b56d86e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 12 Aug 2024 10:35:03 +0100 Subject: [PATCH] Support package specific javadoc-location attributes Closes gh-18 --- README.adoc | 22 +++++++++++++++++++++- lib/javadoc-extension.js | 18 ++++++++++++++++-- test/javadoc-extension-test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/README.adoc b/README.adoc index 4f9123c..520d008 100644 --- a/README.adoc +++ b/README.adoc @@ -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-` document attribute. +The `` 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] diff --git a/lib/javadoc-extension.js b/lib/javadoc-extension.js index 05ddd83..4225109 100644 --- a/lib/javadoc-extension.js +++ b/lib/javadoc-extension.js @@ -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) { @@ -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 diff --git a/test/javadoc-extension-test.js b/test/javadoc-extension-test.js index 7a9feaa..f7556a8 100644 --- a/test/javadoc-extension-test.js +++ b/test/javadoc-extension-test.js @@ -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( + 'MyClass1' + ) + expect(actual).to.include( + 'MyClass2' + ) + expect(actual).to.include( + 'MyClass3' + ) + expect(actual).to.include( + 'MyClass4' + ) + }) + it('should convert with specified location when has xref location in macro', () => { const input = heredoc` = Page Title