Build a docs site

See how this docs site is built with Bevel, from Markdown files and a build-time processor to Pagefind search and GitHub Pages.

  • Since 0.3
On this page
bash
dotnet watch --project docs/Bevel.Docs     # live preview
./eng/docs-build.sh                        # build, export, index, check links

You are reading a Bevel site. Its source is docs/Bevel.Docs. This page shows how it works, so you can build a docs site of your own the same way.

The whole site is a Bevel app with no special tooling. Markdown files are a dataset. One processor turns them into pages. A few routes show them.

The content model

Every page is a Markdown file under Data/Docs/. One record describes it.

Data/DocPage.cscsharp
[DataSource("Docs", "Data/Docs/**/*.md")]
[DependsOn("Diagnostics")]
[DependsOn("Options")]
[PostProcess<DocsBuild>]
public sealed record DocPage(
    string Title,
    string Description,
    string Slug,
    string SourcePath,
    string RawMarkdown,
    int Order = 0,
    string? Since = null,
    // ... members the processor fills in
    string Kind = DocKind.Page,
    string Section = "",
    string Url = "",
    string BodyHtml = "");

The front matter fills the first block of members. Everything after Order is derived at build time. The folder is the section, and the file name is the slug, so you never write either. CONTRIBUTING-DOCS.md lists the front matter and every extension.

Two more datasets feed the reference pages:

  • Data/Options/*.yml holds the options and MSBuild tables.
  • Data/Diagnostics/diagnostics.yml holds one entry per BEVELnnn code.

DocPage depends on both with [DependsOn], so they load first.

The build-time processor

[PostProcess<DocsBuild>] runs once, after every page is loaded. It sees the whole list, so it can do work no single page can.

  1. Derive the structure

    It reads each SourcePath and sets the kind (home, section, folder, page), section, slug, URL and edit link. A page outside a section, a duplicate URL, or a section with no _index.md is reported as an error.

  2. Add generated pages

    It adds one page for each diagnostic code, so BEVEL012 lives at /reference/diagnostics/bevel012/ with no .md file. Bevel's own error messages link to those URLs.

  3. Render the Markdown

    It renders every page's RawMarkdown to HTML with a shared Markdig pipeline. It collects the h2 and h3 headings for the "On this page" list.

  4. It computes previous and next links along the reading order, across sections.

The processor reports problems with BevelLoadContext, using DOCS### codes. They print in dotnet build with a file and line.

Code Problem
DOCS001 Description longer than 160 characters (warning).
DOCS002 Empty title or description.
DOCS003 Pages nested more than two folders deep.
DOCS004 A page outside a section folder.
DOCS005 Two pages with one URL.
DOCS006 A section or folder with no _index.md.

The same processor reads files outside Data/. A snippet include like <<< @/samples/... goes through ctx.ReadAllText, which registers the file as a build input. Edit the sample and the data step runs again.

Markdown extensions

The pipeline is Markdig's advanced extensions, YAML front matter, alert blocks and custom containers, plus one class for each site feature in Data/Markdown/Extensions. Bevel finds them by reflection, so adding one means adding a file.

Extension What it gives you
Alerts > [!NOTE] and four more kinds.
Steps, cards, tabs ::: steps, ::: cards, ::: tabs.
Code blocks Title, line highlights, line numbers and diffs on a fence.
Snippet includes <<< @/path#region pulls real code from the repo.
Tree blocks ```tree draws a file tree.
Cross-references [[BEVEL012]] and <xref:uid>. A broken link fails the build.
Badges :since[0.3] and :badge[...].
Components Allow-listed tags such as <OptionsTable />.

An unknown alert, container or component fails the build on purpose. See Markdown extensions for how to write your own.

Allow-listed components

A Markdown page can embed a Razor component. Only the names in ComponentRegistry work. Each entry names the component and its parameters.

Data/Markdown/ComponentRegistry.cscsharp
new("OptionsTable", Ns + "OptionsTable",
    [new("For", ComponentParamType.String, true, ["BevelOptions", "MSBuild"])]),

The registry checks the names and values at build time. PageBody.razor then renders the page as HTML segments with the real components in between. To add a component, add one entry and one Razor file in Components/Docs/.

Routes

Program.cs maps four routes. They cover the home page, section landings, and pages one or two folders deep.

Program.cscsharp
app.MapStatic<HomePage>("/");
app.MapStatic<SectionLanding>("/{Section}", (ISiteData data) => data.Docs.Where(static p => p.Kind == DocKind.Section));
app.MapStatic<DocArticle>("/{Section}/{Slug}", (ISiteData data) => data.Docs.Where(static p => p is { Kind: DocKind.Page, Folder: "" }));
app.MapStatic<DocArticle>("/{Section}/{Folder}/{Slug}", (ISiteData data) => data.Docs.Where(static p => p is { Kind: DocKind.Page, Folder.Length: > 0 }));

The site also sets two options for GitHub Pages:

Program.cscsharp
options.PathBase = builder.Environment.IsDevelopment() ? "" : "/bevel";
options.OutputPath = "../../artifacts/docs-site";

Under dotnet watch the site lives at /. In an export it lives at /bevel/.

The code block contract

The build emits a fixed HTML shape for every code block. The script reads that shape and adds the highlighting, line numbers and the copy button. If the script does not run, you still get readable code.

html
<figure class="code-block" data-lang="csharp">
  <figcaption class="code-block__header">…</figcaption>
  <pre class="code-block__pre" data-line-numbers data-highlight="3-5">
    <code class="language-csharp">…</code>
  </pre>
</figure>

The browser does the coloring with MicroLighter. See Highlight code.

Pagefind indexes the exported HTML. The article element carries data-pagefind-body, and the breadcrumb and other chrome carry data-pagefind-ignore. A search modal in wwwroot/js/search.js loads the index on demand. See Search your site.

The build pipeline

eng/docs-build.sh runs four steps in order.

eng/docs-build.shbash
dotnet build "$PROJ" -c Release --nologo -v q
dotnet run --project "$PROJ" -c Release --no-build -- --bevel-build
"$ROOT/eng/pagefind.sh" "$SITE"
"$ROOT/eng/link-check.sh" "$SITE"
  1. Build. The data step reads the Markdown and fails on any DOCS or BEVEL error.
  2. Export. --bevel-build renders every page into artifacts/docs-site.
  3. Index. Pagefind writes artifacts/docs-site/pagefind/.
  4. Check. An offline link checker, lychee, follows every internal link and fragment with the /bevel/ path base in place.

Host it on GitHub Pages

A project site is served at https://<user>.github.io/<repo>/. That is why PathBase is /bevel and why Docs:SiteUrl is https://khalidabuhakmeh.github.io/bevel/. The artifacts/docs-site folder is the root of the site.

.github/workflows/docs.ymlyaml
name: docs
on:
  push:
    branches: [main]
permissions:
  contents: read
  pages: write
  id-token: write
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment:
      name: github-pages
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
      - uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0
        with:
          dotnet-version: 10.0.x
      - run: ./eng/docs-build.sh
      - uses: actions/upload-pages-artifact@fc324d3547104276b827a68afc52ff2a11cc49c9 # v5.0.0
        with:
          path: artifacts/docs-site
      - uses: actions/deploy-pages@368f82528645a54fb793d4d04e342629a3f51346 # v5.0.1

See Publish to GitHub Pages for the full hosting steps.