Dokka 插件

Dokka was built from the ground up to be easily extensible and highly customizable, which allows the community to implement plugins for missing or very specific features that are not provided out of the box.

Dokka plugins range anywhere from supporting other programming language sources to exotic output formats. You can add support for your own KDoc tags or annotations, teach Dokka how to render different DSLs that are found in KDoc descriptions, visually redesign Dokka’s pages to be seamlessly integrated into your company’s website, integrate it with other tools and so much more.

If you want to learn how to create Dokka plugins, see Developer guides.

Apply Dokka plugins

Dokka plugins are published as separate artifacts, so to apply a Dokka plugin you only need to add it as a dependency. From there, the plugin extends Dokka by itself - no further action is needed.

Plugins that use the same extension points or work in a similar way can interfere with each other. This may lead to visual bugs, general undefined behaviour or even failed builds. However, it should not lead to concurrency issues since Dokka does not expose any mutable data structures or objects.

If you notice problems like this, it’s a good idea to check which plugins are applied and what they do.

Dokka 插件 - 图1

Let’s have a look at how you can apply the mathjax plugin to your project:

【Kotlin】

The Gradle plugin for Dokka creates convenient dependency configurations that allow you to apply plugins universally or for a specific output format only.

  1. dependencies {
  2. // Is applied universally
  3. dokkaPlugin("org.jetbrains.dokka:mathjax-plugin:1.7.20")
  4. // Is applied for the single-module dokkaHtml task only
  5. dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.7.20")
  6. // Is applied for HTML format in multi-project builds
  7. dokkaHtmlPartialPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.7.20")
  8. }

When documenting multi-project builds, you need to apply Dokka plugins within subprojects as well as in their parent project.

Dokka 插件 - 图2

【Groovy】

The Gradle plugin for Dokka creates convenient dependency configurations that allow you to apply Dokka plugins universally or for a specific output format only.

  1. dependencies {
  2. // Is applied universally
  3. dokkaPlugin 'org.jetbrains.dokka:mathjax-plugin:1.7.20'
  4. // Is applied for the single-module dokkaHtml task only
  5. dokkaHtmlPlugin 'org.jetbrains.dokka:kotlin-as-java-plugin:1.7.20'
  6. // Is applied for HTML format in multi-project builds
  7. dokkaHtmlPartialPlugin 'org.jetbrains.dokka:kotlin-as-java-plugin:1.7.20'
  8. }

When documenting multi-project builds, you need to apply Dokka plugins within subprojects as well as in their parent project.

Dokka 插件 - 图3

【Maven】

  1. <plugin>
  2. <groupId>org.jetbrains.dokka</groupId>
  3. <artifactId>dokka-maven-plugin</artifactId>
  4. ...
  5. <configuration>
  6. <dokkaPlugins>
  7. <plugin>
  8. <groupId>org.jetbrains.dokka</groupId>
  9. <artifactId>mathjax-plugin</artifactId>
  10. <version>1.7.20</version>
  11. </plugin>
  12. </dokkaPlugins>
  13. </configuration>
  14. </plugin>

【CLI】

If you are using the CLI runner with command line options, Dokka plugins should be passed as .jar files to -pluginsClasspath:

  1. java -jar dokka-cli-1.7.20.jar \
  2. -pluginsClasspath "./dokka-base-1.7.20.jar;...;./mathjax-plugin-1.7.20.jar" \
  3. ...

If you are using JSON configuration, Dokka plugins should be specified under pluginsClasspath.

  1. {
  2. ...
  3. "pluginsClasspath": [
  4. "./dokka-base-1.7.20.jar",
  5. "...",
  6. "./mathjax-plugin-1.7.20.jar"
  7. ],
  8. ...
  9. }

Configure Dokka plugins

Dokka plugins can also have configuration options of their own. To see which options are available, consult the documentation of the plugins you are using.

Let’s have a look at how you can configure the DokkaBase plugin, which is responsible for generating HTML documentation, by adding a custom image to the assets (customAssets option), by adding custom style sheets (customStyleSheets option), and by modifying the footer message (footerMessage option):

【Kotlin】

Gradle’s Kotlin DSL allows for type-safe plugin configuration. This is achievable by adding the plugin’s artifact to the classpath dependencies in the buildscript block, and then importing plugin and configuration classes:

  1. import org.jetbrains.dokka.base.DokkaBase
  2. import org.jetbrains.dokka.gradle.DokkaTask
  3. import org.jetbrains.dokka.base.DokkaBaseConfiguration
  4. buildscript {
  5. dependencies {
  6. classpath("org.jetbrains.dokka:dokka-base:1.7.20")
  7. }
  8. }
  9. tasks.withType<DokkaTask>().configureEach {
  10. pluginConfiguration<DokkaBase, DokkaBaseConfiguration> {
  11. customAssets = listOf(file("my-image.png"))
  12. customStyleSheets = listOf(file("my-styles.css"))
  13. footerMessage = "(c) 2022 MyOrg"
  14. }
  15. }

Alternatively, plugins can be configured via JSON. With this method, no additional dependencies are needed.

  1. import org.jetbrains.dokka.gradle.DokkaTask
  2. tasks.withType<DokkaTask>().configureEach {
  3. val dokkaBaseConfiguration = """
  4. {
  5. "customAssets": ["${file("assets/my-image.png")}"],
  6. "customStyleSheets": ["${file("assets/my-styles.css")}"],
  7. "footerMessage": "(c) 2022 MyOrg"
  8. }
  9. """
  10. pluginsMapConfiguration.set(
  11. mapOf(
  12. // fully qualified plugin name to json configuration
  13. "org.jetbrains.dokka.base.DokkaBase" to dokkaBaseConfiguration
  14. )
  15. )
  16. }

【Groovy】

  1. import org.jetbrains.dokka.gradle.DokkaTask
  2. tasks.withType(DokkaTask.class) {
  3. String dokkaBaseConfiguration = """
  4. {
  5. "customAssets": ["${file("assets/my-image.png")}"],
  6. "customStyleSheets": ["${file("assets/my-styles.css")}"],
  7. "footerMessage": "(c) 2022 MyOrg"
  8. }
  9. """
  10. pluginsMapConfiguration.set(
  11. // fully qualified plugin name to json configuration
  12. ["org.jetbrains.dokka.base.DokkaBase": dokkaBaseConfiguration]
  13. )
  14. }

【Maven】

  1. <plugin>
  2. <groupId>org.jetbrains.dokka</groupId>
  3. <artifactId>dokka-maven-plugin</artifactId>
  4. ...
  5. <configuration>
  6. <pluginsConfiguration>
  7. <!-- Fully qualified plugin name -->
  8. <org.jetbrains.dokka.base.DokkaBase>
  9. <!-- Options by name -->
  10. <customAssets>
  11. <asset>${project.basedir}/my-image.png</asset>
  12. </customAssets>
  13. <customStyleSheets>
  14. <stylesheet>${project.basedir}/my-styles.css</stylesheet>
  15. </customStyleSheets>
  16. <footerMessage>(c) MyOrg 2022 Maven</footerMessage>
  17. </org.jetbrains.dokka.base.DokkaBase>
  18. </pluginsConfiguration>
  19. </configuration>
  20. </plugin>

【CLI】

If you are using the CLI runner with command line options, use the -pluginsConfiguration option that accepts JSON configuration in the form of fullyQualifiedPluginName=json.

If you need to configure multiple plugins, you can pass multiple values separated by ^^.

  1. java -jar dokka-cli-1.7.20.jar \
  2. ...
  3. -pluginsConfiguration "org.jetbrains.dokka.base.DokkaBase={\"customAssets\": [\"my-image.png\"], \"customStyleSheets\": [\"my-styles.css\"], \"footerMessage\": \"(c) 2022 MyOrg CLI\"}"

If you are using JSON configuration, there exists a similar pluginsConfiguration array that accepts JSON configuration in values.

  1. {
  2. "moduleName": "Dokka Example",
  3. "pluginsConfiguration": [
  4. {
  5. "fqPluginName": "org.jetbrains.dokka.base.DokkaBase",
  6. "serializationFormat": "JSON",
  7. "values": "{\"customAssets\": [\"my-image.png\"], \"customStyleSheets\": [\"my-styles.css\"], \"footerMessage\": \"(c) 2022 MyOrg\"}"
  8. }
  9. ]
  10. }

Notable plugins

Here are some notable Dokka plugins that you might find useful:

NameDescription
Android documentation pluginImproves the documentation experience on Android
Versioning pluginAdds version selector and helps to organize documentation for different versions of your application/library
MermaidJS HTML pluginRenders MermaidJS diagrams and visualizations found in KDocs
Mathjax HTML pluginPretty prints mathematics found in KDocs
Kotlin as Java pluginRenders Kotlin signatures as seen from Java’s perspective

If you are a Dokka plugin author and would like to add your plugin to this list, get in touch with maintainers via Slack or GitHub.