docfx Documentation

repository·main·Indexed 26 days ago

https://github.com/dotnet/docfx

A tool for building technical documentation sites, supporting landing pages, Markdown, and API reference documentation for .NET and REST APIs. It features a two-stage process consisting of a metadata step to extract documentation from .NET source code or assemblies and a build step to convert YAML and Markdown into HTML. docfx can be installed as a .NET global tool or integrated as a library via the Docfx.App package.

Tokens
40.9K
Snippets
130
Records
230
Agent score
89%

What's inside docfx

  1. Overview of docfx capabilities

    main
    docfx is an API documentation generator for .NET that supports C# and VB. It extracts triple-slash comments from source code and allows linking additional conceptual files to APIs via specific syntax. It generates complete HTML documentation websites and supports customization through templates (including static HTML and AngularJS-managed sites).
  2. Understand Metadata Items and Identifiers

    main

    In the DocFX metadata format, an Item is the basic unit representing a documentation section (e.g., a namespace, class, or method).

    Identifiers

    • ID: A unique identifier for an item within its parent. IDs are case-sensitive. While whitespaces are allowed, they are not recommended for easier Markdown resolution.
    • UID (Unique Identifier): A globally unique identifier.
      • If the item has no parent, the UID is its ID.
      • If it has a parent, the UID is [ParentUID][Separator][ID].
      • Valid separators are ., :, /, and \.
    • Alias: A non-unique, short name used to reference an item easily in Markdown. An item can have multiple aliases.

    Hierarchy

    Items can be hierarchical. A parent item contains its children via its children property, and a child item points to its parent via its parent property.

  3. Understand the DocFX Template System

    main

    The DocFX template system controls how final output files are rendered. It works by loading data models (produced by Document Processors) and transforming them into output files based on their document type (e.g., conceptual for Markdown, Toc for toc.md).

    Key components:

    • Document Type: Categorizes the input data (e.g., conceptual).
    • Renderer: A Mustache template that transforms a data model into an output file.
    • Preprocessor: A JavaScript file that transforms the input data model into a View Model optimized for the Renderer.
    • View Model: The final data model applied to the Renderer.
  4. Understand docfx documentation capabilities

    main

    Docfx is a documentation generator that supports the following workflows:

    • Metadata Extraction: Extracts language metadata for C# and VB (though VB output is rendered in C# format) and saves it in YAML format.
    • Conceptual Content: Processes plain text, html, and markdown files. It supports CommonMark and most GitHub Flavored Markdown (GFM) syntax, with additional features like file inclusion, cross-referencing, and YAML headers.
    • Output Formats:
      • Static Website: Generates a client-only website suitable for hosting on platforms like GitHub Pages or Azure.
      • PDF: Generates offline documentation in PDF format.
  5. Key features of docfx

    main

    Core Features

    • GitHub Integration: Provides a "View Source" link for APIs that routes directly to the source code on GitHub (requires your API to be pushed to GitHub).
    • Cross-Platform: Provides a DNX version for cross-platform usage.
    • Visual Studio Integration: Can be used within Visual Studio. Note that the official docfx.msbuild NuGet package is currently in a pre-release version.
    • Docfx Flavored Markdown (DFM): Supports DFM for conceptual files. DFM is 100% compatible with GitHub Flavored Markdown (GFM) but adds features such as:
      • File inclusion
      • Cross-referencing
      • YAML headers
  6. Understand the DocFX Document Schema v1.0

    main

    The DocFX Document Schema is a JSON-based media type used to define the structure of DocFX documents. It is designed to provide three primary functions:

    1. Annotation: Defining the structure of a DocFX document.
    2. Validation: Using JSON schema keywords (like type and properties) to validate document data.
    3. Interpretation: Defining how specific properties should be interpreted (e.g., marking a summary property as having a markup interpretation using DocFX Flavored Markdown).

    Schema files should follow the convention of using the .schema.json suffix.

  7. Embed Mermaid Diagrams

    main

    You can embed Mermaid diagrams using standard Markdown code blocks with the mermaid language tag.

    Requirement: Mermaid diagrams are only supported when using the modern template.

    Supported diagram types include flowchart, pie, gantt, journey, and classDiagram.

    flowchart LR
    
    A[Hard] -->|Text| B(Round)
    B --> C{Decision}
    C -->|One| D[Result 1]
    C -->|Two| E[Result 2]
  8. Implement RTF Hyperlink Support in DocFX Plugins

    main

    When developing or extending DocFX plugins to support RTF files, you can implement hyperlink updating so that relative links to .rtf files are automatically converted to links to the generated .html files during the build process.

    To ensure links are correctly processed, the hyperlinks in your source RTF files must follow these rules:

    1. Relative Paths Only: Links must be relative and not rooted.
      • Valid: foo\bar.rtf, ../foobar.rtf
      • Invalid: /foo.rtf, c:\foo\bar.rtf, http://foo.bar/, mailto:foo@bar.foobar
    2. Existing Files: The target file must exist in the project structure.

    Implementation Steps

    1. Prepare the Project

    Open your RTF plugin library project in Visual Studio and configure the following:

    • NuGet Package: Add Docfx.Utility.
    • Framework Assembly References: Add System.Core, System.Web, and System.Xml.Linq.

    2. Update the RTF Document Processor

    In your document processor implementation, you need to implement the following logic:

    • FixLink: A helper method to update the link target (e.g., changing .rtf to .html). Use RelativePath to ensure links are generated correctly.
    • CollectLinksAndFixDocument: A method to traverse the document and apply link fixes.
    • Save: Modify the Save method to report or handle the processed links.

    3. Test and Verify

    1. Build your project.
    2. Copy the resulting .dll to the Plugins folder of your DocFX project.
    3. Create a hyperlink in an .rtf file targeting another .rtf file and save it.
    4. Run the build command:
    docfx build
    1. Verify that the output .html file contains the updated link pointing to the .html version of the target.

    Normalized File Path Concept

    DocFX uses a normalized file path to track links reliably. A normalized path:

    • Starts from the working folder (containing docfx.json) using the ~/ prefix.
    • Contains no ../, ./, or //.
    • Uses / instead of \.
    • Contains no URL encoding and no anchors.
    • Example: ~/foo/bar.rtf
  9. Configure Markdown extensions

    main

    Docfx uses the Markdig engine and supports several extensions by default (Mathematics, Emphasis Extras, Auto Identifiers, Media Links, Pipe Tables, Auto Links, and Emoji).

    To use additional custom extensions, you have two options:

    Option 1: Using Docfx as a NuGet library

    If you are building a custom tool using Docfx.App, configure the BuildOptions in your C# code:

    Option 2: Using docfx.json

    Set the build.markdownEngineProperties.markdigExtensions property in your docfx.json file. Provide a list of extension names (e.g., "Abbreviations", "Footnotes").

    Note: Custom configuration of extensions via the build.markdownEngineProperties.markdigExtensions property is not supported when using certain build modes (refer to documentation for specific constraints).

    {
      "build": {
        "markdownEngineProperties": {
          "markdigExtensions": [
              "Abbreviations",
              "Footnotes"
          ]
        }
      }
    }
  10. Create a custom DocFX plugin

    main

    To extend DocFX with custom file support (e.g., converting .rtf to HTML), you must implement two primary interfaces: IDocumentProcessor and IDocumentBuildStep within a C# class library.

    1. Implementation Requirements

    IDocumentProcessor

    Responsible for:

    • Declaring which file extensions can be handled via GetProcessingPriority.
    • Loading file content into an object model via Load.
    • Saving the model via Save.
    • Providing build steps via the BuildSteps property.
    • Managing links via UpdateHref.

    IDocumentBuildStep

    Responsible for transforming document content via three lifecycle methods:

    • Prebuild: Reconstruct or filter documents (e.g., removing documents based on rules).
    • Build: Transform content (e.g., converting RTF content to HTML).
    • Postbuild: Perform cross-document transformations (e.g., extracting link text from titles).

    2. Project Setup

    • Target net8.0 or later.
    • Add NuGet packages: System.Composition, Docfx.Plugins, and Docfx.Common.
    • Use the [Export] attribute to register your classes with the DocFX plugin system.
  11. Initialize DocFX configuration

    main

    Before automating builds, ensure you have a docfx.json configuration file in your repository. You can generate a default configuration file by running the docfx init command with the -q (quiet) flag. This file defines your input/output directories and build settings.

    docfx init -q