This guide explains how to create and customize templates for Microdocs.
Templates are built using Vite, which compiles HTML, CSS (Tailwind), and JavaScript into single-file outputs.
templates_src/{template_name}/
├── {template_name}.html # HTML template with Jinja2 markup
├── {template_name}.css # Tailwind CSS source
└── {template_name}.js # JavaScript source
↓ (build with `npm run build`)
microdocs/templates/{template_name}/
└── {template_name}.html # Single file with all CSS/JS inlined
Important:
- Source files are in
templates_src/at the project root - Built templates are in
microdocs/templates/(distributed with the package) - Never edit files in
microdocs/templates/directly - they are auto-generated! - The build process automatically discovers all template directories
# Install Node.js dependencies
npm install# Start dev server with hot-reloading
npm run dev
# Build all templates for production
npm run build
# Preview built templates
npm run previewLet's create a minimal template called "simple" as an example.
mkdir -p templates_src/simple<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{ title }}</title>
<script type="module" src="./simple.js"></script>
<link rel="stylesheet" href="./simple.css">
</head>
<body>
<header>
<h1>{{ title }}</h1>
</header>
<main>
{% for section in sections %}
<section id="{{ section.id }}">
<h2>{{ section.name }}</h2>
<article>
{{ section.html|safe }}
</article>
</section>
{% endfor %}
</main>
<footer>
<p>Built on {{ build_timestamp }}</p>
</footer>
</body>
</html>@import "../node_modules/tailwindcss/dist/lib.d.mts";
body {
max-width: 800px;
margin: 0 auto;
padding: 2rem;
font-family: system-ui, sans-serif;
}
header {
border-bottom: 2px solid #333;
padding-bottom: 1rem;
margin-bottom: 2rem;
}
section {
margin-bottom: 3rem;
}
footer {
margin-top: 4rem;
padding-top: 2rem;
border-top: 1px solid #ccc;
color: #666;
font-size: 0.875rem;
}// Simple template doesn't need much JavaScript
console.log('Simple template loaded');
// Add smooth scrolling for anchor links
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', (e) => {
e.preventDefault();
const target = document.querySelector(anchor.getAttribute('href'));
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});
});npm run buildThis will create microdocs/templates/simple/simple.html with all CSS and JS inlined.
# Template name is automatically inferred from directory
microdocs README.md -t simple -o output.html
# Or use the full path
microdocs README.md -t templates/simple/simple.html -o output.htmlTemplates have access to the following variables:
| Variable | Type | Description |
|---|---|---|
title |
string | Document title (from first H1 or CLI --title) |
sections |
list | List of sections with id, name, and html attributes |
repo_url |
string | Optional repository URL (from CLI --repo-url) |
build_timestamp |
string | Build timestamp in UTC format |
{
"id": "readme", # Lowercase filename stem
"name": "README", # Original filename stem
"html": "<h1>...</h1>" # Converted HTML content
}If you create a useful template, consider contributing it to the project!
- Create your template in
templates_src/ - Test thoroughly with various content
- Document any special features
- Submit a pull request
Templates should:
- Be responsive
- Support dark mode (optional but recommended)
- Be accessible (semantic HTML, ARIA labels)
- Have minimal dependencies