add support for page listing

This commit is contained in:
facundoolano 2024-02-14 00:05:55 -03:00
parent 88efad43db
commit cea4ac7b1c
2 changed files with 48 additions and 3 deletions

View file

@ -90,7 +90,7 @@ func (site *Site) loadTemplates(srcDir string) error {
relPath, _ := filepath.Rel(srcDir, path)
templ.Metadata["path"] = relPath
templ.Metadata["url"] = "/" + strings.TrimSuffix(relPath, ".html")
templ.Metadata["dir"] = "/" + filepath.Base(relPath)
templ.Metadata["dir"] = "/" + filepath.Dir(relPath)
// posts are templates that can be chronologically sorted --that have a date.
// the rest are pages.
@ -106,8 +106,12 @@ func (site *Site) loadTemplates(srcDir string) error {
}
} else {
// the index pages should be skipped from the page directory
filename := strings.TrimSuffix(entry.Name(), filepath.Ext(entry.Name()))
if filename != "index" {
site.pages = append(site.pages, templ.Metadata)
}
}
site.Templates[path] = templ
}
return nil

View file

@ -222,7 +222,48 @@ hello world!
}
func TestRenderPagesInDir(t *testing.T) {
// TODO
root, layouts, src := newProject()
defer os.RemoveAll(root)
content := `---
title: "1. hello world!"
---
<p>Hello world!</p>`
file := newFile(src, "01-hello.html", content)
defer os.Remove(file.Name())
content = `---
title: "3. goodbye!"
---
<p>goodbye world!</p>`
file = newFile(src, "03-goodbye.html", content)
defer os.Remove(file.Name())
content = `---
title: "2. an oldie!"
---
<p>oldie</p>`
file = newFile(src, "02-an-oldie.html", content)
defer os.Remove(file.Name())
// add a page (no date)
content = `---
---
<ul>{% for page in site.pages %}
<li><a href="{{ page.url }}">{{page.title}}</a></li>{%endfor%}
</ul>`
file = newFile(src, "index.html", content)
defer os.Remove(file.Name())
site, err := Load(src, layouts)
content, err = site.Render(site.Templates[file.Name()])
assertEqual(t, err, nil)
assertEqual(t, content, `<ul>
<li><a href="/01-hello">1. hello world!</a></li>
<li><a href="/02-an-oldie">2. an oldie!</a></li>
<li><a href="/03-goodbye">3. goodbye!</a></li>
</ul>`)
}
func TestRenderArchiveWithExcerpts(t *testing.T) {