goldmark-meta is an extension for the goldmark that allows you to define document metadata in YAML format.
go get github.com/yuin/goldmark-meta
YAML metadata block is a leaf block that can not have any markdown element as a child.
YAML metadata must start with a YAML metadata separator. This separator must be at first line of the document.
A YAML metadata separator is a line that only -
is repeated.
YAML metadata must end with a YAML metadata separator.
You can define objects as a 1st level item. At deeper level, you can define any kind of YAML element.
Example:
---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
- markdown
- goldmark
---
# Heading 1
import (
"bytes"
"fmt"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark-meta"
)
func main() {
markdown := goldmark.New(
goldmark.WithExtensions(
meta.Meta,
),
)
source := `---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
- markdown
- goldmark
---
# Hello goldmark-meta
`
var buf bytes.Buffer
context := parser.NewContext()
if err := markdown.Convert([]byte(source), &buf, parser.WithContext(context)); err != nil {
panic(err)
}
metaData := meta.Get(context)
title := metaData["Title"]
fmt.Print(title)
}
Or WithStoresInDocument
option:
import (
"bytes"
"fmt"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark-meta"
)
func main() {
markdown := goldmark.New(
goldmark.WithExtensions(
meta.New(
meta.WithStoresInDocument(),
),
),
)
source := `---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
- markdown
- goldmark
---
`
document := markdown.Parser().Parse(text.NewReader([]byte(source)))
metaData := document.OwnerDocument().Meta()
title := metaData["Title"]
fmt.Print(title)
}
You need to add extension.TableHTMLRenderer
or the Table
extension to
render metadata as a table.
import (
"bytes"
"fmt"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/util"
"github.com/yuin/goldmark-meta"
)
func main() {
markdown := goldmark.New(
goldmark.WithExtensions(
meta.New(meta.WithTable()),
),
goldmark.WithRendererOptions(
renderer.WithNodeRenderers(
util.Prioritized(extension.NewTableHTMLRenderer(), 500),
),
),
)
// OR
// markdown := goldmark.New(
// goldmark.WithExtensions(
// meta.New(meta.WithTable()),
// extension.Table,
// ),
// )
source := `---
Title: goldmark-meta
Summary: Add YAML metadata to the document
Tags:
- markdown
- goldmark
---
# Hello goldmark-meta
`
var buf bytes.Buffer
if err := markdown.Convert([]byte(source), &buf); err != nil {
panic(err)
}
fmt.Print(buf.String())
}
MIT
Yusuke Inuzuka