adrg / frontmatter Goto Github PK
View Code? Open in Web Editor NEWGo library for detecting and decoding various content front matter formats
Home Page: https://pkg.go.dev/github.com/adrg/frontmatter
License: MIT License
Go library for detecting and decoding various content front matter formats
Home Page: https://pkg.go.dev/github.com/adrg/frontmatter
License: MIT License
Hey, thank you for this great package, I'm currently using it in my static site generator. I'm having issues defining a custom unmarshal function. I'm trying to parse frontmatter to grab an ISO8601 date, convert it into a time.Time
, and marshal it into my structure.
Here's what I've got so far:
package main
import (
"bytes"
"fmt"
"github.com/adrg/frontmatter"
"gopkg.in/yaml.v2"
"time"
)
type Matter struct {
Template string `yaml:"template"`
URL string `yaml:"url"`
Title string `yaml:"title"`
Subtitle string `yaml:"subtitle"`
Date time.Time `yaml:"date"`
Body string
}
func (m *Matter) UnmarshalYAML(unmarshal func(interface{}) error) error {
var temp struct {
Template string `yaml:"template"`
URL string `yaml:"url"`
Title string `yaml:"title"`
Subtitle string `yaml:"subtitle"`
Date string `yaml:"date"`
Body string
}
if err := unmarshal(&temp); err != nil {
return err
}
layout := "2006-01-02"
var date8601 time.Time
var err error
if temp.Date != "" {
date8601, err = time.Parse(layout, temp.Date)
if err != nil {
return err
}
}
m = &Matter{
Template: temp.Template,
URL: temp.URL,
Title: temp.Title,
Subtitle: temp.Subtitle,
Date: date8601,
Body: temp.Body,
}
fmt.Println(m) // The full object gets printed!
return nil
}
var format = frontmatter.NewFormat("---", "---", yaml.Unmarshal)
// Parses frontmatter, populates the `matter` struct and
// returns the rest
func parseFrontmatter(inputBytes []byte) ([]byte, Matter) {
m := Matter{}
input := bytes.NewReader(inputBytes)
rest, err := frontmatter.Parse(input, &m, format)
fmt.Println(m) // Empty object
if err != nil {
printErr(err)
}
return rest, m
}
Ideally, this should populate the m
object, and it seems to do so when I print it from within UnmarshalYAML()
, but when I print it in my parseFrontmatter
function, it's empty. Any ideas?
Excuse the bad quality of code—I'm only a few days into the language. :)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.