Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrg/frontmatter
Go library for detecting and decoding various content front matter formats
https://github.com/adrg/frontmatter
decoder front-matter front-matter-parser frontmatter go golang golang-library golang-package json parser toml yaml
Last synced: about 1 month ago
JSON representation
Go library for detecting and decoding various content front matter formats
- Host: GitHub
- URL: https://github.com/adrg/frontmatter
- Owner: adrg
- License: mit
- Created: 2020-10-11T20:31:11.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-31T13:21:08.000Z (about 2 months ago)
- Last Synced: 2024-07-31T16:39:19.550Z (about 2 months ago)
- Topics: decoder, front-matter, front-matter-parser, frontmatter, go, golang, golang-library, golang-package, json, parser, toml, yaml
- Language: Go
- Homepage: https://pkg.go.dev/github.com/adrg/frontmatter
- Size: 70.3 KB
- Stars: 101
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
frontmatterGo library for detecting and decoding various content front matter formats.
## Supported formats
The following front matter formats are supported by default. If the default
formats are not suitable for your use case, you can easily bring your own.
For more information, see the [usage examples](#usage) below.![Default front matter formats](https://raw.githubusercontent.com/adrg/adrg.github.io/master/assets/projects/frontmatter/formats.png)
## Installation
```bash
go get github.com/adrg/frontmatter
```## Usage
**Default usage.**
```go
package mainimport (
"fmt"
"strings""github.com/adrg/frontmatter"
)var input = `
---
name: "frontmatter"
tags: ["go", "yaml", "json", "toml"]
---
rest of the content`func main() {
var matter struct {
Name string `yaml:"name"`
Tags []string `yaml:"tags"`
}rest, err := frontmatter.Parse(strings.NewReader(input), &matter)
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:[go yaml json toml]}
// rest of the content
}
```**Bring your own formats.**
```go
package mainimport (
"fmt"
"strings""github.com/adrg/frontmatter"
"gopkg.in/yaml.v2"
)var input = `
...
name: "frontmatter"
tags: ["go", "yaml", "json", "toml"]
...
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:[go yaml json toml]}
// rest of the content
}
```Full documentation can be found at: https://pkg.go.dev/github.com/adrg/frontmatter.
## Stargazers over time
[![Stargazers over time](https://starchart.cc/adrg/frontmatter.svg)](https://starchart.cc/adrg/frontmatter)
## Contributing
Contributions in the form of pull requests, issues or just general feedback,
are always welcome. See [CONTRIBUTING.MD](CONTRIBUTING.md).## License
Copyright (c) 2020 Adrian-George Bostan.
This project is licensed under the [MIT license](https://opensource.org/licenses/MIT).
See [LICENSE](LICENSE) for more details.