Improve checker/fixer cohesion and harden workflows

This commit is contained in:
Julien Bisconti
2026-02-28 01:31:37 +01:00
parent e6e23bf1cf
commit c14a071c8d
12 changed files with 279 additions and 85 deletions

View File

@@ -36,12 +36,30 @@ func Build(markdownPath, templatePath, outputPath string) error {
// Inject into template — support both placeholder formats
output := string(tmpl)
replacements := []struct{ old, new string }{
{`<div id="md"></div>`, `<div id="md">` + buf.String() + `</div>`},
{`<section id="md" class="main-content"></section>`, `<section id="md" class="main-content">` + buf.String() + `</section>`},
replacements := []struct {
old string
new string
}{
{
old: `<div id="md"></div>`,
new: `<div id="md">` + buf.String() + `</div>`,
},
{
old: `<section id="md" class="main-content"></section>`,
new: `<section id="md" class="main-content">` + buf.String() + `</section>`,
},
}
replaced := false
for _, r := range replacements {
output = strings.Replace(output, r.old, r.new, 1)
if strings.Contains(output, r.old) {
output = strings.Replace(output, r.old, r.new, 1)
replaced = true
break
}
}
if !replaced {
return fmt.Errorf("template missing supported markdown placeholder")
}
if err := os.WriteFile(outputPath, []byte(output), 0o644); err != nil {

View File

@@ -111,3 +111,23 @@ func TestBuildRealREADME(t *testing.T) {
}
t.Logf("Generated %d bytes", info.Size())
}
func TestBuildFailsWithoutPlaceholder(t *testing.T) {
dir := t.TempDir()
mdPath := filepath.Join(dir, "README.md")
if err := os.WriteFile(mdPath, []byte("# Title\n"), 0o644); err != nil {
t.Fatal(err)
}
tmplPath := filepath.Join(dir, "template.html")
if err := os.WriteFile(tmplPath, []byte("<html><body><main></main></body></html>"), 0o644); err != nil {
t.Fatal(err)
}
outPath := filepath.Join(dir, "index.html")
err := Build(mdPath, tmplPath, outPath)
if err == nil {
t.Fatal("expected Build to fail when template has no supported placeholder")
}
}