fix: broken front matter parsing

This commit is contained in:
Jacky Zhao 2022-05-05 00:55:59 -04:00
parent b724e4aeca
commit b081ab02b0
3 changed files with 26 additions and 16 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/BurntSushi/toml v0.4.1 // indirect
github.com/PuerkitoBio/goquery v1.8.0
github.com/abhinav/goldmark-wikilink v0.3.0
github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a
github.com/adrg/frontmatter v0.2.0 // indirect
github.com/yuin/goldmark v1.4.4
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

4
go.sum
View File

@ -1,9 +1,12 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/abhinav/goldmark-wikilink v0.3.0 h1:ry8CBaULn410PKCSkwLz/WVI2f/g7EB+yqY7LKHDcPQ=
github.com/abhinav/goldmark-wikilink v0.3.0/go.mod h1:MHRZiLRE1ZDZDjHCFYwKEEgITXGbB7N0Yr00dbmfHM8=
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
@ -27,6 +30,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

36
walk.go
View File

@ -2,24 +2,33 @@ package main
import (
"fmt"
"github.com/gernest/front"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/adrg/frontmatter"
"gopkg.in/yaml.v2"
)
type Front struct {
Title string `yaml:"title"`
Draft bool `yaml:"draft"`
}
// recursively walk directory and return all files with given extension
func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []Link, i ContentIndex) {
fmt.Printf("Scraping %s\n", root)
i = make(ContentIndex)
m := front.NewMatter()
m.Handle("---", front.YAMLHandler)
nPrivate := 0
formats := []*frontmatter.Format{
frontmatter.NewFormat("---", "---", yaml.Unmarshal),
}
start := time.Now()
err := filepath.WalkDir(root, func(fp string, d fs.DirEntry, e error) error {
@ -37,28 +46,25 @@ func walk(root, ext string, index bool, ignorePaths map[string]struct{}) (res []
if index {
text := getText(s)
frontmatter, body, err := m.Parse(strings.NewReader(text))
var matter Front
raw_body, err := frontmatter.Parse(strings.NewReader(text), &matter, formats...)
body := string(raw_body)
if err != nil {
frontmatter = map[string]interface{}{}
matter = Front{
Title: "Untitled Page",
Draft: false,
}
body = text
}
var title string
if parsedTitle, ok := frontmatter["title"]; ok {
title = parsedTitle.(string)
} else {
title = "Untitled Page"
}
// check if page is private
if parsedPrivate, ok := frontmatter["draft"]; !ok || !parsedPrivate.(bool) {
if !matter.Draft {
info, _ := os.Stat(s)
source := processSource(trim(s, root, ".md"))
// adjustedPath := UnicodeSanitize(strings.Replace(hugoPathTrim(trim(s, root, ".md")), " ", "-", -1))
i[source] = Content{
LastModified: info.ModTime(),
Title: title,
Title: matter.Title,
Content: body,
}
} else {