Swift-DocC Documentation

repository·main·Indexed 23 days ago

https://github.com/swiftlang/swift-docc

A documentation compiler for Swift frameworks and packages used to create reference documentation, tutorials, and long-form content. It provides a `docc` CLI, a Swift-DocC Plugin for SwiftPM, and integration with Xcode. Key features include compiler-verified code snippets via the @Snippet directive, support for appearance-aware images, and a suite of benchmark and testing tools including the `benchmark` and `make-test-bundle` CLI utilities.

Tokens
52.8K
Snippets
152
Records
278
Agent score
77%

What's inside Swift-DocC

  1. Overview of SwiftDocC capabilities

    main

    SwiftDocC is a tool for combining code comments with markup prose to produce structured, semantic documentation. It provides APIs to:

    • Discover documentation inputs.
    • Load catalog content.
    • Parse symbol-graph meta-information.
    • Extract symbol documentation.
    • Pair symbol documentation with external file content.

    SwiftDocC represents compiled documentation as an in-memory model, which can then be converted into a persistable representation for writing to disk.

  2. Overview of DocC

    main

    DocC is a documentation compiler that converts Markdown-based text into rich, interactive documentation for Swift frameworks and packages. It supports two primary types of content:

    1. API Reference Documentation: Generated by adding documentation markup to your Swift source code. This includes cross-symbol linking, term-definition lists, and code listings.
    2. Supplemental Content: Created using documentation markup and specific directives to build interactive tutorials (with coding exercises) or comprehensive articles that explain specific technologies.

    You can preview documentation in its published form during development or host the final output on a website.

  3. Link to symbols and other content in DocC

    main
    DocC allows you to facilitate navigation between different parts of your documentation by creating links to symbols, articles, headings, task groups, and external web content. This enables a cohesive documentation experience where users can jump from high-level descriptions directly to the implementation details or related topics.
  4. Create API documentation using DocC markup

    main
    DocC allows you to generate reference documentation directly from your source code using a custom variant of Markdown called documentation markup. You can add in-source documentation to your APIs via code comments and extension files, or write detailed articles and conceptual guides using this markup. DocC then processes these files to build a complete documentation website.
  5. Understand the Swift-DocC compilation pipeline

    main

    The Swift-DocC compilation process follows a discrete, sequential pipeline consisting of three main phases: Discovery, Analysis and Registration, and Rendering. Understanding these phases helps in organizing documentation catalogs and troubleshooting how symbols and markup are integrated into the final output.

    1. Discovery

    DocC identifies inputs from command-line arguments and the documentation catalog (.docc directory). Discovered inputs include:

    • Markup, tutorials, and assets: Must be inside a .docc directory.
    • Symbol graph files: Can be inside a .docc directory or provided via command-line arguments.
    • Meta information: Can be provided via an optional top-level Info.plist inside a .docc directory or via command-line arguments.
    • Render template customizations: Must be inside a .docc directory.

    2. Analysis and Registration

    DocC builds an in-memory topic graph using the discovered inputs:

    • Symbol Registration: Machine-generated symbol graph files are loaded first. Each symbol becomes a documentation node (a topic).
    • Markup Analysis: Markup files (articles, tutorials) are converted to documents and added to the graph. These can extend existing symbol documentation.
    • External Resolution: Symbols referenced from other frameworks are fetched and added if resolvable.
    • Curation: Authors can manually curate the experience by adding Topics and See Also sections to articles. DocC also performs automatic curation for uncurated symbols and checks for dead links.

    3. Rendering

    DocC converts the in-memory model into a persistable format:

    • Each DocumentationNode is translated into a RenderNode (containing hierarchy, meta info, resolved links, and processed markup).
    • JSONEncodingRenderNodeWriter encodes these nodes into JSON files.
    • The output is written to a file hierarchy (typically under .docc-build) where each JSON file represents a single topic.
  6. Create interactive tutorials with Swift-DocC

    main

    Swift-DocC allows you to create interactive, step-by-step tutorials for your Swift frameworks and packages. These tutorials augment standard reference documentation by guiding users through coding exercises and realistic project scenarios.

    To create a tutorial, you write content in Markdown and use specific DocC directives to define elements such as:

    • Introductions
    • Sections
    • Steps
    • Assessments

    Once written, you run the DocC compiler to transform your Markdown into a rich, interactive learning format.

  7. What is documentation markup in DocC?

    main

    Documentation markup is a custom variant of Markdown used by DocC to provide developer-specific features. It extends standard Markdown to support:

    • Cross-symbol linking: Easily link to specific APIs or symbols within your project.
    • Term-definition lists: Create structured lists of terms and their meanings.
    • Code listings: Embed and format code snippets.
    • Asides: Add callouts or supplementary notes to your content.

    This markup is added directly to your source code or supplemental Markdown files and is processed by the DocC compiler to produce the final documentation site.

  8. What is a Documentation Context

    main

    A DocumentationContext is the in-memory representation of a documentation unit, such as a module, package, or technology. It serves as the central hub for interacting with the documentation model.

    Key responsibilities of a context include:

    • Analyzing bundle file contents and converting them into semantic models.
    • Managing a graph of DocumentationNode objects (where each node represents a single documentation topic).
    • Processing assets like media files or download archives.
    • Resolving links to external documentation sources via ExternalDocumentationSource and resolving external symbols via GlobalExternalSymbolResolver.
    • Providing random access to documentation data, including graph walking and path finding.
  9. Use the @Comment directive for internal notes

    main

    The @Comment directive allows you to include writer notes, reminders, or author comments within your .docc files. These comments are captured during the documentation process but are completely ignored by the DocC renderer. They will not appear in the final, published documentation, making them ideal for internal workflows like marking areas that need more content or adding technical reminders.

    @Comment {
        Add lots more photos of sloths here!
    }
  10. How to perform concurrent work on the DocC model

    main

    DocC operates as a compilation pipeline where each stage's input is the previous stage's output. While work is generally performed serially, you can use specific Collection extensions to perform concurrent work when serial processing becomes a bottleneck.

    Best Practice: The preferred pattern is to call a function from the main queue, perform concurrent operations inside that function, and return the aggregated results. This keeps concurrency scoped locally within the function and manages complexity.

  11. Access diagnostics via DocumentationContext

    main
    When performing documentation operations such as discovery, loading, or processing, diagnostics (warnings and errors) are collected and made available through the DocumentationContext. You can access them via the diagnostics property on the DocumentationContext object. This is essential for integrating DocC output with automation tools or IDEs that need to parse rich diagnostic information.