internal/content/posts.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | package content import ( "bytes" "io" "io/fs" "os" "path" "path/filepath" "slices" "strings" "time" "go.alanpearce.eu/homestead/internal/markdown" "go.alanpearce.eu/homestead/internal/vcs" "go.alanpearce.eu/x/log" "github.com/adrg/frontmatter" mapset "github.com/deckarep/golang-set/v2" "gitlab.com/tozd/go/errors" ) var SkipList = []string{ "LICENSE", "taplo.toml", } type PostMatter struct { Date time.Time Description string Title string Taxonomies struct { Tags []string } } type Post struct { Input string Basename string URL string Commits []*vcs.Commit *PostMatter content []byte } type Config struct { Root string PostDir string Repo *vcs.Repository } type Collection struct { config *Config log *log.Logger Posts []*Post Pages []*Post StaticFiles []string Tags mapset.Set[string] } var postURLReplacer = strings.NewReplacer( "index.md", "", ".md", "/", ) func (cc *Collection) GetPost(filename string) (*Post, error) { fp := filepath.Join(cc.config.Root, filename) url := path.Join("/", postURLReplacer.Replace(filename)) + "/" cs, err := cc.config.Repo.GetFileLog(filename) if err != nil { return nil, errors.WithMessagef(err, "could not get commit log for file %s", filename) } post := &Post{ Input: filename, Basename: filepath.Base(url), URL: url, PostMatter: &PostMatter{}, Commits: cs, } err = parse(fp, post) if err != nil { return nil, err } return post, nil } var pageURLReplacer = strings.NewReplacer( "index.md", "", ".md", "", ) func (cc *Collection) GetPage(filename string) (*Post, errors.E) { fp := filepath.Join(cc.config.Root, filename) url := path.Join("/", pageURLReplacer.Replace(filename)) cs, err := cc.config.Repo.GetFileLog(filename) if err != nil { return nil, errors.WithMessagef(err, "could not get commit log for file %s", filename) } post := &Post{ Input: filename, Basename: filepath.Base(url), URL: url, PostMatter: &PostMatter{}, Commits: cs, } err = parse(fp, post) if err != nil { return nil, err } if post.Date.IsZero() && len(cs) > 1 { post.Date = cs[0].Date } return post, nil } func parse(fp string, post *Post) errors.E { content, err := os.Open(fp) if err != nil { return errors.WithMessagef(err, "could not open post %s", fp) } defer content.Close() post.content, err = frontmatter.Parse(content, post.PostMatter) if err != nil { return errors.WithMessagef( err, "could not parse front matter of post %s", fp, ) } return nil } // implements gomponent.Node func (p *Post) Render(w io.Writer) error { return markdown.Convert(p.content, w) } func (p *Post) RenderString() (string, errors.E) { var buf bytes.Buffer if err := p.Render(&buf); err != nil { return "", errors.WithMessage(err, "could not convert markdown content") } return buf.String(), nil } func (cc *Collection) HandleFile(filename string, d fs.DirEntry) error { switch { case strings.HasPrefix(filename, ".") && filename != "." && !strings.HasPrefix(filename, ".well-known"): cc.log.Debug("skipping", "filename", filename, "is_dir", d.Type().IsDir()) if d.Type().IsDir() { return fs.SkipDir } case !d.Type().IsDir(): if filepath.Ext(filename) == ".md" { if strings.HasPrefix(filename, cc.config.PostDir) { post, err := cc.GetPost(filename) if err != nil { return err } for _, tag := range post.Taxonomies.Tags { cc.Tags.Add(strings.ToLower(tag)) } cc.Posts = append(cc.Posts, post) } else { page, err := cc.GetPage(filename) if err != nil { return err } cc.Pages = append(cc.Pages, page) } } else if !slices.Contains(SkipList, filename) { cc.StaticFiles = append(cc.StaticFiles, filename) } } return nil } func NewContentCollection(config *Config, log *log.Logger) (*Collection, errors.E) { cc := &Collection{ Posts: []*Post{}, Tags: mapset.NewSet[string](), Pages: []*Post{}, StaticFiles: []string{}, config: config, log: log, } err := filepath.WalkDir(config.Root, func(filename string, d fs.DirEntry, err error) error { if err != nil { return err } filename, err = filepath.Rel(config.Root, filename) if err != nil { return err } log.Debug("walking", "filename", filename) return cc.HandleFile(filename, d) }) slices.SortFunc(cc.Posts, func(a, b *Post) int { return b.Date.Compare(a.Date) }) if err != nil { return nil, errors.WithMessage(err, "could not walk directory") } return cc, nil } |