home/vendor/github.com/adrg/frontmatter
2023-11-08 04:31:38 +06:00
..
CODE_OF_CONDUCT.md 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00
CONTRIBUTING.md 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00
format.go 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00
frontmatter.go 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00
LICENSE 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00
parser.go 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00
README.md 📌 Vendored imported go modules 2023-11-08 04:31:38 +06:00

frontmatter logo
frontmatter

Go library for detecting and decoding various content front matter formats.

Build status Code coverage pkg.go.dev documentation MIT License Go report card
GitHub contributors GitHub open issues GitHub closed issues Buy me a coffee GitHub stars

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 below.

Default front matter formats

Installation

go get github.com/adrg/frontmatter

Usage

Default usage.

package main

import (
	"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.

package main

import (
	"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

Contributing

Contributions in the form of pull requests, issues or just general feedback,
are always welcome.
See CONTRIBUTING.MD.

Buy me a coffee

If you found this project useful and want to support it, consider buying me a coffee.

Buy Me A Coffee

License

Copyright (c) 2020 Adrian-George Bostan.

This project is licensed under the MIT license.
See LICENSE for more details.