build.sh (2683B)
1 #!/bin/bash 2 # Simple build script that converts Markdown docs to static HTML. 3 # This simulates what Docusaurus (or any static-site generator) would produce. 4 # 5 # Usage: ./build.sh [source_dir] [output_dir] 6 # source_dir — directory containing .md files (default: docs) 7 # output_dir — where to write HTML output (default: build) 8 9 set -euo pipefail 10 11 SRC_DIR="${1:-docs}" 12 OUT_DIR="${2:-build}" 13 14 rm -rf "$OUT_DIR" 15 mkdir -p "$OUT_DIR" 16 17 # ── helper: wrap markdown in a minimal HTML page ────────────────────────── 18 md_to_html() { 19 local md_file="$1" 20 local title 21 # Pull the first H1 as the page title, fall back to the filename. 22 title=$(grep -m1 '^# ' "$md_file" | sed 's/^# //' || basename "$md_file" .md) 23 24 cat <<-HEADER 25 <!DOCTYPE html> 26 <html lang="en"> 27 <head> 28 <meta charset="utf-8"> 29 <meta name="viewport" content="width=device-width, initial-scale=1"> 30 <title>${title}</title> 31 <style> 32 body { font-family: system-ui, -apple-system, sans-serif; max-width: 48rem; margin: 2rem auto; padding: 0 1rem; line-height: 1.6; color: #1a1a1a; } 33 h1,h2,h3 { margin-top: 1.5em; } 34 code { background: #f4f4f4; padding: .15em .3em; border-radius: 3px; } 35 pre { background: #f4f4f4; padding: 1em; overflow-x: auto; border-radius: 4px; } 36 a { color: #0969da; } 37 </style> 38 </head> 39 <body> 40 HEADER 41 42 if command -v pandoc &>/dev/null; then 43 pandoc --from=markdown --to=html "$md_file" 44 else 45 # Bare-bones Markdown → HTML using sed (handles headings, bold, 46 # inline code, links, list items, and paragraphs). 47 sed -E \ 48 -e 's|^### (.+)|<h3>\1</h3>|' \ 49 -e 's|^## (.+)|<h2>\1</h2>|' \ 50 -e 's|^# (.+)|<h1>\1</h1>|' \ 51 -e 's|\*\*([^*]+)\*\*|<strong>\1</strong>|g' \ 52 -e 's|`([^`]+)`|<code>\1</code>|g' \ 53 -e 's|\[([^]]+)\]\(([^)]+)\)|<a href="\2">\1</a>|g' \ 54 -e 's|^- (.+)|<li>\1</li>|' \ 55 -e 's|^[0-9]+\. (.+)|<li>\1</li>|' \ 56 -e '/^$/s|.*|<br>|' \ 57 "$md_file" 58 fi 59 60 cat <<-FOOTER 61 </body> 62 </html> 63 FOOTER 64 } 65 66 # ── convert every .md file ──────────────────────────────────────────────── 67 count=0 68 while IFS= read -r -d '' md; do 69 rel="${md#"$SRC_DIR"/}" 70 html_path="$OUT_DIR/${rel%.md}.html" 71 mkdir -p "$(dirname "$html_path")" 72 md_to_html "$md" > "$html_path" 73 echo " ✓ $md → $html_path" 74 count=$((count + 1)) 75 done < <(find "$SRC_DIR" -name '*.md' -print0 | sort -z) 76 77 echo "" 78 echo "Build complete: $count page(s) written to $OUT_DIR/"