Skip to content

Commit

Permalink
parse frontmatter from md files
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcharter committed Jul 29, 2024
1 parent 9e8621e commit 4455741
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@

# Go workspace file
go.work

config.toml

dist/
content/
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/jmcharter/lumaca

go 1.20

require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/adrg/frontmatter v0.2.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
79 changes: 79 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"github.com/adrg/frontmatter"
)

type Config struct {
ContentDirectories struct {
Posts string
Pages string
}
Author struct {
Name string
}
}

type Matter struct {
Title string `yaml:"title"`
Author string `yaml:"author"`
Tags []string `yaml:"tags"`
Date string
}

func main() {
fmt.Println("Lumaca starting...")
var config Config
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
log.Fatal(err)
}
err := run(config)
if err != nil {
log.Fatal(err)
}
fmt.Println("Lumaca finished.")
}

func run(config Config) error {

files, err := os.ReadDir(config.ContentDirectories.Posts)
if err != nil {
return err
}

var matters []Matter
for _, file := range files {
if file.IsDir() || !strings.HasSuffix(file.Name(), ".md") {
break
}
filepath := filepath.Join(config.ContentDirectories.Posts, file.Name())
content, err := os.ReadFile(filepath)
if err != nil {
log.Println("Error reading file:", err)
continue
}

var matter Matter
_, err = frontmatter.Parse(strings.NewReader(string(content)), &matter)
if err != nil {
log.Println("Error parsing frontmatter from file:", err)
continue
}
matters = append(matters, matter)

}

for _, m := range matters {
fmt.Printf("%v\n", m)
}

return nil

}

0 comments on commit 4455741

Please sign in to comment.