Build a Markdown Static Site Generator
A from-scratch static site generator that reads a folder of Markdown files, renders a shared layout, resolves internal links, and emits a browsable dist directory you can host anywhere.
Project goal
Build a static site generator that turns a directory of Markdown files into a fully linked HTML site with a shared layout, per-page front matter, an index page, and copyable assets, using only the Node standard library.
Final features
- Reads every Markdown file in content/, parses front matter, and renders each to its own HTML page.
- Applies one shared layout with a title, navigation, and footer around every page body.
- Generates an index page that lists posts in reverse chronological order with excerpts.
- Rewrites relative Markdown links into correct output paths and reports broken internal links.
- Copies static assets into dist/ and cleans stale output between builds.
Architecture
A four-stage pipeline: a loader reads and parses content files into page objects, a renderer converts Markdown to HTML and injects it into the layout, an assembler writes routes to dist/ and builds the index, and a link checker validates internal targets across the whole corpus. Each stage is a pure function over page objects so it can be tested without touching disk.
Prerequisites
- Node.js 20 LTS or later
- Comfort with the Node file system APIs and async/await
- A basic understanding of Markdown structure and front matter
Dependencies
- node >=20 — File system, path handling, and the module runner
- vitest ^2.1.0 — Unit tests for the pipeline stages
Directory structure
markdown-ssg/
├── content/
│ ├── index.md
│ └── posts/
│ └── first-post.md
├── src/
│ ├── loader.js
│ ├── markdown.js
│ ├── layout.js
│ ├── build.js
│ └── links.js
├── templates/
│ └── layout.html
├── test/
│ ├── loader.test.js
│ └── build.test.js
└── package.json
Phases
1. Content loader and front matter
Recursively read Markdown files, split YAML-style front matter from the body, and normalise each file into a page object.
Files: src/loader.js
import { readdir, readFile } from 'node:fs/promises';
import { join, relative } from 'node:path';
export async function walk(dir) {
const entries = await readdir(dir, { withFileTypes: true });
const files = [];
for (const e of entries) {
const full = join(dir, e.name);
if (e.isDirectory()) files.push(...(await walk(full)));
else if (e.name.endsWith('.md')) files.push(full);
}
return files;
}
export function parseFrontMatter(raw, slug) {
const m = raw.match(/^---\n([\s\S]*?)\n---\n?/);
const meta = m ? fromYaml(m[1]) : {};
const body = m ? raw.slice(m[0].length) : raw;
return { slug, title: meta.title ?? slug, date: meta.date ?? null, body, meta };
}
export async function loadContent(dir) {
const files = await walk(dir);
const pages = [];
for (const file of files) {
const slug = relative(dir, file).replace(/\\/g, '/').replace(/\.md$/, '');
pages.push(parseFrontMatter(await readFile(file, 'utf8'), slug));
}
return pages;
}
function fromYaml(text) {
const out = {};
for (const line of text.split('\n')) {
const [k, ...rest] = line.split(':');
if (k && rest.length) out[k.trim()] = rest.join(':').trim();
}
return out;
}Expected behavior: Loader tests pass for nested directories, front matter with quoted colons, and files that omit optional fields without throwing.
2. Markdown renderer
Convert the Markdown body into HTML, covering headings, lists, code fences, emphasis, and inline links with correct href output.
Files: src/markdown.js
export function renderMarkdown(md) {
const lines = md.split('\n');
const html = [];
let inCode = false;
let inList = false;
for (const line of lines) {
if (line.startsWith('```')) {
inCode = !inCode;
html.push(inCode ? '<pre><code>' : '</code></pre>');
continue;
}
if (inCode) { html.push(escapeHtml(line)); continue; }
if (line.startsWith('# ')) { html.push(lineHtml('h1', line.slice(2))); continue; }
if (line.startsWith('## ')) { html.push(lineHtml('h2', line.slice(3))); continue; }
if (line.startsWith('- ')) {
if (!inList) { html.push('<ul>'); inList = true; }
html.push('<li>' + inline(line.slice(2)) + '</li>');
continue;
}
if (inList) { html.push('</ul>'); inList = false; }
if (line.trim()) html.push('<p>' + inline(line) + '</p>');
}
if (inList) html.push('</ul>');
return html.join('\n');
}
function inline(text) {
return escapeHtml(text).replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, t, href) => {
return '<a href="' + href + '">' + t + '</a>';
});
}Expected behavior: Rendering a fixture document produces headings, a list, and a code block in order, and links become anchors with the original href preserved.
3. Layout and page assembly
Wrap every rendered body in the shared layout template, substitute title and navigation placeholders, and compute relative links between pages.
Files: src/layout.js
import { readFileSync } from 'node:fs';
export function loadLayout(templatePath) {
return readFileSync(templatePath, 'utf8');
}
export function renderPage(page, templatePath) {
const layout = loadLayout(templatePath);
const body = renderMarkdown(page.body);
const html = layout
.replaceAll('{{title}}', escapeHtml(page.title))
.replaceAll('{{body}}', body)
.replaceAll('{{nav}}', navFor(page));
return { slug: page.slug, html, title: page.title, date: page.date };
}
function navFor(page) {
const home = page.slug.includes('/') ? '../../index.html' : 'index.html';
return '<nav><a href="' + home + '">Home</a></nav>';
}Expected behavior: A rendered page contains exactly one html root, the page title in the head, and navigation links that resolve from the page depth.
4. Output writer and index
Write rendered pages into dist/ at paths derived from slugs, copy static assets, and generate a reverse-chronological index page.
Files: src/build.js
import { mkdir, writeFile, rm, cp } from 'node:fs/promises';
import { dirname, join } from 'node:path';
export async function writeSite(outDir, rendered, pages) {
await rm(outDir, { recursive: true, force: true });
for (const page of rendered) {
const target = join(outDir, page.slug + '.html');
await mkdir(dirname(target), { recursive: true });
await writeFile(target, page.html);
}
const sorted = [...pages].sort((a, b) => (b.date ?? '').localeCompare(a.date ?? ''));
await writeFile(join(outDir, 'index.html'), buildIndex(sorted, rendered));
}
function buildIndex(sorted, rendered) {
const items = sorted.map((p) => '<li><a href="' + p.slug + '.html">' + p.title + '</a></li>');
return '<ul class="index">' + items.join('') + '</ul>';
}Expected behavior: A build writes one HTML file per source page plus index.html, removes stale output from a previous run, and orders the index newest-first.
5. Link integrity checker
Extract internal hrefs from every rendered page and fail the build when a target page or asset is missing from the output set.
Files: src/links.js
export function checkLinks(rendered) {
const known = new Set(rendered.map((p) => p.slug + '.html').concat(['index.html']));
const broken = [];
for (const page of rendered) {
const hrefs = [...page.html.matchAll(/href="([^"]+)"/g)].map((m) => m[1]);
for (const href of hrefs) {
if (/^https?:|^mailto:|^#/.test(href)) continue;
const normalized = normalize(href, page.slug);
if (!known.has(normalized)) broken.push({ from: page.slug, href });
}
}
return broken;
}
function normalize(href, fromSlug) {
const parts = fromSlug.split('/');
parts.pop();
for (const seg of href.split('/')) {
if (seg === '..') parts.pop();
else if (seg !== '.') parts.push(seg);
}
return parts.join('/');
}Expected behavior: A site with one page linking to a nonexistent target reports exactly that pair, and a fully valid site reports zero broken links.
6. CLI wiring and tests
Expose the build through a small CLI, print a summary, exit non-zero on broken links, and cover the stages with unit tests.
Files: src/cli.js, test/build.test.js
import { buildSite } from './build.js';
import { loadContent } from './loader.js';
import { renderPage } from './layout.js';
const args = Object.fromEntries(
process.argv.slice(2).map((a) => a.replace(/^--/, '').split('=')),
);
const pages = await loadContent(args.content ?? 'content');
const rendered = pages.map((p) => renderPage(p, args.template ?? 'templates/layout.html'));
const broken = (await import('./links.js')).checkLinks(rendered);
if (broken.length) {
console.error('broken links:', broken.length);
process.exit(1);
}
const result = await buildSite({
contentDir: args.content ?? 'content',
outDir: args.out ?? 'dist',
templatePath: args.template ?? 'templates/layout.html',
});
console.log('wrote ' + result.pageCount + ' pages to ' + result.outDir);Expected behavior: Running node src/cli.js writes the site and prints a page count; a deliberately broken link makes the same command exit with code 1.
Testing
Run npx vitest. Loader tests cover front matter parsing, missing files, and default titles. Markdown tests cover headings, lists, code fences, and inline links. Build tests write to a temporary directory and assert that every expected file exists, that the index lists posts newest-first, and that a page with a broken internal link is reported instead of silently emitted.
Troubleshooting
Generated pages link to content.md instead of content.html
Normalise every internal link during rendering so the .md suffix is rewritten to .html before the page is written, and keep that rewrite in one helper so the checker validates the same shape the browser will follow.
Repeated builds leave stale pages in dist
Remove the whole output directory at the start of writeSite rather than writing file by file; the clean-then-write order makes the output a function of the current content instead of the history of builds.
The index lists posts in an unpredictable order
Sort by the front matter date with an explicit tie-breaker on slug, so two posts with the same day render in the same order on every machine.
Deployment
Run npm run build and deploy dist/ to any static host. Because the output is plain files with relative links, the same directory works from a subpath or a local file server without configuration changes.
Known limitations
- The renderer covers common Markdown constructs but not full CommonMark edge cases such as reference links or nested block quotes.
- There is no incremental build: every run re-renders the whole site, which is fine up to a few hundred pages.
Project overview
Static site generators sit at a useful intersection: they are simple enough to finish in a weekend and deep enough to teach file pipelines, templating, and link integrity. This project builds one from scratch so that every stage is visible, instead of hiding the interesting parts behind a plugin system.
The generator reads a content directory, renders each Markdown file through a single shared layout, assembles an index, and validates that every internal link points at a page that actually exists. Output is a plain dist/ folder with no runtime, ready for any static host.
The whole pipeline is orchestrated by one function that loads, renders, and writes, which keeps the entry point small enough to read in a single sitting:
import { loadContent } from './loader.js';
import { renderPage } from './layout.js';
import { writeSite } from './build.js';
export async function buildSite({ contentDir, outDir, templatePath }) {
const pages = await loadContent(contentDir);
const rendered = pages.map((page) => renderPage(page, templatePath));
await writeSite(outDir, rendered, pages);
return { pageCount: pages.length, outDir };
}
Everything else in the project is a detail of one of those three calls, which is the property that keeps a growing site generator comprehensible.
Setup
This project deliberately keeps its runtime dependencies at zero so the pipeline stays legible; the only package you add is the test runner.
| Dependency | Version | Why it is here |
|---|---|---|
| node | >=20 | File system, path handling, and the module runner |
| vitest | ^2.1.0 | Unit tests for the pipeline stages |
Create the project and install:
mkdir markdown-ssg && cd markdown-ssg
npm init -y
npm install -D vitest@^2.1.0
Configure the environment in package.json so the build and the tests have separate entry points:
{
"name": "markdown-ssg",
"type": "module",
"scripts": {
"build": "node src/cli.js",
"test": "vitest run"
}
}
Create content/, templates/, and src/ now; each later phase fills one directory, so the layout of the repository mirrors the pipeline.
Phase 1: Content loader and front matter
Recursively read Markdown files, split YAML-style front matter from the body, and normalise each file into a page object. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.
import { readdir, readFile } from 'node:fs/promises';
import { join, relative } from 'node:path';
export async function walk(dir) {
const entries = await readdir(dir, { withFileTypes: true });
const files = [];
for (const e of entries) {
const full = join(dir, e.name);
if (e.isDirectory()) files.push(...(await walk(full)));
else if (e.name.endsWith('.md')) files.push(full);
}
return files;
}
export function parseFrontMatter(raw, slug) {
const m = raw.match(/^---\n([\s\S]*?)\n---\n?/);
const meta = m ? fromYaml(m[1]) : {};
const body = m ? raw.slice(m[0].length) : raw;
return { slug, title: meta.title ?? slug, date: meta.date ?? null, body, meta };
}
export async function loadContent(dir) {
const files = await walk(dir);
const pages = [];
for (const file of files) {
const slug = relative(dir, file).replace(/\\/g, '/').replace(/\.md$/, '');
pages.push(parseFrontMatter(await readFile(file, 'utf8'), slug));
}
return pages;
}
function fromYaml(text) {
const out = {};
for (const line of text.split('\n')) {
const [k, ...rest] = line.split(':');
if (k && rest.length) out[k.trim()] = rest.join(':').trim();
}
return out;
}
Acceptance for this phase: Loader tests pass for nested directories, front matter with quoted colons, and files that omit optional fields without throwing.
Phase 2: Markdown renderer
Convert the Markdown body into HTML, covering headings, lists, code fences, emphasis, and inline links with correct href output. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.
export function renderMarkdown(md) {
const lines = md.split('\n');
const html = [];
let inCode = false;
let inList = false;
for (const line of lines) {
if (line.startsWith('```')) {
inCode = !inCode;
html.push(inCode ? '<pre><code>' : '</code></pre>');
continue;
}
if (inCode) { html.push(escapeHtml(line)); continue; }
if (line.startsWith('# ')) { html.push(lineHtml('h1', line.slice(2))); continue; }
if (line.startsWith('## ')) { html.push(lineHtml('h2', line.slice(3))); continue; }
if (line.startsWith('- ')) {
if (!inList) { html.push('<ul>'); inList = true; }
html.push('<li>' + inline(line.slice(2)) + '</li>');
continue;
}
if (inList) { html.push('</ul>'); inList = false; }
if (line.trim()) html.push('<p>' + inline(line) + '</p>');
}
if (inList) html.push('</ul>');
return html.join('\n');
}
function inline(text) {
return escapeHtml(text).replace(/\[([^\]]+)\]\(([^)]+)\)/g, (_m, t, href) => {
return '<a href="' + href + '">' + t + '</a>';
});
}
Acceptance for this phase: Rendering a fixture document produces headings, a list, and a code block in order, and links become anchors with the original href preserved.
Phase 3: Layout and page assembly
Wrap every rendered body in the shared layout template, substitute title and navigation placeholders, and compute relative links between pages. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.
import { readFileSync } from 'node:fs';
export function loadLayout(templatePath) {
return readFileSync(templatePath, 'utf8');
}
export function renderPage(page, templatePath) {
const layout = loadLayout(templatePath);
const body = renderMarkdown(page.body);
const html = layout
.replaceAll('{{title}}', escapeHtml(page.title))
.replaceAll('{{body}}', body)
.replaceAll('{{nav}}', navFor(page));
return { slug: page.slug, html, title: page.title, date: page.date };
}
function navFor(page) {
const home = page.slug.includes('/') ? '../../index.html' : 'index.html';
return '<nav><a href="' + home + '">Home</a></nav>';
}
Acceptance for this phase: A rendered page contains exactly one html root, the page title in the head, and navigation links that resolve from the page depth.
Phase 4: Output writer and index
Write rendered pages into dist/ at paths derived from slugs, copy static assets, and generate a reverse-chronological index page. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.
import { mkdir, writeFile, rm, cp } from 'node:fs/promises';
import { dirname, join } from 'node:path';
export async function writeSite(outDir, rendered, pages) {
await rm(outDir, { recursive: true, force: true });
for (const page of rendered) {
const target = join(outDir, page.slug + '.html');
await mkdir(dirname(target), { recursive: true });
await writeFile(target, page.html);
}
const sorted = [...pages].sort((a, b) => (b.date ?? '').localeCompare(a.date ?? ''));
await writeFile(join(outDir, 'index.html'), buildIndex(sorted, rendered));
}
function buildIndex(sorted, rendered) {
const items = sorted.map((p) => '<li><a href="' + p.slug + '.html">' + p.title + '</a></li>');
return '<ul class="index">' + items.join('') + '</ul>';
}
Acceptance for this phase: A build writes one HTML file per source page plus index.html, removes stale output from a previous run, and orders the index newest-first.
Phase 5: Link integrity checker
Extract internal hrefs from every rendered page and fail the build when a target page or asset is missing from the output set. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.
export function checkLinks(rendered) {
const known = new Set(rendered.map((p) => p.slug + '.html').concat(['index.html']));
const broken = [];
for (const page of rendered) {
const hrefs = [...page.html.matchAll(/href="([^"]+)"/g)].map((m) => m[1]);
for (const href of hrefs) {
if (/^https?:|^mailto:|^#/.test(href)) continue;
const normalized = normalize(href, page.slug);
if (!known.has(normalized)) broken.push({ from: page.slug, href });
}
}
return broken;
}
function normalize(href, fromSlug) {
const parts = fromSlug.split('/');
parts.pop();
for (const seg of href.split('/')) {
if (seg === '..') parts.pop();
else if (seg !== '.') parts.push(seg);
}
return parts.join('/');
}
Acceptance for this phase: A site with one page linking to a nonexistent target reports exactly that pair, and a fully valid site reports zero broken links.
Phase 6: CLI wiring and tests
Expose the build through a small CLI, print a summary, exit non-zero on broken links, and cover the stages with unit tests. Scoped this tightly, the phase ends with a runnable artifact and a test that proves it, so the next one starts from a known-good base.
import { buildSite } from './build.js';
import { loadContent } from './loader.js';
import { renderPage } from './layout.js';
const args = Object.fromEntries(
process.argv.slice(2).map((a) => a.replace(/^--/, '').split('=')),
);
const pages = await loadContent(args.content ?? 'content');
const rendered = pages.map((p) => renderPage(p, args.template ?? 'templates/layout.html'));
const broken = (await import('./links.js')).checkLinks(rendered);
if (broken.length) {
console.error('broken links:', broken.length);
process.exit(1);
}
const result = await buildSite({
contentDir: args.content ?? 'content',
outDir: args.out ?? 'dist',
templatePath: args.template ?? 'templates/layout.html',
});
console.log('wrote ' + result.pageCount + ' pages to ' + result.outDir);
Acceptance for this phase: Running node src/cli.js writes the site and prints a page count; a deliberately broken link makes the same command exit with code 1.
Testing strategy
Run npx vitest. Loader tests cover front matter parsing, missing files, and default titles. Markdown tests cover headings, lists, code fences, and inline links. Build tests write to a temporary directory and assert that every expected file exists, that the index lists posts newest-first, and that a page with a broken internal link is reported instead of silently emitted.
The most valuable test is the link-integrity one: it walks every rendered page, extracts internal hrefs, and asserts each target exists in the output set, which turns a whole class of broken-navigation regressions into a single failing assertion.
Extending the project
The pipeline accepts new stages cleanly: a syntax-highlighting pass slots between Markdown and layout, an RSS generator consumes the same page objects the index uses, and a tag index is one more grouping of the same data. Because every stage is a pure function over page objects, additions are testable before they are wired in.