Implementation: DocFx Templates
DocFX uses templates
to transform the structured data model into the final static website.
It is configured in /docfx.json
in the build/template
section:
"template": [
"default",
"modern",
"templates/2sxc", // or "Oqtane" / "cre8magic"
],
It basically uses "stacked" system of settings, HTML, CSS and JavaScript which is used to generate the final documentation.
The idea is that a base template default
provides a lot of defaults, modern
will add Bootstrap 5 and then the project-specific template will add all the customizations, custom logos etc.
This is the basic structure:
default
is the base template to generate everything. It's not in the code, docfx keeps its own copy.modern
is a standard Bootstrap 5 template which builds on the default/templates/2sxc/
contains all the customizations
The default
and modern
templates are provided by DocFX, while the 2sxc
template is custom-made for 2sxc. We won't explain the built-in templates here, but focus on the custom template.
Tip
Sometimes when debugging it's useful to find out what the default templates do,
so you can understand what's happening.
You can tell docfx to output the generated HTML files to a folder
by running docfx template export modern
, so you can inspect them.
You can also find the raw source code for the templates in GitHub.
The /templates/[project]
Folder
If you ever need to do anything here, this is what you need to know:
.../build-toc
has our custom scripts to enhance the C# API TOC (explained later).../layout
contains the HTML Mustache layout files, but only the ones we customized.../public
contains the CSS and JavaScript files which are published by docfx. Do not work in thepublic
folder, as we generate the files from thesrc
folder..../src
contains the TypeScript files which are compiled to thepublic
folder.../toc.json.js
is a special script which enhances the C# API TOC (explained later).../build-toc-specs.js
is a special script which enhances the C# API TOC (explained later)
More info:
Custom Template in this Docs Projects
We try to customize as little as possible, to make updates easier. These are the things we usually customize:
Some changes to the layout - usually in
.../layout/_master_.tmpl
Some magic changes to the C# Namespace TOC - usually in
.../toc.json.js
Some changes to CSS, in
.../src/styles/*.scss
Some additional JS, in
.../src/*.ts
Note that the CSS/JS changes are compiled using WebPack (explained later) and copied to two locations at the same time:
/templates/[project]/public
- where docfx will get them on every build/docs/public
- where the docs are, so we can dev/refresh without running docfx
We try to avoid customizing anything else, as it will make updates harder. Specifically ATM we don't use
- DocFx Transformers
- DocFx Renderers
- DocFx Partials
- The
/templates/2sxc/toc.json.js
is run by docfx to customize the C# API TOC - The
/templates/2sxc/src
folder contains the TypeScript which is compiled with WebPack to the/public
folder- the
/main.ts
file is the main entry point - ... with a special export for docfx
- Which will reconfigure
highlightjs
to support Razor syntax
- Which will reconfigure
- ... and an on-Load watcher
- Which will add a lightbox to all images using Fancybox (installed through NPM)
- ...and gallery functionality
- it will add a version switcher
- it will add a permalink XREF system
- it will add blinking architecture illustrations
- the
- NPM / WebPack are used to make things better
Template Customizations
Special JavaScripts and WebPack
We have some special TypeScript code which enhances the documentation, like the lightbox feature for images.
This is in the /2sxc Docs Generator/templates/2sxc/main.ts
file.
It is built with WebPack and must be started manually to compile. Note that you can run WebPack and make ongoing changes without rebuilding the whole documentation.
Daniel Mettler, @iJungleboy
[MS MVP, Oqtane Core Team]
Content Management Expert, Chief Architect of 2sxc and cre8magic.
Forged in the jungles of Indonesia, lives in Switzerland , loves Oqtane 🩸 & 2sxc 💜.
LinkedIn | Discord: @iJungleboy | Twitter: @iJungleboy | Github: @iJungleboy