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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
package builder
import (
"fmt"
"html/template"
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"nebbet.no/internal/db"
)
// Builder orchestrates the markdown → HTML build pipeline.
type Builder struct {
ContentDir string
OutputDir string
TemplateDir string
ComponentDir string
LibDir string
MetaDB *db.MetaDB
SearchDB *db.SearchDB
tmpl *template.Template
}
func New(contentDir, outputDir string, meta *db.MetaDB, search *db.SearchDB) *Builder {
return &Builder{
ContentDir: contentDir,
OutputDir: outputDir,
TemplateDir: "templates",
ComponentDir: "components",
LibDir: "lib",
MetaDB: meta,
SearchDB: search,
}
}
// PageData is passed to HTML templates.
type PageData struct {
Title string
Content template.HTML
// ImportMapTag is the full <script type="importmap">…</script> block,
// pre-rendered as safe HTML so the JSON inside is never entity-escaped.
ImportMapTag template.HTML
ComponentScripts []string
Date string
Tags []string
Path string
}
// BuildAll performs a full site build.
func (b *Builder) BuildAll() error {
if err := b.loadTemplates(); err != nil {
return fmt.Errorf("load templates: %w", err)
}
importMap, err := GenerateImportMap(b.LibDir)
if err != nil {
return fmt.Errorf("importmap: %w", err)
}
return filepath.WalkDir(b.ContentDir, func(path string, d os.DirEntry, err error) error {
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".md") {
return err
}
// Skip content/admin/ — served dynamically by the admin HTTP server.
rel, _ := filepath.Rel(b.ContentDir, path)
if strings.HasPrefix(filepath.ToSlash(rel), "admin/") || filepath.ToSlash(rel) == "admin" {
return nil
}
return b.BuildFile(path, importMap)
})
}
// BuildFile converts a single markdown file and updates both databases.
func (b *Builder) BuildFile(mdPath, importMap string) error {
data, err := os.ReadFile(mdPath)
if err != nil {
return err
}
fm, body := ParseFrontmatter(string(data))
if fm.Draft {
fmt.Printf("skip draft: %s\n", mdPath)
return nil
}
htmlBody, err := MarkdownToHTML(body)
if err != nil {
return fmt.Errorf("markdown: %w", err)
}
htmlBody = ProcessComponents(htmlBody)
scripts := FindComponentScripts(htmlBody, b.ComponentDir)
// Derive URL path and output file path from content-relative path.
rel, _ := filepath.Rel(b.ContentDir, mdPath)
urlPath := "/" + filepath.ToSlash(strings.TrimSuffix(rel, ".md"))
// /index → / and /section/index → /section
switch {
case urlPath == "/index":
urlPath = "/"
case strings.HasSuffix(urlPath, "/index"):
urlPath = strings.TrimSuffix(urlPath, "/index")
}
outPath := filepath.Join(b.OutputDir, filepath.FromSlash(
strings.TrimSuffix(filepath.ToSlash(rel), ".md")+".html"))
if err := os.MkdirAll(filepath.Dir(outPath), 0755); err != nil {
return err
}
var importMapTag template.HTML
if importMap != "" {
importMapTag = template.HTML(
"<script type=\"importmap\">" + importMap + "</script>")
}
page := PageData{
Title: fm.Title,
Content: template.HTML(htmlBody),
ImportMapTag: importMapTag,
ComponentScripts: scripts,
Date: fm.Date,
Tags: fm.Tags,
Path: urlPath,
}
tmplName := fm.Layout + ".html"
f, err := os.Create(outPath)
if err != nil {
return err
}
defer f.Close()
if err := b.tmpl.ExecuteTemplate(f, tmplName, page); err != nil {
return fmt.Errorf("template %s: %w", tmplName, err)
}
if err := b.MetaDB.UpsertPage(db.PageMeta{
Path: urlPath,
HTMLPath: outPath,
Title: fm.Title,
Date: fm.Date,
Tags: fm.Tags,
UpdatedAt: time.Now(),
}); err != nil {
return fmt.Errorf("meta db: %w", err)
}
if err := b.SearchDB.IndexPage(db.SearchPage{
Path: urlPath,
Title: fm.Title,
Content: StripHTML(htmlBody),
}); err != nil {
return fmt.Errorf("search db: %w", err)
}
fmt.Printf("built %s → %s\n", mdPath, outPath)
return nil
}
// RemovePage deletes the built HTML and removes the page from both databases.
func (b *Builder) RemovePage(mdPath string) error {
rel, _ := filepath.Rel(b.ContentDir, mdPath)
urlPath := "/" + filepath.ToSlash(strings.TrimSuffix(rel, ".md"))
switch {
case urlPath == "/index":
urlPath = "/"
case strings.HasSuffix(urlPath, "/index"):
urlPath = strings.TrimSuffix(urlPath, "/index")
}
outPath := filepath.Join(b.OutputDir, filepath.FromSlash(
strings.TrimSuffix(filepath.ToSlash(rel), ".md")+".html"))
_ = os.Remove(outPath)
_ = b.MetaDB.DeletePage(urlPath)
_ = b.SearchDB.DeletePage(urlPath)
fmt.Printf("removed %s\n", outPath)
return nil
}
func (b *Builder) loadTemplates() error {
tmpl, err := template.ParseGlob(filepath.Join(b.TemplateDir, "*.html"))
if err != nil {
return err
}
b.tmpl = tmpl
return nil
}
// Watch monitors source directories and rebuilds on changes.
// A 150 ms debounce prevents redundant rebuilds when many files change at once.
func (b *Builder) Watch() error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
// Add all dirs (including nested content subdirs) to watcher.
watchDirs := []string{b.ContentDir, b.TemplateDir, b.ComponentDir, b.LibDir, "styles"}
for _, dir := range watchDirs {
if err := addDirRecursive(watcher, dir); err != nil && !os.IsNotExist(err) {
return err
}
}
fmt.Println("watching for changes — Ctrl+C to stop")
var (
debounce = time.NewTimer(0)
pendingMD = "" // non-empty → rebuild only this file
fullBuild = false
)
<-debounce.C // drain initial tick
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return nil
}
if !event.Has(fsnotify.Write) && !event.Has(fsnotify.Create) && !event.Has(fsnotify.Remove) {
continue
}
// If a new directory appears, start watching it.
if event.Has(fsnotify.Create) {
if info, err := os.Stat(event.Name); err == nil && info.IsDir() {
_ = watcher.Add(event.Name)
}
}
isMD := strings.HasSuffix(event.Name, ".md")
isContentMD := isMD && strings.HasPrefix(
filepath.ToSlash(event.Name),
filepath.ToSlash(b.ContentDir),
)
if isContentMD && !fullBuild {
if event.Has(fsnotify.Remove) {
b.RemovePage(event.Name)
pendingMD = ""
} else if pendingMD == "" {
pendingMD = event.Name
} else if pendingMD != event.Name {
// Multiple different md files → full rebuild.
fullBuild = true
pendingMD = ""
}
} else {
// Templates, styles, components, lib, or multiple md changed.
fullBuild = true
pendingMD = ""
}
debounce.Reset(150 * time.Millisecond)
case <-debounce.C:
importMap, _ := GenerateImportMap(b.LibDir)
if fullBuild {
if err := b.loadTemplates(); err == nil {
_ = b.BuildAll()
}
fullBuild = false
} else if pendingMD != "" {
if err := b.loadTemplates(); err == nil {
_ = b.BuildFile(pendingMD, importMap)
}
pendingMD = ""
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
fmt.Fprintf(os.Stderr, "watch error: %v\n", err)
}
}
}
func addDirRecursive(w *fsnotify.Watcher, root string) error {
return filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
if err != nil {
return nil // skip unreadable entries
}
if d.IsDir() {
return w.Add(path)
}
return nil
})
}
|