fast and minimal static site generator
ssg

Add a --drafts flag to include drafts while building

anirudh.fi 81b8a3fa cf6acc3b

verified
+27 -17
+12 -9
commands/build.go
··· 81 81 82 82 // Build is the core builder function. Converts markdown/yaml 83 83 // to html, copies over non-.md/.yaml files, etc. 84 - func Build() error { 84 + func Build(drafts bool) error { 85 85 if err := preBuild(); err != nil { 86 86 return err 87 87 } ··· 96 96 return err 97 97 } 98 98 99 - if err := pages.ProcessFiles(); err != nil { 99 + if err := pages.ProcessFiles(drafts); err != nil { 100 100 return err 101 101 } 102 102 103 - if err := pages.ProcessDirectories(); err != nil { 103 + if err := pages.ProcessDirectories(drafts); err != nil { 104 104 return err 105 105 } 106 106 ··· 117 117 118 118 // ProcessFiles handles root level files under 'pages', 119 119 // for example: 'pages/_index.md' or 'pages/about.md'. 120 - func (p *Pages) ProcessFiles() error { 120 + func (p *Pages) ProcessFiles(drafts bool) error { 121 121 for _, f := range p.Files { 122 122 var htmlDir string 123 123 if f.Basename() == "_index.md" { ··· 134 134 return err 135 135 } 136 136 } 137 - if err := f.Render(destFile, nil); err != nil { 137 + if err := f.Render(destFile, nil, drafts); err != nil { 138 138 return fmt.Errorf("error: failed to render %s: %w", destFile, err) 139 139 } 140 140 } ··· 143 143 144 144 // ProcessDirectories handles directories of posts under 'pages', 145 145 // for example: 'pages/photos/foo.md' or 'pages/blog/bar.md'. 146 - func (p *Pages) ProcessDirectories() error { 146 + func (p *Pages) ProcessDirectories(drafts bool) error { 147 147 for _, dir := range p.Dirs { 148 148 dstDir := filepath.Join(types.BuildDir, dir.Name) 149 149 if err := os.MkdirAll(dstDir, 0755); err != nil { ··· 163 163 return fmt.Errorf("error: failed to create directory: %s: %w", dstDir, err) 164 164 } 165 165 166 - if err := file.Render(dstFile, nil); err != nil { 166 + if err := file.Render(dstFile, nil, drafts); err != nil { 167 167 return fmt.Errorf("error: failed to render %s: %w", dstFile, err) 168 168 } 169 169 170 170 post.Meta = file.Frontmatter() 171 171 post.Body = file.Body() 172 - posts = append(posts, post) 172 + isDraft := post.Meta["draft"] == "true" 173 + if !isDraft || (isDraft && drafts) { 174 + posts = append(posts, post) 175 + } 173 176 } 174 177 175 178 sort.Slice(posts, func(i, j int) bool { ··· 184 187 indexMd := filepath.Join(types.PagesDir, dir.Name, "_index.md") 185 188 index := markdown.Markdown{Path: indexMd} 186 189 dstFile := filepath.Join(dstDir, "index.html") 187 - if err := index.Render(dstFile, posts); err != nil { 190 + if err := index.Render(dstFile, posts, false); err != nil { 188 191 return fmt.Errorf("error: failed to render index %s: %w", dstFile, err) 189 192 } 190 193 }
+1 -1
formats/anything.go
··· 14 14 func (Anything) Body() string { return "" } 15 15 func (a Anything) Basename() string { return filepath.Base(a.Path) } 16 16 17 - func (a Anything) Render(dest string, data interface{}) error { 17 + func (a Anything) Render(dest string, data interface{}, drafts bool) error { 18 18 return util.CopyFile(a.Path, dest) 19 19 }
+6 -3
formats/markdown/markdown.go
··· 96 96 Extra interface{} 97 97 } 98 98 99 - func (md *Markdown) Render(dest string, data interface{}) error { 99 + func (md *Markdown) Render(dest string, data interface{}, drafts bool) error { 100 100 source, err := os.ReadFile(md.Path) 101 101 if err != nil { 102 102 return fmt.Errorf("markdown: error reading file: %w", err) ··· 108 108 } 109 109 110 110 if md.frontmatter["draft"] == "true" { 111 - fmt.Printf("vite: skipping draft %s\n", md.Path) 112 - return nil 111 + if !drafts { 112 + fmt.Printf("vite: skipping draft %s\n", md.Path) 113 + return nil 114 + } 115 + fmt.Printf("vite: rendering draft %s\n", md.Path) 113 116 } 114 117 115 118 err = md.template(dest, types.TemplatesDir, templateData{
+1 -1
formats/yaml/yaml.go
··· 54 54 return tmpl.Write(dest, metaTemplate, data) 55 55 } 56 56 57 - func (y *YAML) Render(dest string, data interface{}) error { 57 + func (y *YAML) Render(dest string, data interface{}, drafts bool) error { 58 58 yamlBytes, err := os.ReadFile(y.Path) 59 59 if err != nil { 60 60 return fmt.Errorf("yaml: failed to read file: %s: %w", y.Path, err)
+6 -2
main.go
··· 16 16 17 17 options: 18 18 init PATH create vite project at PATH 19 - build builds the current project 19 + build [--drafts] builds the current project 20 20 new PATH create a new markdown post 21 21 serve [HOST:PORT] serves the 'build' directory 22 22 ` ··· 38 38 } 39 39 40 40 case "build": 41 - if err := commands.Build(); err != nil { 41 + var drafts bool 42 + if len(args) > 2 && args[2] == "--drafts" { 43 + drafts = true 44 + } 45 + if err := commands.Build(drafts); err != nil { 42 46 fmt.Fprintf(os.Stderr, "error: build: %+v\n", err) 43 47 } 44 48
+1 -1
types/types.go
··· 13 13 // page frontmatter and the body, as template params. Templates are read 14 14 // from types.TemplateDir and the final html is written to dest, 15 15 // with necessary directories being created. 16 - Render(dest string, data interface{}) error 16 + Render(dest string, data interface{}, drafts bool) error 17 17 18 18 // Frontmatter will not be populated if Render hasn't been called. 19 19 Frontmatter() map[string]string