-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssg.js
More file actions
60 lines (52 loc) · 1.95 KB
/
ssg.js
File metadata and controls
60 lines (52 loc) · 1.95 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
49
50
51
52
53
54
55
56
57
58
59
60
// µStatic - The Tiny SSG
print("\x1b[1;36m--- µStatic Build Process Starting ---\x1b[0m");
var Markdown = {
parse: function(md) {
// Simple regex-based parser
var html = md;
// Headers
html = html.replace(/^### (.*$)/gm, '<h3>$1</h3>');
html = html.replace(/^## (.*$)/gm, '<h2>$1</h2>');
html = html.replace(/^# (.*$)/gm, '<h1>$1</h1>');
// Bold
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
// Links
html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>');
// Code blocks
html = html.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
// Paragraphs (simplified)
html = html.split('\n\n').map(function(p) {
if (p.indexOf('<h') === 0 || p.indexOf('<pre') === 0) return p;
return '<p>' + p + '</p>';
}).join('\n');
return html;
}
};
function build() {
var layout = readFile("layout.html");
if (!layout) {
print("Error: layout.html not found");
return;
}
var files = listFiles("content");
if (!files) {
print("Error: content directory not found");
return;
}
for (var i = 0; i < files.length; i++) {
var filename = files[i];
if (filename.indexOf(".md") === -1) continue;
print("Processing: " + filename);
var content = readFile("content/" + filename);
var htmlBody = Markdown.parse(content);
var finalHtml = layout.replace("{{title}}", filename.replace(".md", ""));
finalHtml = finalHtml.replace("{{body}}", htmlBody);
var outName = "public/" + filename.replace(".md", ".html");
writeFile(outName, finalHtml);
print("Generated: " + outName);
}
}
// Ensure public directory exists (we assume user created it or we'd need mkdir binding)
// For now, let's just run it.
build();
print("\x1b[1;32m--- µStatic Build Complete ---\x1b[0m");