Dokka Documentation

repository·master·Indexed 25 days ago

https://github.com/kotlin/dokka

Dokka is an API documentation engine for Kotlin that supports mixed-language projects (Kotlin and Java) and generates documentation in HTML, Markdown, and Javadoc formats. It includes specialized plugins such as the All Modules plugin for multi-module projects and an Android documentation plugin for @hide KDoc tag support. The engine utilizes the analysis-kotlin-api for implementation-agnostic interaction with Kotlin declarations.

Tokens
48K
Snippets
139
Records
254
Agent score
85%

What's inside Dokka

  1. Overview of Dokka documentation engine

    master

    Dokka is an API documentation engine for Kotlin that supports mixed-language projects. It processes both Kotlin's KDoc comments and Java's Javadoc comments to generate documentation.

    Supported output formats include:

    • Modern HTML format
    • Javadoc HTML
    • GFM (GitHub Flavored Markdown)
    • Jekyll Markdown

    You can integrate Dokka into your workflow using Gradle, Maven, or the Command Line Interface (CLI).

  2. Extend Dokka with plugins

    master
    Dokka is highly extensible. You can implement plugins to add missing features or customize the documentation generation process. You can also configure custom plugins to enable additional processing or modifications.
  3. Explore the Custom Dokka Plugin example

    master

    This example project demonstrates how to create and configure a custom Dokka plugin using the Dokka Gradle Plugin (DGPv2) architecture. It is composed of two main parts:

    1. dokka-plugin-hide-internal-api: A subproject containing a custom implementation of the HideInternalApiPlugin.
    2. demo-library: A subproject that demonstrates how to apply and configure the HideInternalApiPlugin to a library.

    For comprehensive instructions on developing plugins, refer to the official Dokka developer documentation.

  4. Understand the Dokka data model pipeline

    master

    Dokka uses a multi-layered abstraction model to transform source code into documentation. This allows developers to extend specific parts of the process (like filtering declarations or changing output formats) without modifying the entire engine.

    The pipeline follows this flow:

    1. Input: Generalization of sources (e.g., Kotlin or Java files).
    2. Documentables: A unified, language-independent tree representation of parsed sources (e.g., classes, functions, packages).
    3. Pages: A universal model representing output pages and their content (e.g., lists, text, code blocks), independent of the final file format.
    4. Output: The mapping of the Pages/Content model to specific visual representations (e.g., HTML, Markdown, or Javadoc).

    By targeting specific layers, you can perform tasks like:

    • Filtering: Modify the Documentables level to make certain annotations or classes invisible.
    • Restructuring: Modify the Pages layer to merge multiple pages (e.g., grouping overloaded methods).
  5. Understand the Documentable Model

    master

    The Documentable model is the core data representation of parsed programming language sources in Dokka. It represents code elements as a collection of trees, where each tree is rooted with a DModule.

    Key characteristics:

    • It is populated by translators from different compiler frontends: DefaultDescriptorToDocumentableTranslator (Kotlin K1), DefaultSymbolToDocumentableTranslator (Kotlin K2), and DefaultPsiToDocumentableTranslator (Java).
    • Elements are organized hierarchically (e.g., DPackage contains DClass, which contains DFunction).
    • In later stages, multiple trees are merged into one using a DocumentableMerger.
  6. Available Dokka plugins

    master

    Dokka is extensible through various plugins that add specific documentation capabilities, such as multi-module support, platform-specific improvements, or additional output formats. Use these plugins to customize your documentation generation process.

    | Plugin project | Description |
    |----------------|-----------------------------------------------------------------------------------------------|
    | [plugin-all-modules-page](plugin-all-modules-page) | Provides the ability to generate multi-module documentation. |
    | [plugin-android-documentation](plugin-android-documentation) | Improves documentation experience on the Android platform. |
    | [plugin-base](plugin-base) | Includes base Dokka features and extensions, along with the built-in HTML format |
    | [plugin-gfm](plugin-gfm) | Provides the ability to generate documentation in `GitHub Flavoured Markdown` format. |
    | [plugin-javadoc](plugin-javadoc) | Provides the ability to generate documentation in Javadoc format. |
    | [plugin-jekyll](plugin-jekyll) | Provides the ability to generate documentation in `Jekyll Flavoured Markdown` format. |
    | [plugin-kotlin-as-java](plugin-kotlin-as-java) | Renders all Kotlin signatures as Java signatures. |
    | [plugin-kotlin-playground-samples](plugin-kotlin-playground-samples) | Makes @sample code blocks interactive and runnable using Kotlin Playground. |
    | [plugin-mathjax](plugin-mathjax) | Allows rendering mathematics in the web pages. |
    | [plugin-templating](plugin-templating) | An internal Dokka plugin that handles HTML template processing. |
    | [plugin-versioning](plugin-versioning) | Provides the ability to host documentation for multiple versions of your library/application. |
  7. Understand the Page Model

    master

    The Page model defines the structure of documentation pages. Each Page corresponds to exactly one output file and is independent of the final output format (e.g., HTML, Markdown). The model is represented as a tree starting from a RootPageNode.

    Key concepts:

    • Subclasses of PageNode: Represent different page types like ModulePage, PackagePage, ClasslikePage, and MemberPage.
    • ContentPage: A specific type of page that contains user-visible content.
    • Renderer Responsibility: The Renderer extension determines the file extension and how the page is physically created.
  8. Use the Analysis: Kotlin symbols implementation

    master

    The analysis-kotlin-symbols project provides a symbols-based implementation for the analysis-kotlin-api (also known as K2 or "the new compiler").

    Warning: This project contains no stable public API and must not be used directly. It should only be accessed via the analysis-kotlin-api package. It is intended to be added as a runtime dependency by the runner.

  9. Parse code comments using the Documentation model

    master

    The Documentation model stores data parsed from code comments (KDocs/Javadocs) and works alongside the Documentable model.

    • DocumentationNode: A container for multiple TagWrapper elements for a specific Documentable.
    • TagWrapper: Describes a specific comment tag or the entire comment description (e.g., @see, @author, @return). Each wrapper can contain DocTag children.
    • DocTag: Describes specific syntax elements (e.g., H1, Strong, CodeBlock). Tags can be deeply nested.
    // Example of DocTag implementations
    data class H1(
        override val children: List<DocTag> = emptyList(),
        override val params: Map<String, String> = emptyMap()
    ) : DocTag()
    
    data class Strong(
        override val children: List<DocTag> = emptyList(),
        override val params: Map<String, String> = emptyMap()
    ) : DocTag()