all repos — vite @ 81b8a3faf63f5b53e9d6e95aa09149941c4a2f11

a fast (this time, actually) and minimal static site generator

Add a --drafts flag to include drafts while building
Anirudh Oppiliappan x@icyphox.sh
Sun, 17 Nov 2024 16:25:33 +0200
commit

81b8a3faf63f5b53e9d6e95aa09149941c4a2f11

parent

cf6acc3b2905bd905bd44805a159e4bb3b6072e6

M commands/build.gocommands/build.go

@@ -81,7 +81,7 @@ }

// Build is the core builder function. Converts markdown/yaml // to html, copies over non-.md/.yaml files, etc. -func Build() error { +func Build(drafts bool) error { if err := preBuild(); err != nil { return err }

@@ -96,11 +96,11 @@ if err := util.Clean(types.BuildDir); err != nil {

return err } - if err := pages.ProcessFiles(); err != nil { + if err := pages.ProcessFiles(drafts); err != nil { return err } - if err := pages.ProcessDirectories(); err != nil { + if err := pages.ProcessDirectories(drafts); err != nil { return err }

@@ -117,7 +117,7 @@ }

// ProcessFiles handles root level files under 'pages', // for example: 'pages/_index.md' or 'pages/about.md'. -func (p *Pages) ProcessFiles() error { +func (p *Pages) ProcessFiles(drafts bool) error { for _, f := range p.Files { var htmlDir string if f.Basename() == "_index.md" {

@@ -134,7 +134,7 @@ if err := os.MkdirAll(htmlDir, 0755); err != nil {

return err } } - if err := f.Render(destFile, nil); err != nil { + if err := f.Render(destFile, nil, drafts); err != nil { return fmt.Errorf("error: failed to render %s: %w", destFile, err) } }

@@ -143,7 +143,7 @@ }

// ProcessDirectories handles directories of posts under 'pages', // for example: 'pages/photos/foo.md' or 'pages/blog/bar.md'. -func (p *Pages) ProcessDirectories() error { +func (p *Pages) ProcessDirectories(drafts bool) error { for _, dir := range p.Dirs { dstDir := filepath.Join(types.BuildDir, dir.Name) if err := os.MkdirAll(dstDir, 0755); err != nil {

@@ -163,13 +163,16 @@ if err := os.MkdirAll(filepath.Join(dstDir, slug), 0755); err != nil {

return fmt.Errorf("error: failed to create directory: %s: %w", dstDir, err) } - if err := file.Render(dstFile, nil); err != nil { + if err := file.Render(dstFile, nil, drafts); err != nil { return fmt.Errorf("error: failed to render %s: %w", dstFile, err) } post.Meta = file.Frontmatter() post.Body = file.Body() - posts = append(posts, post) + isDraft := post.Meta["draft"] == "true" + if !isDraft || (isDraft && drafts) { + posts = append(posts, post) + } } sort.Slice(posts, func(i, j int) bool {

@@ -184,7 +187,7 @@ if dir.HasIndex {

indexMd := filepath.Join(types.PagesDir, dir.Name, "_index.md") index := markdown.Markdown{Path: indexMd} dstFile := filepath.Join(dstDir, "index.html") - if err := index.Render(dstFile, posts); err != nil { + if err := index.Render(dstFile, posts, false); err != nil { return fmt.Errorf("error: failed to render index %s: %w", dstFile, err) } }
M formats/anything.goformats/anything.go

@@ -14,6 +14,6 @@ func (Anything) Frontmatter() map[string]string { return nil }

func (Anything) Body() string { return "" } func (a Anything) Basename() string { return filepath.Base(a.Path) } -func (a Anything) Render(dest string, data interface{}) error { +func (a Anything) Render(dest string, data interface{}, drafts bool) error { return util.CopyFile(a.Path, dest) }
M formats/markdown/markdown.goformats/markdown/markdown.go

@@ -96,7 +96,7 @@ Body string

Extra interface{} } -func (md *Markdown) Render(dest string, data interface{}) error { +func (md *Markdown) Render(dest string, data interface{}, drafts bool) error { source, err := os.ReadFile(md.Path) if err != nil { return fmt.Errorf("markdown: error reading file: %w", err)

@@ -108,8 +108,11 @@ return fmt.Errorf("markdown: error extracting frontmatter: %w", err)

} if md.frontmatter["draft"] == "true" { - fmt.Printf("vite: skipping draft %s\n", md.Path) - return nil + if !drafts { + fmt.Printf("vite: skipping draft %s\n", md.Path) + return nil + } + fmt.Printf("vite: rendering draft %s\n", md.Path) } err = md.template(dest, types.TemplatesDir, templateData{
M formats/yaml/yaml.goformats/yaml/yaml.go

@@ -54,7 +54,7 @@

return tmpl.Write(dest, metaTemplate, data) } -func (y *YAML) Render(dest string, data interface{}) error { +func (y *YAML) Render(dest string, data interface{}, drafts bool) error { yamlBytes, err := os.ReadFile(y.Path) if err != nil { return fmt.Errorf("yaml: failed to read file: %s: %w", y.Path, err)
M main.gomain.go

@@ -16,7 +16,7 @@ A simple and minimal static site generator.

options: init PATH create vite project at PATH - build builds the current project + build [--drafts] builds the current project new PATH create a new markdown post serve [HOST:PORT] serves the 'build' directory `

@@ -38,7 +38,11 @@ fmt.Fprintf(os.Stderr, "error: init: %+v\n", err)

} case "build": - if err := commands.Build(); err != nil { + var drafts bool + if len(args) > 2 && args[2] == "--drafts" { + drafts = true + } + if err := commands.Build(drafts); err != nil { fmt.Fprintf(os.Stderr, "error: build: %+v\n", err) }
M types/types.gotypes/types.go

@@ -13,7 +13,7 @@ // Render takes any arbitrary data and combines that with the global config,

// page frontmatter and the body, as template params. Templates are read // from types.TemplateDir and the final html is written to dest, // with necessary directories being created. - Render(dest string, data interface{}) error + Render(dest string, data interface{}, drafts bool) error // Frontmatter will not be populated if Render hasn't been called. Frontmatter() map[string]string