Repos / s4g / 4dc9bc254b
commit 4dc9bc254bae1b7d30651316f38b938a0a217c4c
Author: Nhân <hi@imnhan.com>
Date: Thu Jul 13 05:56:55 2023 +0700
various fixes for new empty site
- Cope with empty feed
- Fix unmarshalling []string for metadata
- Fix Root trimming logic
diff --git a/docs/_theme/home.tmpl b/docs/_theme/home.tmpl
index b777357..d85797f 100644
--- a/docs/_theme/home.tmpl
+++ b/docs/_theme/home.tmpl
@@ -21,6 +21,7 @@
{{.Content}}
+{{if .ArticlesInFeed -}}
<p>All posts, newest first:</p>
<ul>
@@ -34,6 +35,9 @@
{{- end}}
{{- end}}
</ul>
+{{else}}
+<p>No posts yet.</p>
+{{end}}
<style>
.site-title {
diff --git a/docs/index.html b/docs/index.html
index 36b2cbc..839474d 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -45,6 +45,7 @@ <h1 class="site-title">CoolZone</h1>
</li>
</ul>
+
<style>
.site-title {
margin-bottom: 0;
diff --git a/main.go b/main.go
index 18d09c7..6463ca2 100644
--- a/main.go
+++ b/main.go
@@ -165,12 +165,14 @@ func regenerate(fsys writablefs.FS) (site SiteMetadata) {
}
fmt.Printf("Processed %d articles\n", len(articles))
- fsys.WriteFile(
- FeedPath,
- generateFeed(site, articlesInFeed, site.Root+FeedPath),
- )
- generatedFiles[FeedPath] = true
- fmt.Println("Generated", FeedPath)
+ if len(articlesInFeed) > 0 {
+ fsys.WriteFile(
+ FeedPath,
+ generateFeed(site, articlesInFeed, site.Root+FeedPath),
+ )
+ generatedFiles[FeedPath] = true
+ fmt.Println("Generated", FeedPath)
+ }
DeleteOldGeneratedFiles(fsys, generatedFiles)
WriteManifest(fsys, generatedFiles)
diff --git a/makesite.go b/makesite.go
index 789d243..a45415f 100644
--- a/makesite.go
+++ b/makesite.go
@@ -30,6 +30,14 @@ func makeSite(path string, meta SiteMetadata) error {
// Copy default theme into new site
copyTheme(defaultTheme, path)
+ // Write default index page
+ indexData := []byte(`Title: Home
+ShowInFeed: false
+Templates: $_theme/base.tmpl, $_theme/includes.tmpl, $_theme/home.tmpl
+---
+`)
+ err = ioutil.WriteFile(filepath.Join(path, "index.dj"), indexData, 0664)
+
return nil
}
diff --git a/metadata.go b/metadata.go
index 9f77e96..a047d37 100644
--- a/metadata.go
+++ b/metadata.go
@@ -36,8 +36,9 @@ type ArticleMetadata struct {
func NewSiteMetadata() SiteMetadata {
return SiteMetadata{
- Root: "/",
- ShowFooter: true,
+ Root: "/",
+ ShowFooter: true,
+ NavbarLinks: []string{"index.dj"},
}
}
@@ -52,7 +53,12 @@ func ReadSiteMetadata(fsys writablefs.FS) SiteMetadata {
UnmarshalMetadata(data, &sm)
// normalize root path to always include leading & trailing slashes
- sm.Root = fmt.Sprintf("/%s/", strings.Trim(sm.Root, "/"))
+ trimmed := strings.Trim(sm.Root, "/")
+ if trimmed == "" {
+ sm.Root = "/"
+ } else {
+ sm.Root = fmt.Sprintf("/%s/", trimmed)
+ }
return sm
}
@@ -124,7 +130,16 @@ func MarshalMetadata(v any) []byte {
f := s.Field(i)
key := sType.Field(i).Name
val := f.Interface()
- result += fmt.Sprintf("%s: %v\n", key, val)
+
+ var repr string
+ switch f.Type().String() {
+ case "[]string":
+ repr = strings.Join(val.([]string), ", ")
+ default:
+ repr = fmt.Sprintf("%v", val)
+ }
+
+ result += fmt.Sprintf("%s: %s\n", key, repr)
}
return []byte(result)
diff --git a/theme/home.tmpl b/theme/home.tmpl
index b777357..d85797f 100644
--- a/theme/home.tmpl
+++ b/theme/home.tmpl
@@ -21,6 +21,7 @@
{{.Content}}
+{{if .ArticlesInFeed -}}
<p>All posts, newest first:</p>
<ul>
@@ -34,6 +35,9 @@
{{- end}}
{{- end}}
</ul>
+{{else}}
+<p>No posts yet.</p>
+{{end}}
<style>
.site-title {