summaryrefslogtreecommitdiff
path: root/pkg/content/content.go
blob: 934b2eef247607ec9094d66fae903568a01fea74 (plain)
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
package content

import (
	"io/fs"
	"path/filepath"
	"strings"
)

type Frontmatter struct {
	Title string   `yaml:"title"`
	Date  string   `yaml:"date"`
	Tags  []string `yaml:"tags"`
}

type PostSummary struct {
	Title string
	Slug  string
	Date  string
	Tags  []string
}

type ContentFile struct {
	Path        string
	Slug        string
	Frontmatter Frontmatter
}

func ListContentFiles(contentDir string) ([]ContentFile, error) {
	var files []ContentFile

	err := filepath.WalkDir(contentDir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		if d.IsDir() {
			return nil
		}

		if strings.HasSuffix(d.Name(), ".md") {
			slug := strings.TrimSuffix(d.Name(), ".md")
			files = append(files, ContentFile{
				Path: path,
				Slug: slug,
			})
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return files, nil
}

func GetOutputPath(contentDir, contentPath, outputDir string) (string, error) {
	relPath, err := filepath.Rel(contentDir, contentPath)
	if err != nil {
		return "", err
	}

	outputFilePath := filepath.Join(outputDir, strings.Replace(relPath, ".md", "/index.html", 1))
	return outputFilePath, nil
}