MakerFLOSS/build-slides.sh

77 lines
2.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUTPUT_DIR="$REPO_ROOT/slides"
mkdir -p "$OUTPUT_DIR"
# Find all markdown files with marp: true frontmatter
SLIDES=()
while IFS= read -r f; do
SLIDES+=("$f")
done < <(grep -rl "^marp: true" "$REPO_ROOT/docs" --include="*.md" 2>/dev/null || true)
if [ ${#SLIDES[@]} -eq 0 ]; then
echo "No marp presentations found in docs/."
exit 0
fi
echo "Found ${#SLIDES[@]} presentation(s):"
printf ' %s\n' "${SLIDES[@]}"
2026-05-10 12:21:09 +02:00
if command -v marp &>/dev/null; then
marp --html --output "$OUTPUT_DIR/" "${SLIDES[@]}"
else
echo "marp not found locally — using Docker (marpteam/marp-cli)..."
REL_SLIDES=()
for f in "${SLIDES[@]}"; do
REL_SLIDES+=("${f#"$REPO_ROOT"/}")
done
docker run --rm \
-v "$REPO_ROOT":/home/marp/app:ro \
-v "$OUTPUT_DIR":/home/marp/output \
marpteam/marp-cli --html --output /home/marp/output "${REL_SLIDES[@]}"
fi
# Inject mermaid.js into any HTML that contains mermaid code blocks.
# Marp emits fenced mermaid blocks as <pre><code class="language-mermaid">.
# The script finds those elements, replaces them with <div class="mermaid">,
# then loads and runs mermaid.js from CDN.
inject_mermaid() {
local html_file="$1"
python3 - "$html_file" << 'PYEOF'
import sys
path = sys.argv[1]
snippet = """\
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
document.querySelectorAll('code.language-mermaid').forEach(el => {
const div = document.createElement('div');
div.className = 'mermaid';
div.textContent = el.textContent;
el.closest('pre').replaceWith(div);
});
mermaid.initialize({ startOnLoad: false, theme: 'dark' });
await mermaid.run();
</script>"""
content = open(path).read()
open(path, 'w').write(content.replace('</body>', snippet + '\n</body>', 1))
PYEOF
}
if command -v python3 &>/dev/null; then
for html_file in "$OUTPUT_DIR"/*.html; do
[ -f "$html_file" ] || continue
if grep -q 'class="language-mermaid"' "$html_file"; then
inject_mermaid "$html_file"
echo " Injected mermaid.js into $(basename "$html_file")"
fi
done
else
echo "Warning: python3 not found — skipping mermaid injection"
fi
echo "Done — slides in $OUTPUT_DIR/"