Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MathJax rendering bug #2342

Merged
merged 6 commits into from Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions plugins/mathjax/api/mathjax.api
@@ -1,8 +1,16 @@
public final class org/jetbrains/dokka/mathjax/MathjaxPlugin : org/jetbrains/dokka/plugability/DokkaPlugin {
public fun <init> ()V
public final fun getMathjaxTagContentProvider ()Lorg/jetbrains/dokka/plugability/Extension;
public final fun getTransformer ()Lorg/jetbrains/dokka/plugability/Extension;
}

public final class org/jetbrains/dokka/mathjax/MathjaxTagContentProvider : org/jetbrains/dokka/base/transformers/pages/tags/CustomTagContentProvider {
public static final field INSTANCE Lorg/jetbrains/dokka/mathjax/MathjaxTagContentProvider;
public fun contentForBrief (Lorg/jetbrains/dokka/base/translators/documentables/PageContentBuilder$DocumentableContentBuilder;Lorg/jetbrains/dokka/DokkaConfiguration$DokkaSourceSet;Lorg/jetbrains/dokka/model/doc/CustomTagWrapper;)V
public fun contentForDescription (Lorg/jetbrains/dokka/base/translators/documentables/PageContentBuilder$DocumentableContentBuilder;Lorg/jetbrains/dokka/DokkaConfiguration$DokkaSourceSet;Lorg/jetbrains/dokka/model/doc/CustomTagWrapper;)V
public fun isApplicable (Lorg/jetbrains/dokka/model/doc/CustomTagWrapper;)Z
}

public final class org/jetbrains/dokka/mathjax/MathjaxTransformer : org/jetbrains/dokka/transformers/pages/PageTransformer {
public static final field INSTANCE Lorg/jetbrains/dokka/mathjax/MathjaxTransformer;
public fun invoke (Lorg/jetbrains/dokka/pages/RootPageNode;)Lorg/jetbrains/dokka/pages/RootPageNode;
Expand Down
18 changes: 18 additions & 0 deletions plugins/mathjax/src/main/kotlin/MathjaxPlugin.kt
Expand Up @@ -16,6 +16,12 @@ class MathjaxPlugin : DokkaPlugin() {
val transformer by extending {
CoreExtensions.pageTransformer with MathjaxTransformer
}

val mathjaxTagContentProvider by extending {
plugin<DokkaBase>().customTagContentProvider with MathjaxTagContentProvider order {
before(plugin<DokkaBase>().sinceKotlinTagContentProvider)
}
}
}

private const val ANNOTATION = "usesMathJax"
Expand All @@ -34,3 +40,15 @@ object MathjaxTransformer : PageTransformer {
.orEmpty()
.any { (it as? CustomTagWrapper)?.name == ANNOTATION }
}

object MathjaxTagContentProvider : CustomTagContentProvider {

override fun isApplicable(customTag: CustomTagWrapper) = customTag.name == ANNOTATION

override fun DocumentableContentBuilder.contentForDescription(
sourceSet: DokkaConfiguration.DokkaSourceSet,
customTag: CustomTagWrapper
) {
comment(customTag.root, sourceSets = setOf(sourceSet))
}
}
24 changes: 13 additions & 11 deletions plugins/mathjax/src/test/kotlin/MathjaxPluginTest.kt
Expand Up @@ -53,15 +53,15 @@ class MathjaxPluginTest : BaseAbstractTest() {
}
}
}
val math = "a^2 = b^2 + c^2"
val source =
"""
|/src/main/kotlin/test/Test.kt
|package example
| /**
| * @usesMathJax
| *
| * \(\alpha_{out} = \alpha_{dst}\)
| * \(C_{out} = C_{dst}\)
| *
| * \($math\)
| */
| fun test(): String = ""
""".trimIndent()
Expand All @@ -71,14 +71,16 @@ class MathjaxPluginTest : BaseAbstractTest() {
configuration,
pluginOverrides = listOf(writerPlugin, MathjaxPlugin())
) {
renderingStage = {
_, _ -> Jsoup
.parse(writerPlugin.writer.contents["root/example/test.html"])
.head()
.select("link, script")
.let {
assert(it.`is`("[href=$LIB_PATH], [src=$LIB_PATH]"))
}
renderingStage = { _, _ ->
val parsed = Jsoup.parse(writerPlugin.writer.contents["root/example/test.html"])

// Ensure the MathJax CDN is loaded
assert(parsed.select("link, script").`is`("[href=$LIB_PATH], [src=$LIB_PATH]"))

// Ensure the contents are displayed
assert(parsed.select("p").any {
it.text().contains(math)
})
}
}
}
Expand Down