docfx Documentation
repository·main·Indexed 26 days ago
https://github.com/dotnet/docfxA 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.
What's inside docfx
- 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).
Understand Metadata Items and Identifiers
mainIn 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
childrenproperty, and a child item points to its parent via itsparentproperty.Understand the DocFX Template System
mainThe 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.,
conceptualfor Markdown,Tocfortoc.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.
- Document Type: Categorizes the input data (e.g.,
Understand docfx documentation capabilities
mainDocfx is a documentation generator that supports the following workflows:
- Metadata Extraction: Extracts language metadata for
C#andVB(thoughVBoutput is rendered inC#format) and saves it inYAMLformat. - 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.
- Metadata Extraction: Extracts language metadata for
Key features of docfx
mainCore 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.msbuildNuGet 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
Understand the DocFX Document Schema v1.0
mainThe 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:
- Annotation: Defining the structure of a DocFX document.
- Validation: Using JSON schema keywords (like
typeandproperties) to validate document data. - Interpretation: Defining how specific properties should be interpreted (e.g., marking a
summaryproperty as having amarkupinterpretation using DocFX Flavored Markdown).
Schema files should follow the convention of using the
.schema.jsonsuffix.Embed Mermaid Diagrams
mainYou can embed Mermaid diagrams using standard Markdown code blocks with the
mermaidlanguage tag.Requirement: Mermaid diagrams are only supported when using the
moderntemplate.Supported diagram types include
flowchart,pie,gantt,journey, andclassDiagram.flowchart LR A[Hard] -->|Text| B(Round) B --> C{Decision} C -->|One| D[Result 1] C -->|Two| E[Result 2]Implement RTF Hyperlink Support in DocFX Plugins
mainWhen developing or extending DocFX plugins to support RTF files, you can implement hyperlink updating so that relative links to
.rtffiles are automatically converted to links to the generated.htmlfiles during the build process.Hyperlink Requirements
To ensure links are correctly processed, the hyperlinks in your source RTF files must follow these rules:
- 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
- Valid:
- 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, andSystem.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.rtfto.html). UseRelativePathto ensure links are generated correctly.CollectLinksAndFixDocument: A method to traverse the document and apply link fixes.Save: Modify theSavemethod to report or handle the processed links.
3. Test and Verify
- Build your project.
- Copy the resulting
.dllto thePluginsfolder of your DocFX project. - Create a hyperlink in an
.rtffile targeting another.rtffile and save it. - Run the build command:
docfx build- Verify that the output
.htmlfile contains the updated link pointing to the.htmlversion 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
- Relative Paths Only: Links must be relative and not rooted.
Configure Markdown extensions
mainDocfx 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 theBuildOptionsin your C# code:Option 2: Using docfx.json
Set the
build.markdownEngineProperties.markdigExtensionsproperty in yourdocfx.jsonfile. Provide a list of extension names (e.g., "Abbreviations", "Footnotes").Note: Custom configuration of extensions via the
build.markdownEngineProperties.markdigExtensionsproperty is not supported when using certain build modes (refer to documentation for specific constraints).{ "build": { "markdownEngineProperties": { "markdigExtensions": [ "Abbreviations", "Footnotes" ] } } }Create a custom DocFX plugin
mainTo extend DocFX with custom file support (e.g., converting
.rtfto HTML), you must implement two primary interfaces:IDocumentProcessorandIDocumentBuildStepwithin 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
BuildStepsproperty. - 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.0or later. - Add NuGet packages:
System.Composition,Docfx.Plugins, andDocfx.Common. - Use the
[Export]attribute to register your classes with the DocFX plugin system.
- Declaring which file extensions can be handled via
Initialize DocFX configuration
mainBefore automating builds, ensure you have a
docfx.jsonconfiguration file in your repository. You can generate a default configuration file by running thedocfx initcommand with the-q(quiet) flag. This file defines your input/output directories and build settings.docfx init -qHighlight lines in Code Snippets
mainDocfx allows you to highlight specific lines or ranges in a code snippet using a custom syntax. The syntax follows the pattern
[!code-language[](path?highlight=line-numbers)].[!code-csharp[](media/Program.cs?highlight=2,5-7,9-)]