-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathrender-mermaid.sh
More file actions
executable file
·48 lines (41 loc) · 1.34 KB
/
render-mermaid.sh
File metadata and controls
executable file
·48 lines (41 loc) · 1.34 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
#!/usr/bin/env bash
#
# Re-renders all .mmd Mermaid sources to .svg in the on-premises images folder.
#
# Usage (from repo root):
# ./-scripts/render-mermaid.sh
#
# Requirements:
# Node.js (npx downloads @mermaid-js/mermaid-cli automatically)
#
set -euo pipefail
DIAGRAM_DIR="modules/ROOT/images/tinymceai-on-premises"
CONFIG_FILE=$(mktemp)
cat > "$CONFIG_FILE" << 'JSON'
{
"htmlLabels": false,
"flowchart": { "htmlLabels": false, "useMaxWidth": true },
"sequence": { "useMaxWidth": true },
"theme": "default"
}
JSON
trap 'rm -f "$CONFIG_FILE"' EXIT
count=0
for mmd in "$DIAGRAM_DIR"/*.mmd; do
[ -f "$mmd" ] || continue
svg="${mmd%.mmd}.svg"
name=$(basename "$mmd")
printf " Rendering %s\n" "$name"
npx -y @mermaid-js/mermaid-cli -i "$mmd" -o "$svg" \
-c "$CONFIG_FILE" --backgroundColor white 2>/dev/null
# Mermaid outputs width="100%" which has no intrinsic size in <img> tags.
# Replace with the actual pixel width from the viewBox so browsers can
# calculate the correct aspect ratio when the page scales the image.
vb_width=$(grep -o 'viewBox="[^"]*"' "$svg" | head -1 | awk -F'[ "]' '{print $4}')
if [ -n "$vb_width" ]; then
vb_int=$(printf "%.0f" "$vb_width")
perl -i -pe "s/width=\"100%\"/width=\"${vb_int}\"/" "$svg"
fi
count=$((count + 1))
done
printf "\nRendered %d diagrams in %s\n" "$count" "$DIAGRAM_DIR"