Repository navigation
feat(mermaid): add C4 context diagram support #156
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| ## C4 Context | ||
|
|
||
| ```mermaid | ||
| C4Context | ||
| title System Context: Internet Banking | ||
| Enterprise_Boundary(bank, "Big Bank plc") { | ||
| Person(customer, "Personal Banking Customer", "A customer of the bank.") | ||
| System_Boundary(banking, "Internet Banking") { | ||
| System(web, "Internet Banking System", "Shows account information.") | ||
| SystemDb(accounts, "Accounts Database") | ||
| } | ||
| } | ||
| System_Ext(mail, "E-mail System", "The internal Microsoft Exchange system.") | ||
| Rel(customer, web, "Views balances", "HTTPS") | ||
| BiRel(web, accounts, "Reads from and writes to", "SQL/TCP") | ||
| Rel(web, mail, "Sends e-mail using", "SMTP") | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //go:build linux || darwin | ||
|
|
||
| // Package main is generating mermaid C4 context diagram. | ||
| package main | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
|
|
||
| "github.com/nao1215/markdown" | ||
| "github.com/nao1215/markdown/mermaid/c4" | ||
| ) | ||
|
|
||
| // This file is gated by //go:build linux || darwin, so //go:generate is skipped | ||
| // on Windows. To regenerate generated.md on Windows, run under WSL or via CI. | ||
| //go:generate go run main.go | ||
|
Comment on lines
+1
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- tracked files ---'
git ls-files 'doc/c4' 'go.mod' 'go.work' | sed -n '1,120p'
printf '%s\n' '--- generator source ---'
cat -n doc/c4/main.go
printf '%s\n' '--- C4 directory ---'
find doc/c4 -maxdepth 1 -type f -print -exec wc -l {} \;
printf '%s\n' '--- generation directives and related constraints ---'
rg -n --glob '!generated.md' 'go:generate|go:build|mermaid/c4|package main' doc/c4 .Repository: nao1215/markdown Length of output: 11896 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- module metadata ---'
cat -n go.mod | sed -n '1,80p'
printf '%s\n' '--- Windows package selection ---'
GOOS=windows GOARCH=amd64 go list -e -json ./doc/c4 | jq '{Dir,Name,GoFiles,IgnoredGoFiles,Imports,Error}'
printf '%s\n' '--- generation commands discovered for Windows ---'
GOOS=windows GOARCH=amd64 go generate -n ./doc/c4 2>&1 || true
printf '%s\n' '--- native package selection ---'
GOOS=linux GOARCH=amd64 go list -e -json ./doc/c4 | jq '{Dir,Name,GoFiles,IgnoredGoFiles,Imports,Error}'
printf '%s\n' '--- build constraints in imported C4 package ---'
rg -n --glob '*.go' '^//go:build|^// \+build|^package |^import ' mermaid/c4Repository: nao1215/markdown Length of output: 2944 🏁 Script executed: #!/bin/bash
set -eu
for pkg in . ./mermaid/c4 ./doc/c4; do
printf '%s\n' "--- Windows package: $pkg ---"
GOOS=windows GOARCH=amd64 go list -e -json "$pkg" |
jq '{ImportPath,Name,GoFiles,IgnoredGoFiles,Imports,Error}'
done
printf '%s\n' '--- platform-specific files in generator dependencies ---'
rg -n --glob '*.go' '^//go:build|^// \+build' markdown.go mermaid/c4 2>/dev/null || trueRepository: nao1215/markdown Length of output: 1619 Remove the Windows build constraint from On Windows, Go ignores 🤖 Prompt for AI Agents |
||
|
|
||
| func main() { | ||
| f, err := os.Create("generated.md") | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| defer func() { | ||
| if err := f.Close(); err != nil { | ||
| panic(err) | ||
| } | ||
| }() | ||
|
|
||
| diagram := c4.NewDiagram(io.Discard, c4.WithTitle("System Context: Internet Banking")). | ||
| EnterpriseBoundary("bank", "Big Bank plc"). | ||
| Person("customer", "Personal Banking Customer", c4.WithDescription("A customer of the bank.")). | ||
| SystemBoundary("banking", "Internet Banking"). | ||
| System("web", "Internet Banking System", c4.WithDescription("Shows account information.")). | ||
| SystemDb("accounts", "Accounts Database"). | ||
| BoundaryEnd(). | ||
| BoundaryEnd(). | ||
| SystemExt("mail", "E-mail System", c4.WithDescription("The internal Microsoft Exchange system.")). | ||
| Rel("customer", "web", "Views balances", c4.WithTechnology("HTTPS")). | ||
| BiRel("web", "accounts", "Reads from and writes to", c4.WithTechnology("SQL/TCP")). | ||
| Rel("web", "mail", "Sends e-mail using", c4.WithTechnology("SMTP")). | ||
| String() | ||
|
|
||
| err = markdown.NewMarkdown(f, markdown.WithBlockSpacing()). | ||
| H2("C4 Context"). | ||
| CodeBlocks(markdown.SyntaxHighlightMermaid, diagram). | ||
| Build() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # C4 context rendering edge cases | ||
|
|
||
| Every label below holds the punctuation this diagram type can carry. Generated by doc/edgecase/main.go and rendered in continuous integration, so a quoting defect fails the build rather than reaching a diagram. | ||
|
|
||
| ```mermaid | ||
| C4Context | ||
| title title "'#35;#59;[](){}🎉日本語:,*-|%%\ | ||
| Enterprise_Boundary(enterprise, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") { | ||
| Person(person, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x", "before #quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\ after") | ||
| System_Boundary(systems, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x nested") { | ||
| System(system, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x", "before #quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\ after") | ||
| } | ||
| } | ||
| Boundary(outside, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x", "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") { | ||
| System_Ext(external, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") | ||
| } | ||
| Person_Ext(auditor, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") | ||
| SystemDb(db, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") | ||
| SystemQueue(queue, "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") | ||
| Rel(person, system, "before #quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\ after", "x#quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\x") | ||
| BiRel(system, db, "before #quot;'#35;;[](){}<br/>🎉日本語:,*-|%%\ after") | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Make the README example reproduce the linked generated file.
Line 2179 uses default spacing, but
doc/c4/main.goLine 43 usesmarkdown.WithBlockSpacing(). The committeddoc/c4/generated.mdalso contains a blank line after## C4 Context, while both README output blocks omit it.Add
markdown.WithBlockSpacing()to the README example and regenerate the displayed output.Suggested fix
Also applies to: 2194-2229
🤖 Prompt for AI Agents