fork of anirudh.fi/vite that uses chroma for hl
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Reorganize; index rendering

+115 -87
+107 -79
commands/build.go
··· 1 1 package commands 2 2 3 3 import ( 4 - "fmt" 5 4 "os" 6 5 "path/filepath" 7 6 "sort" ··· 43 42 return nil 44 43 } 45 44 46 - // Core builder function. Converts markdown to html, 47 - // copies over non .md files, etc. 48 - func Build() error { 49 - pages := Pages{} 50 - err := pages.initPages() 51 - if err != nil { 52 - return err 53 - } 45 + func (pgs *Pages) processFiles() error { 46 + for _, f := range pgs.Files { 47 + if filepath.Ext(f) == ".md" { 48 + // ex: pages/about.md 49 + mdFile := filepath.Join(PAGES, f) 50 + var htmlDir string 51 + // ex: build/index.html (root index) 52 + if f == "_index.md" { 53 + htmlDir = BUILD 54 + } else { 55 + htmlDir = filepath.Join( 56 + BUILD, 57 + strings.TrimSuffix(f, ".md"), 58 + ) 59 + } 60 + os.Mkdir(htmlDir, 0755) 61 + // ex: build/about/index.html 62 + htmlFile := filepath.Join(htmlDir, "index.html") 54 63 55 - // Deal with files. 56 - // ex: pages/{_index,about,etc}.md 57 - err = func() error { 58 - for _, f := range pages.Files { 59 - if filepath.Ext(f) == ".md" { 60 - // ex: pages/about.md 61 - mdFile := filepath.Join(PAGES, f) 62 - var htmlDir string 63 - if f == "_index.md" { 64 - htmlDir = BUILD 65 - } else { 66 - htmlDir = filepath.Join( 67 - BUILD, 68 - strings.TrimSuffix(f, ".md"), 69 - ) 70 - } 71 - os.Mkdir(htmlDir, 0755) 72 - // ex: build/about/index.html 73 - htmlFile := filepath.Join(htmlDir, "index.html") 64 + fb, err := os.ReadFile(mdFile) 65 + if err != nil { 66 + return err 67 + } 74 68 75 - fb, err := os.ReadFile(mdFile) 76 - if err != nil { 77 - return err 78 - } 79 - 80 - out := markdown.Output{} 81 - out.RenderMarkdown(fb) 82 - if err = out.RenderHTML( 83 - htmlFile, 84 - TEMPLATES, 85 - struct { 86 - Cfg config.ConfigYaml 87 - Fm markdown.Matter 88 - Body string 89 - }{config.Config, out.Meta, string(out.HTML)}, 90 - ); err != nil { 91 - return err 92 - } 93 - } else { 94 - src := filepath.Join(PAGES, f) 95 - util.CopyFile(src, BUILD) 69 + out := markdown.Output{} 70 + out.RenderMarkdown(fb) 71 + if err = out.RenderHTML( 72 + htmlFile, 73 + TEMPLATES, 74 + struct { 75 + Cfg config.ConfigYaml 76 + Fm markdown.Matter 77 + Body string 78 + }{config.Config, out.Meta, string(out.HTML)}, 79 + ); err != nil { 80 + return err 96 81 } 82 + } else { 83 + src := filepath.Join(PAGES, f) 84 + util.CopyFile(src, BUILD) 97 85 } 98 - return nil 99 - }() 100 - if err != nil { 101 - return err 102 86 } 87 + return nil 88 + } 103 89 104 - // Deal with dirs -- i.e. of markdown files. 105 - // ex: pages/{blog,travel}/*.md 106 - err = func() error { 107 - for _, d := range pages.Dirs { 108 - // ex: build/blog 109 - dstDir := filepath.Join(BUILD, d) 110 - // ex: pages/blog 111 - srcDir := filepath.Join(PAGES, d) 112 - os.Mkdir(dstDir, 0755) 90 + func (pgs *Pages) processDirs() error { 91 + for _, d := range pgs.Dirs { 92 + // ex: build/blog 93 + dstDir := filepath.Join(BUILD, d) 94 + // ex: pages/blog 95 + srcDir := filepath.Join(PAGES, d) 96 + os.Mkdir(dstDir, 0755) 97 + 98 + entries, err := os.ReadDir(srcDir) 99 + if err != nil { 100 + return err 101 + } 102 + 103 + posts := []markdown.Output{} 104 + // Collect all posts 105 + for _, e := range entries { 106 + // foo-bar.md -> foo-bar 107 + slug := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name())) 113 108 114 - entries, err := os.ReadDir(srcDir) 115 - if err != nil { 116 - return err 117 - } 109 + // ex: build/blog/foo-bar/ 110 + os.Mkdir(filepath.Join(dstDir, slug), 0755) 111 + // ex: build/blog/foo-bar/index.html 112 + htmlFile := filepath.Join(dstDir, slug, "index.html") 118 113 119 - posts := []markdown.Output{} 120 - // Collect all posts 121 - for _, e := range entries { 114 + if e.Name() != "_index.md" { 122 115 ePath := filepath.Join(srcDir, e.Name()) 123 116 fb, err := os.ReadFile(ePath) 124 117 if err != nil { ··· 128 121 out := markdown.Output{} 129 122 out.RenderMarkdown(fb) 130 123 131 - slug := strings.TrimSuffix(filepath.Ext(e.Name()), ".md") 132 - 133 - htmlFile := filepath.Join(dstDir, slug) 134 - out.RenderHTML( 124 + if err = out.RenderHTML( 135 125 htmlFile, 136 126 TEMPLATES, 137 127 struct { 138 128 Cfg config.ConfigYaml 139 129 Fm markdown.Matter 140 130 Body string 141 - }{config.Config, out.Meta, string(out.HTML)}) 131 + }{config.Config, out.Meta, string(out.HTML)}, 132 + ); err != nil { 133 + return err 134 + } 142 135 posts = append(posts, out) 143 136 } 144 137 ··· 148 141 date2 := posts[i].Meta["date"].(time.Time) 149 142 return date1.Before(date2) 150 143 }) 144 + } 151 145 152 - for _, p := range posts { 153 - fmt.Println(p.Meta["date"]) 154 - } 146 + // Render index using posts slice. 147 + // ex: build/blog/index.html 148 + indexHTML := filepath.Join(dstDir, "index.html") 149 + // ex: pages/blog/_index.md 150 + indexMd, err := os.ReadFile(filepath.Join(srcDir, "_index.md")) 151 + if err != nil { 152 + return err 155 153 } 156 - return nil 157 - }() 154 + out := markdown.Output{} 155 + out.RenderMarkdown(indexMd) 156 + 157 + out.RenderHTML(indexHTML, TEMPLATES, struct { 158 + Cfg config.ConfigYaml 159 + Fm markdown.Matter 160 + Body string 161 + Posts []markdown.Output 162 + }{config.Config, out.Meta, string(out.HTML), posts}) 163 + } 164 + return nil 165 + } 166 + 167 + // Core builder function. Converts markdown to html, 168 + // copies over non .md files, etc. 169 + func Build() error { 170 + pages := Pages{} 171 + err := pages.initPages() 172 + if err != nil { 173 + return err 174 + } 175 + 176 + // Deal with files. 177 + // ex: pages/{_index,about,etc}.md 178 + err = pages.processFiles() 179 + if err != nil { 180 + return err 181 + } 182 + 183 + // Deal with dirs -- i.e. dirs of markdown files. 184 + // ex: pages/{blog,travel}/*.md 185 + err = pages.processDirs() 158 186 if err != nil { 159 187 return err 160 188 }
+2 -2
config/config.go
··· 24 24 func init() { 25 25 cf, err := os.ReadFile("config.yaml") 26 26 if err != nil { 27 - fmt.Errorf("error: %+v\n", err) 27 + fmt.Fprintf(os.Stderr, "error: %+v\n", err) 28 28 os.Exit(1) 29 29 } 30 30 if err = Config.ParseConfig(cf); err != nil { 31 - fmt.Errorf("error: %+v\n", err) 31 + fmt.Fprintf(os.Stderr, "error: %+v\n", err) 32 32 } 33 33 } 34 34
+3 -3
main.go
··· 34 34 initPath := args[2] 35 35 err := commands.Init(initPath) 36 36 if err != nil { 37 - fmt.Errorf("error: init: %+v\n", err) 37 + fmt.Fprintf(os.Stderr, "error: init: %+v\n", err) 38 38 } 39 39 40 40 case "build": 41 41 err := commands.Build() 42 42 if err != nil { 43 - fmt.Errorf("error: build: %+v\n", err) 43 + fmt.Fprintf(os.Stderr, "error: build: %+v\n", err) 44 44 } 45 45 46 46 case "new": ··· 51 51 newPath := args[2] 52 52 err := commands.New(newPath) 53 53 if err != nil { 54 - fmt.Errorf("error: new: %+v\n", err) 54 + fmt.Fprintf(os.Stderr, "error: new: %+v\n", err) 55 55 } 56 56 } 57 57
+3 -3
markdown/markdown.go
··· 45 45 // Renders out.HTML into dst html file, using the template specified 46 46 // in the frontmatter. data is the template struct. 47 47 func (out *Output) RenderHTML(dst, tmplDir string, data interface{}) error { 48 - metaTemplate := out.Meta["template"].(string) 49 - if metaTemplate == "" { 48 + metaTemplate := out.Meta["template"] 49 + if metaTemplate == nil { 50 50 metaTemplate = "text.html" 51 51 } 52 52 ··· 60 60 return err 61 61 } 62 62 63 - if err = t.ExecuteTemplate(w, metaTemplate, data); err != nil { 63 + if err = t.ExecuteTemplate(w, metaTemplate.(string), data); err != nil { 64 64 return err 65 65 } 66 66 return nil