diff --git a/inject-mermaid.sh b/inject-mermaid.sh new file mode 100755 index 0000000..6d78a22 --- /dev/null +++ b/inject-mermaid.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +# Injects mermaid.js script into HTML files that contain mermaid code blocks. +# Usage: inject-mermaid.sh + +HTML_DIR="${1:-.}" + +inject_mermaid() { + local html_file="$1" + python3 - "$html_file" << 'PYEOF' +import sys + +path = sys.argv[1] +snippet = """\ +""" + +with open(path, encoding='utf-8') as f: + content = f.read() + +# Skip if already injected +if 'mermaid.esm' in content: + sys.exit(0) + +# Skip if no mermaid blocks +if 'language-mermaid' not in content: + sys.exit(0) + +new_content = content.replace('', snippet + '\n', 1) +if new_content == content: + print(f"Warning: not found in {path}", file=sys.stderr) + sys.exit(1) + +with open(path, 'w', encoding='utf-8') as f: + f.write(new_content) +PYEOF +} + +# Find and process all HTML files in the directory +if command -v python3 &>/dev/null; then + for html_file in "$HTML_DIR"/*.html; do + [ -f "$html_file" ] || continue + [ "$(basename "$html_file")" = "index.html" ] && continue + + if grep -q 'language-mermaid' "$html_file" 2>/dev/null; then + if ! grep -q 'mermaid.esm' "$html_file" 2>/dev/null; then + inject_mermaid "$html_file" && echo " Injected mermaid.js into $(basename "$html_file")" + fi + fi + done +else + echo "Warning: python3 not found — skipping mermaid injection" + exit 1 +fi