adrg/frontmatter

Parse nested TOML

Closed this issue · 3 comments

pinpox commented

I'm trying to parse the frontmatter as used by the zola static site generator, it looks something like this for my blog:

++
draft = true
date = 2020-07-17T09:30:09+02:00
title = "Test example of new style"
description = "This is the new example used to test out typography settings" 
slug = "test-example" 

[taxonomies]
tags = ["github", "markdown", "golang", "CI", "CD", "automation"]
+++

It works for the most part, but fails to get the tags, I assume because they are nested below the [taxonomis] subdivision. Is there a way to parse these? I'm using the example code from the readme, but it only works for the other fields.

Thanks for any help!

adrg commented

Hi @pinpox.

This works fine for me. Here's a quick sample:

package main

import (
	"fmt"
	"log"
	"strings"
	"time"

	"github.com/adrg/frontmatter"
)

var input = `
+++
draft = true
date = 2020-07-17T09:30:09+02:00
title = "Test example of new style"
description = "This is the new example used to test out typography settings" 
slug = "test-example" 

[taxonomies]
tags = ["github", "markdown", "golang", "CI", "CD", "automation"]
+++
rest of the content`

type Post struct {
	Draft       bool      `toml:"draft"`
	Date        time.Time `toml:"date"`
	Title       string    `toml:"title"`
	Description string    `toml:"description"`
	Slug        string    `toml:"slug"`
	Taxonomies  struct {
		Tags []string `toml:"tags"`
	} `toml:"taxonomies"`
}

func main() {
	matter := &Post{}

	rest, err := frontmatter.Parse(strings.NewReader(input), &matter)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%+v\n", matter)
	fmt.Println(string(rest))
}
adrg commented

Closing this issue as it has been addressed. Please reopen if necessary.

pinpox commented

Thank you, workse perfectly!