|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | +import { parseMarkdown, exportCompressedMarkdownCSS } from "../index.js"; |
| 4 | + |
| 5 | +const TEMPLATE_HTML = `<!doctype html><html lang="de"><head><meta charset="UTF-8"><title>MiniSite</title><style>__CSS__{}</style></head><body><main id="app" class="md-body"></main><script> const pages = __PAGES__; const app = document.getElementById("app"); function normalizeRoute(route) { route = route.trim(); route = route.replace(/^\/+/, ""); if ( route === "" || route === "/" || route === "index" ) { return "index"; } return route; } function render() { let route = location.hash.slice(1); route = normalizeRoute(route); const html = pages[route] ?? pages["404"] ?? \`<h1>404</h1><a href="#">Home</a>\`; app.innerHTML = html; } window.addEventListener("hashchange", render); window.addEventListener("DOMContentLoaded", render); </script></body></html> |
| 6 | +`; |
| 7 | + |
| 8 | +export function newProj() { |
| 9 | + fs.writeFileSync("index.html", TEMPLATE_HTML); |
| 10 | + |
| 11 | + console.log(`Create new Markdown files! Start with pages/index.md for the start page. Then run sam-cli minisite to compile it.! |
| 12 | +Have fun! |
| 13 | +`) |
| 14 | +} |
| 15 | + |
| 16 | +export function main() { |
| 17 | + const pages = {}; |
| 18 | + |
| 19 | + function walk(dir) { |
| 20 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 21 | + const file = path.join(dir, entry.name); |
| 22 | + |
| 23 | + if (entry.isDirectory()) { |
| 24 | + walk(file); |
| 25 | + continue; |
| 26 | + } |
| 27 | + |
| 28 | + if (!entry.name.endsWith(".md")) |
| 29 | + continue; |
| 30 | + |
| 31 | + const route = path |
| 32 | + .relative("pages", file) |
| 33 | + .replace(/\\/g, "/") |
| 34 | + .replace(/\.md$/, ""); |
| 35 | + |
| 36 | + const md = fs.readFileSync(file, "utf8"); |
| 37 | + |
| 38 | + // 👉 HIER passiert die Compilation |
| 39 | + const html = parseMarkdown(md); |
| 40 | + |
| 41 | + pages[route] = html; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + walk("pages"); |
| 46 | + |
| 47 | + const template = fs.readFileSync("template.html", "utf8"); |
| 48 | + |
| 49 | + const html = template.replace( |
| 50 | + "__PAGES__", |
| 51 | + JSON.stringify(pages) |
| 52 | + ); |
| 53 | + |
| 54 | + const html2 = html.replace( |
| 55 | + "__CSS__{}", |
| 56 | + exportCompressedMarkdownCSS() |
| 57 | + ) |
| 58 | + |
| 59 | + fs.writeFileSync("index.html", html2); |
| 60 | + |
| 61 | + console.log("compiled", Object.keys(pages).length, "pages"); |
| 62 | +} |
0 commit comments