Skip to content

Add diagrams support using Kroki #1599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 22, 2025
Merged

Conversation

theletterf
Copy link
Contributor

This PR adds diagrams support for most docs-as-code diagram types using the Kroki universal API. This has the advantage that it doesn't add overhead to the docs site like MermaidJS does, nor lengthens the build process (it doesn't call a CLI tool like D2). The PR currently uses the free Kroki service, though we could easily self-host Kroki.

Supported formats

Accepts mermaid, d2, graphviz, plantuml, ditaa, erd, excalidraw, nomnoml, pikchr, structurizr, svgbob, vega, vegalite, and wavedrom. Uses mermaid by default.

How it works

  1. Diagram content is compressed and base64url-encoded according to Kroki's requirements.
  2. Diagrams are rendered as SVG images via Kroki, with proper alt text and lazy loading.
  3. Invalid or empty diagrams display a clear error message in the output.

Usage example

::::{diagram} mermaid
flowchart LR
  A[Start] --> B{Decision}
  B -->|Yes| C[Action 1]
  B -->|No| D[Action 2]
  C --> E[End]
  D --> E
::::

Implementation details

  • DiagramBlock.cs: Parses directive, extracts content and type, handles validation.
  • DiagramEncoder.cs: Handles zlib compression and base64url encoding for Kroki compatibility.
  • DiagramViewModel.cs and DiagramView.cshtml: Render SVG images or error messages.
  • DirectiveBlockParser.cs and DirectiveHtmlRenderer.cs: Integrated new directive into the parsing and rendering pipeline.
  • DiagramTests.cs: 16+ unit tests for all supported diagram types and error states.

@theletterf theletterf requested review from a team as code owners July 22, 2025 09:11
@theletterf theletterf self-assigned this Jul 22, 2025
Copy link

github-actions bot commented Jul 22, 2025

🔍 Preview links for changed docs

Copy link
Member

@reakaleek reakaleek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

Nit: can you add the diagram type to the LLM markdown output as type attribute

In diagrams.md you can see that diagrams are rendered as

<diagram>
  digraph G {
    rankdir=LR;
    A -> B -> C;
    A -> C;
    }
</diagram>

I think it would be nice if it was <diagram type="graphviz">

@theletterf
Copy link
Contributor Author

@reakaleek Added in 48d7319 — check it out!

@theletterf theletterf requested a review from reakaleek July 22, 2025 09:44
@theletterf theletterf requested a review from reakaleek July 22, 2025 09:54
@theletterf theletterf enabled auto-merge (squash) July 22, 2025 10:24
@theletterf theletterf merged commit 77b60bd into main Jul 22, 2025
17 checks passed
@theletterf theletterf deleted the theletterf-add-kroki-support branch July 22, 2025 10:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants