frontmatter
Go library for detecting and decoding various content front matter formats.
Installation
go get github.com/aisbergg/go-frontmatter
Usage
Default usage.
package main
import (
"fmt"
"strings"
"github.com/aisbergg/go-frontmatter/pkg/frontmatter"
)
var input = `
---json
{
"name": "frontmatter",
"tags": ["foo", "bar", "baz"]
}
---
rest of the content
`
func main() {
var matter struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
body, err := frontmatter.Parse(strings.NewReader(input), &matter)
if err != nil {
panic(err)
}
// NOTE: If a front matter must be present in the input data, use
// frontmatter.MustParse instead.
fmt.Printf("%+v\n", matter)
fmt.Println(string(body))
// Output:
// {Name:frontmatter Tags:[foo bar baz]}
// rest of the content
}
Bring your own formats.
This library includes only a JSON format by default. This removes the need for external dependencies and gives your the freedom to choose whatever unmarshaller library you want.
If you like to use any other formats than JSON, you can easily add them. Here is how:
package main
import (
"fmt"
"strings"
"github.com/aisbergg/go-frontmatter"
"gopkg.in/yaml.v3"
)
var input = `
---
name: "frontmatter"
"tags": ["foo", "bar", "baz"]
...
rest of the content
`
func main() {
var matter struct {
Name string `yaml:"name"`
Tags []string `yaml:"tags"`
}
formats := []*frontmatter.Format{
frontmatter.NewFormat("---", "...", yaml.Unmarshal),
}
rest, err := frontmatter.Parse(strings.NewReader(input), &matter, formats...)
if err != nil {
// Treat error.
}
// NOTE: If a front matter must be present in the input data, use
// frontmatter.MustParse instead.
fmt.Printf("%+v\n", matter)
fmt.Println(string(rest))
// Output:
// {Name:frontmatter Tags:[foo bar baz]}
// rest of the content
}
Full documentation can be found at: https://pkg.go.dev/github.com/aisbergg/go-frontmatter.
Contributing
Contributions in the form of pull requests, issues or just general feedback, are always welcome. See CONTRIBUTING.md.
Acknowledgements
This project is a fork of github.com/adrg/frontmatter developed by Adrian-George Bostan. This fork removes the external dependencies and improves performance.
Licence
This project is under the MIT Licence. See the LICENCE file for the full license text.