diff options
Diffstat (limited to 'plugins')
3 files changed, 77 insertions, 3 deletions
diff --git a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt index 79e49d53..e73a36b2 100644 --- a/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt +++ b/plugins/base/src/main/kotlin/renderers/html/HtmlRenderer.kt @@ -14,6 +14,7 @@ import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.query import org.jetbrains.dokka.plugability.querySingle import java.io.File +import java.net.URI open class HtmlRenderer( context: DokkaContext @@ -493,6 +494,8 @@ open class HtmlRenderer( } } + private fun resolveLink(link: String, page: PageNode) : String = if (URI(link).isAbsolute) link else page.root(link) + open fun buildHtml(page: PageNode, resources: List<String>, content: FlowContent.() -> Unit) = createHTML().html { head { @@ -500,9 +503,9 @@ open class HtmlRenderer( title(page.name) with(resources) { filter { it.substringBefore('?').substringAfterLast('.') == "css" } - .forEach { link(rel = LinkRel.stylesheet, href = page.root(it)) } + .forEach { link(rel = LinkRel.stylesheet, href = resolveLink(it,page)) } filter { it.substringBefore('?').substringAfterLast('.') == "js" } - .forEach { script(type = ScriptType.textJavaScript, src = page.root(it)) { async = true } } + .forEach { script(type = ScriptType.textJavaScript, src = resolveLink(it,page)) { async = true } } } script { unsafe { +"""var pathToRoot = "${locationProvider.resolveRoot(page)}";""" } } } diff --git a/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt b/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt new file mode 100644 index 00000000..be87813b --- /dev/null +++ b/plugins/base/src/test/kotlin/resourceLinks/ResourceLinksTest.kt @@ -0,0 +1,71 @@ +package resourceLinks + +import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.pages.RootPageNode +import org.jetbrains.dokka.plugability.DokkaPlugin +import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest +import org.jetbrains.dokka.transformers.pages.PageTransformer +import org.jsoup.Jsoup +import org.junit.jupiter.api.Test +import utils.TestOutputWriterPlugin + +class ResourceLinksTest : AbstractCoreTest() { + class TestResourcesAppenderPlugin(val resources: List<String>) : DokkaPlugin() { + class TestResourcesAppender(val resources: List<String>) : PageTransformer { + override fun invoke(input: RootPageNode) = input.transformContentPagesTree { + it.modified( + embeddedResources = it.embeddedResources + resources + ) + } + } + + val appender by extending { + plugin<DokkaBase>().htmlPreprocessors with TestResourcesAppender(resources) + } + } + @Test + fun resourceLinksTest() { + val configuration = dokkaConfiguration { + passes { + pass { + sourceRoots = listOf("src/main/kotlin/test/Test.kt") + } + } + } + val absoluteResources = listOf( + "https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css", + "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" + ) + val relativeResources = listOf( + "test/relativePath.js", + "test/relativePath.css" + ) + + val source = + """ + |/src/main/kotlin/test/Test.kt + |package example + """.trimIndent() + val writerPlugin = TestOutputWriterPlugin() + testInline( + source, + configuration, + pluginOverrides = listOf(TestResourcesAppenderPlugin(absoluteResources + relativeResources), writerPlugin) + ) { + renderingStage = { + root, context -> Jsoup + .parse(writerPlugin.writer.contents["root/example.html"]) + .head() + .select("link, script") + .let { + absoluteResources.forEach { + r -> assert(it.`is`("[href=$r], [src=$r]")) + } + relativeResources.forEach { + r -> assert(it.`is`("[href=../$r] , [src=../$r]")) + } + } + } + } + } +}
\ No newline at end of file diff --git a/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt b/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt index 607d3ced..46ada43a 100644 --- a/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt +++ b/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt @@ -6,7 +6,7 @@ import org.jetbrains.dokka.plugability.DokkaPlugin import java.io.File class TestOutputWriterPlugin(failOnOverwrite: Boolean = true): DokkaPlugin() { - private val writer = TestOutputWriter(failOnOverwrite) + val writer = TestOutputWriter(failOnOverwrite) val testWriter by extending { plugin<DokkaBase>().outputWriter with writer } } |