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.

Load data from generic YAML files

+58 -19
+58 -19
commands/build.go
··· 13 13 "git.icyphox.sh/vite/config" 14 14 "git.icyphox.sh/vite/markdown" 15 15 "git.icyphox.sh/vite/util" 16 + "gopkg.in/yaml.v3" 16 17 ) 17 18 18 19 const ( 19 - BUILD = "build" 20 - PAGES = "pages" 21 - TEMPLATES = "templates" 22 - STATIC = "static" 20 + BuildDir = "build" 21 + PagesDir = "pages" 22 + TemplatesDir = "templates" 23 + StaticDir = "static" 23 24 ) 24 25 25 26 type Pages struct { ··· 48 49 49 50 func (pgs *Pages) processFiles() error { 50 51 for _, f := range pgs.Files { 51 - if filepath.Ext(f) == ".md" { 52 + switch filepath.Ext(f) { 53 + case ".md": 52 54 // ex: pages/about.md 53 - mdFile := filepath.Join(PAGES, f) 55 + mdFile := filepath.Join(PagesDir, f) 54 56 var htmlDir string 55 57 // ex: build/index.html (root index) 56 58 if f == "_index.md" { 57 - htmlDir = BUILD 59 + htmlDir = BuildDir 58 60 } else { 59 61 htmlDir = filepath.Join( 60 - BUILD, 62 + BuildDir, 61 63 strings.TrimSuffix(f, ".md"), 62 64 ) 63 65 } ··· 76 78 } 77 79 if err = out.RenderHTML( 78 80 htmlFile, 79 - TEMPLATES, 81 + TemplatesDir, 80 82 struct { 81 83 Cfg config.ConfigYaml 82 84 Meta markdown.Matter ··· 85 87 ); err != nil { 86 88 return err 87 89 } 88 - } else { 89 - src := filepath.Join(PAGES, f) 90 - dst := filepath.Join(BUILD, f) 90 + case ".yaml": 91 + // ex: pages/reading.yaml 92 + yamlFile := filepath.Join(PagesDir, f) 93 + htmlDir := filepath.Join(BuildDir, strings.TrimSuffix(f, ".yaml")) 94 + os.Mkdir(htmlDir, 0755) 95 + htmlFile := filepath.Join(htmlDir, "index.html") 96 + 97 + yb, err := os.ReadFile(yamlFile) 98 + if err != nil { 99 + return err 100 + } 101 + 102 + data := map[string]interface{}{} 103 + err = yaml.Unmarshal(yb, &data) 104 + if err != nil { 105 + return fmt.Errorf("error: unmarshalling yaml file %s: %v", yamlFile, err) 106 + } 107 + 108 + meta := make(map[string]string) 109 + for k, v := range data["meta"].(map[string]interface{}) { 110 + meta[k] = v.(string) 111 + } 112 + 113 + out := markdown.Output{} 114 + out.Meta = meta 115 + if err = out.RenderHTML( 116 + htmlFile, 117 + TemplatesDir, 118 + struct { 119 + Cfg config.ConfigYaml 120 + Meta markdown.Matter 121 + Yaml map[string]interface{} 122 + Body string 123 + }{config.Config, meta, data, ""}, 124 + ); err != nil { 125 + return err 126 + } 127 + default: 128 + src := filepath.Join(PagesDir, f) 129 + dst := filepath.Join(BuildDir, f) 91 130 if err := util.CopyFile(src, dst); err != nil { 92 131 return err 93 132 } ··· 99 138 func (pgs *Pages) processDirs() error { 100 139 for _, d := range pgs.Dirs { 101 140 // ex: build/blog 102 - dstDir := filepath.Join(BUILD, d) 141 + dstDir := filepath.Join(BuildDir, d) 103 142 // ex: pages/blog 104 - srcDir := filepath.Join(PAGES, d) 143 + srcDir := filepath.Join(PagesDir, d) 105 144 os.Mkdir(dstDir, 0755) 106 145 107 146 entries, err := os.ReadDir(srcDir) ··· 133 172 } 134 173 if err = out.RenderHTML( 135 174 htmlFile, 136 - TEMPLATES, 175 + TemplatesDir, 137 176 struct { 138 177 Cfg config.ConfigYaml 139 178 Meta markdown.Matter ··· 168 207 return err 169 208 } 170 209 171 - out.RenderHTML(indexHTML, TEMPLATES, struct { 210 + out.RenderHTML(indexHTML, TemplatesDir, struct { 172 211 Cfg config.ConfigYaml 173 212 Meta markdown.Matter 174 213 Body string ··· 197 236 } 198 237 199 238 // Clean the build directory. 200 - if err := util.Clean(BUILD); err != nil { 239 + if err := util.Clean(BuildDir); err != nil { 201 240 return err 202 241 } 203 242 ··· 242 281 243 282 // Copy the static directory into build 244 283 // ex: build/static/ 245 - buildStatic := filepath.Join(BUILD, STATIC) 284 + buildStatic := filepath.Join(BuildDir, StaticDir) 246 285 os.Mkdir(buildStatic, 0755) 247 - if err := util.CopyDir(STATIC, buildStatic); err != nil { 286 + if err := util.CopyDir(StaticDir, buildStatic); err != nil { 248 287 return err 249 288 } 250 289