Guidedog Manual 1.0.0-beta.16
Language
On this page
Guidedog / Documentation 1.0.0-beta.16

Custom directives and dynamic content

Guidedog publishes the same document tree to an interactive HTML website and a typeset PDF book. Web readers benefit from interactive calculators, search filters, and embedded widgets. Book readers benefit from balanced columns, callout cards, and vector typography.

This tutorial builds a project that demonstrates both without duplicating prose. We declare a custom semantic directive in conf.toml, add an interactive calculator for web readers, and design a print-quality callout block for the PDF book.

One source document evaluates object types for both outputs, sending raw HTML to the web and raw Typst to the PDF book.
Fig. 5 Format-gated evaluation and custom directives across HTML and PDF pipelines.

1. Create the project

Start in a clean directory and run:

guidedog quickstart telemetry -q -p "Cluster Telemetry" -a "Operations Team" -v 1.0

Move into the new directory:

cd telemetry

2. Declare custom domain directives in conf.toml

A project often needs structured entities that standard domains (such as Python or C++) do not cover. Rather than requiring a Python runtime or custom extension compiler, Guidedog lets us declare custom directives and roles in conf.toml.

Open conf.toml and append the declaration for a metric directive:

[[object_types]]
directive = "metric"
role = "metric"
index = "pair: %s; cluster telemetry"
name = "first-word"
display = "Metric: %s"

This single declaration establishes a complete syntax contract:

  • .. metric:: p99_latency (milliseconds) describes the metric.
  • :metric:`p99_latency` links to the definition from any document.
  • The index table automatically generates a pair index entry (p99_latency; cluster telemetry).
  • In HTML, Guidedog generates a semantic definition list with CSS anchor hooks.
  • In PDF, Guidedog typesets a description and an index entry linking to its page.

3. Author the document with format guards

Replace the contents of index.rst with the following document:

Cluster Telemetry
=================

We monitor service level indicators across all ingress gateways.
Refer to :metric:`p99_latency` for service level objectives.

.. metric:: p99_latency (milliseconds)

   The 99th percentile response duration measured over a 60-second sliding window.

Interactive estimator
---------------------

.. only:: html

   .. raw:: html

      <div class="telemetry-widget" style="background: #f4f8f6; border: 1px solid #216553; border-radius: 6pt; padding: 14pt; margin: 16pt 0;">
        <label for="rps-slider" style="font-weight: bold; color: #16382f; display: block; margin-bottom: 6pt;">Simulate Request Rate (req/s):</label>
        <input type="range" id="rps-slider" min="500" max="10000" step="100" value="2500" style="width: 100%; max-width: 400pt;"
               oninput="document.getElementById('est-p99').textContent = (0.35 + (this.value / 1000) * 0.42).toFixed(2) + ' ms'">
        <p style="margin-top: 10pt; color: #16382f;">Estimated P99 Latency: <strong id="est-p99" style="color: #216553; font-size: 1.15em;">1.40 ms</strong></p>
      </div>

.. only:: pdf

   .. raw:: typst

      #rect(
        fill: rgb("#edf5f2"),
        stroke: 0.75pt + rgb("#216553"),
        radius: 4pt,
        inset: (x: 12pt, y: 10pt),
        width: 100%
      )[
        #text(weight: "bold", fill: rgb("#216553"))[Telemetry SLA Specification] \
        #v(3pt)
        P99 latency must remain strictly below 2.50 ms under sustained 5,000 req/s load.
        An interactive calculator is available in the web edition.
      ]

4. Build and inspect both publications

Build the HTML website:

guidedog build -b html . _build/html -W

Open _build/html/index.html in a browser or run guidedog serve. Notice that:

  • The :metric:`p99_latency` reference resolves directly to the definition anchor.
  • The interactive calculator slider is rendered and active in the web DOM.
  • The PDF callout block is omitted entirely, without emitting warnings.

Now build the PDF book:

guidedog build -b pdf . _build/pdf -W

Open _build/pdf/cluster-telemetry-en.pdf (or the inspection source at _build/pdf/book-en.typ). Notice that:

  • The slider and raw HTML are completely absent.
  • The Typst #rect(...) vector box is typeset with native margins, borders, and typography.
  • The book’s index includes an entry under p99_latency.

5. Custom Typst functions in theme templates

When we reuse formatting across chapters, we append a Typst function to _templates/theme.typ:

#let status-badge(status, text-content) = {
  let bg = if status == "ok" { rgb("#edf5f2") } else { rgb("#fff9e6") }
  let fg = if status == "ok" { rgb("#216553") } else { rgb("#854d0e") }
  box(fill: bg, inset: (x: 6pt, y: 3pt), radius: 3pt, baseline: 10%)[
    #text(fill: fg, size: 0.85em, weight: "bold")[#text-content]
  ]
}

In any document, invoke the function inside a raw Typst block:

.. only:: pdf

   .. raw:: typst

      #import "/_templates/theme.typ": status-badge
      #status-badge("ok")[SLO COMPLIANT]

Import the function explicitly: the book template’s own imports are scoped to its module and do not expose helpers to document content. For many call sites, place this import in a project preamble selected by pdf_preamble.