-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRender-MarkdownDiagrams.ps1
More file actions
286 lines (234 loc) · 9.2 KB
/
Render-MarkdownDiagrams.ps1
File metadata and controls
286 lines (234 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<#
.SYNOPSIS
Renders DiagramForge-compatible fenced diagrams in markdown files to SVG.
.DESCRIPTION
Scans markdown files for fenced code blocks labeled `mermaid`, `diagram`,
`diagramforge`, or `conceptual`, renders each block to SVG using DiagramForge,
and writes the outputs to a mirror directory under OutputRoot.
Optionally rewrites markdown to replace fenced diagrams with image references.
By default, rewrite mode preserves the original diagram source inside a
collapsible `<details>` block so the markdown file retains an editable
source-of-truth without risking broken HTML comment syntax.
.PARAMETER RootPath
Root directory to scan for markdown files. Defaults to the repository root.
.PARAMETER OutputRoot
Root directory where rendered SVGs will be written. Defaults to
artifacts/rendered-diagrams relative to the repository root.
.PARAMETER Mode
Render mode:
- project: uses the local CLI project via `dotnet run`
- dnx: uses `dnx --yes DiagramForge.Tool`
.PARAMETER RewriteMarkdown
Rewrites markdown files to replace matching fenced code blocks with rendered
image references.
.PARAMETER SourceHandling
Controls rewrite behavior when -RewriteMarkdown is used:
- details: preserve the original fence in a collapsible <details> block and add an image reference
- remove: replace the original fence with an image reference only
.EXAMPLE
pwsh scripts/Render-MarkdownDiagrams.ps1 -RootPath docs -OutputRoot docs/generated/diagrams
.EXAMPLE
pwsh scripts/Render-MarkdownDiagrams.ps1 -RootPath docs -OutputRoot docs/generated/diagrams -RewriteMarkdown -SourceHandling details
#>
[CmdletBinding()]
param(
[string]$RootPath,
[string]$OutputRoot,
[ValidateSet('project', 'dnx')]
[string]$Mode = 'project',
[switch]$RewriteMarkdown,
[ValidateSet('details', 'remove')]
[string]$SourceHandling = 'details'
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$repoRoot = (Get-Item $PSScriptRoot).Parent.FullName
if (-not $RootPath) {
$RootPath = $repoRoot
}
if (-not $OutputRoot) {
$OutputRoot = Join-Path $repoRoot 'artifacts\rendered-diagrams'
}
$RootPath = [System.IO.Path]::GetFullPath($RootPath)
$OutputRoot = [System.IO.Path]::GetFullPath($OutputRoot)
$supportedFenceLanguages = @('mermaid', 'diagram', 'diagramforge', 'conceptual')
$script:renderedFenceCount = 0
$script:rewrittenFileCount = 0
function Ensure-Directory {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
if (-not (Test-Path -LiteralPath $Path)) {
New-Item -ItemType Directory -Path $Path -Force | Out-Null
}
}
function Get-RelativePath {
param(
[Parameter(Mandatory = $true)]
[string]$BasePath,
[Parameter(Mandatory = $true)]
[string]$TargetPath
)
$baseUri = [System.Uri]((Resolve-Path -LiteralPath $BasePath).Path.TrimEnd([System.IO.Path]::DirectorySeparatorChar) + [System.IO.Path]::DirectorySeparatorChar)
$targetUri = [System.Uri](Resolve-Path -LiteralPath $TargetPath).Path
return [System.Uri]::UnescapeDataString($baseUri.MakeRelativeUri($targetUri).ToString()).Replace('/', [System.IO.Path]::DirectorySeparatorChar)
}
function Get-MarkdownFiles {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
return Get-ChildItem -Path $Path -Recurse -File -Filter '*.md' |
Where-Object {
$_.FullName -notlike "*$([System.IO.Path]::DirectorySeparatorChar).git$([System.IO.Path]::DirectorySeparatorChar)*" -and
$_.FullName -notlike "*$([System.IO.Path]::DirectorySeparatorChar)bin$([System.IO.Path]::DirectorySeparatorChar)*" -and
$_.FullName -notlike "*$([System.IO.Path]::DirectorySeparatorChar)obj$([System.IO.Path]::DirectorySeparatorChar)*" -and
$_.FullName -notlike "$OutputRoot*"
}
}
function Get-DiagramFenceMatches {
param(
[Parameter(Mandatory = $true)]
[string]$Text
)
$pattern = '(?ms)^```(?<lang>[A-Za-z0-9_-]+)\s*\r?\n(?<body>.*?)^```[ \t]*(?:\r?\n|$)'
$matches = [System.Text.RegularExpressions.Regex]::Matches($Text, $pattern)
$result = @()
for ($i = 0; $i -lt $matches.Count; $i++) {
$match = $matches[$i]
$lang = $match.Groups['lang'].Value.ToLowerInvariant()
if ($supportedFenceLanguages -notcontains $lang) {
continue
}
$result += [PSCustomObject]@{
Match = $match
Index = $i + 1
Language = $lang
Body = $match.Groups['body'].Value.TrimEnd("`r", "`n")
}
}
return $result
}
function Invoke-DiagramRender {
param(
[Parameter(Mandatory = $true)]
[string]$InputPath,
[Parameter(Mandatory = $true)]
[string]$OutputPath,
[Parameter(Mandatory = $true)]
[string]$SourceMarkdownPath,
[Parameter(Mandatory = $true)]
[int]$SourceFenceIndex
)
Ensure-Directory -Path (Split-Path -Path $OutputPath -Parent)
if ($Mode -eq 'dnx') {
& dnx --yes DiagramForge.Tool $InputPath --output $OutputPath
}
else {
& dotnet run --project (Join-Path $repoRoot 'src\DiagramForge.Cli') -c Release -- $InputPath --output $OutputPath
}
if ($LASTEXITCODE -ne 0) {
throw "Diagram render failed for fence $SourceFenceIndex in '$SourceMarkdownPath' with exit code $LASTEXITCODE."
}
}
function Get-OutputPathForFence {
param(
[Parameter(Mandatory = $true)]
[string]$MarkdownPath,
[Parameter(Mandatory = $true)]
[int]$FenceIndex
)
$relativeMarkdown = Get-RelativePath -BasePath $RootPath -TargetPath $MarkdownPath
$relativeDirectory = Split-Path -Path $relativeMarkdown -Parent
$fileNameStem = [System.IO.Path]::GetFileNameWithoutExtension($MarkdownPath)
$svgFileName = "$fileNameStem.diagram-$FenceIndex.svg"
if ([string]::IsNullOrWhiteSpace($relativeDirectory)) {
return Join-Path $OutputRoot $svgFileName
}
return Join-Path (Join-Path $OutputRoot $relativeDirectory) $svgFileName
}
function Get-RewriteContent {
param(
[Parameter(Mandatory = $true)]
[pscustomobject]$Fence,
[Parameter(Mandatory = $true)]
[string]$RelativeSvgPath
)
$normalizedPath = $RelativeSvgPath.Replace('\', '/')
$imageLine = ""
if ($SourceHandling -eq 'remove') {
return $imageLine
}
$fenceStart = '```' + $Fence.Language
$fenceEnd = '```'
$originalFence = @(
'<details>',
'<summary>diagram source</summary>',
'',
$fenceStart,
$Fence.Body,
$fenceEnd,
'',
'</details>',
'',
$imageLine
) -join [Environment]::NewLine
return $originalFence
}
function Process-MarkdownFile {
param(
[Parameter(Mandatory = $true)]
[System.IO.FileInfo]$File
)
$raw = Get-Content -LiteralPath $File.FullName -Raw
$fences = Get-DiagramFenceMatches -Text $raw
if ($fences.Count -eq 0) {
return
}
Write-Host "Rendering $($fences.Count) diagram(s) from $($File.FullName)"
$replacements = @()
foreach ($fence in $fences) {
$tempExtension = if ($fence.Language -eq 'mermaid') { '.mmd' } else { '.txt' }
$tempBaseName = [System.IO.Path]::GetFileNameWithoutExtension([IO.Path]::GetRandomFileName())
$tempPath = Join-Path ([IO.Path]::GetTempPath()) ($tempBaseName + $tempExtension)
Set-Content -LiteralPath $tempPath -Value $fence.Body -Encoding UTF8 -NoNewline
try {
$svgPath = Get-OutputPathForFence -MarkdownPath $File.FullName -FenceIndex $fence.Index
Invoke-DiagramRender -InputPath $tempPath -OutputPath $svgPath -SourceMarkdownPath $File.FullName -SourceFenceIndex $fence.Index
$script:renderedFenceCount++
if ($RewriteMarkdown) {
$relativeSvgPath = Get-RelativePath -BasePath $File.Directory.FullName -TargetPath $svgPath
$replacements += [PSCustomObject]@{
Start = $fence.Match.Index
Length = $fence.Match.Length
Replacement = Get-RewriteContent -Fence $fence -RelativeSvgPath $relativeSvgPath
}
}
}
finally {
Remove-Item -LiteralPath $tempPath -Force -ErrorAction SilentlyContinue
}
}
if (-not $RewriteMarkdown -or $replacements.Count -eq 0) {
return
}
$updated = $raw
foreach ($replacement in ($replacements | Sort-Object Start -Descending)) {
$updated = $updated.Remove($replacement.Start, $replacement.Length).Insert($replacement.Start, $replacement.Replacement)
}
if ($updated -ne $raw) {
Set-Content -LiteralPath $File.FullName -Value $updated -Encoding UTF8 -NoNewline
$script:rewrittenFileCount++
}
}
Ensure-Directory -Path $OutputRoot
$markdownFiles = @(Get-MarkdownFiles -Path $RootPath)
Write-Host "Scanning $($markdownFiles.Count) markdown file(s) under $RootPath"
foreach ($file in $markdownFiles) {
Process-MarkdownFile -File $file
}
Write-Host "Rendered $script:renderedFenceCount diagram fence(s)."
if ($RewriteMarkdown) {
Write-Host "Rewrote $script:rewrittenFileCount markdown file(s)."
}