You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
892 B
Go
47 lines
892 B
Go
2 years ago
|
package operation
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"log"
|
||
|
|
||
|
"github.com/aziis98/cabret"
|
||
|
"github.com/iancoleman/strcase"
|
||
|
"github.com/yuin/goldmark"
|
||
|
meta "github.com/yuin/goldmark-meta"
|
||
|
"github.com/yuin/goldmark/parser"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
registerType("frontmatter", &Frontmatter{})
|
||
|
}
|
||
|
|
||
|
type Frontmatter struct {
|
||
|
Options map[string]any
|
||
|
}
|
||
|
|
||
|
func (op *Frontmatter) Load(config map[string]any) error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (op *Frontmatter) ProcessItem(content cabret.Content) (*cabret.Content, error) {
|
||
|
md := goldmark.New(
|
||
|
goldmark.WithExtensions(
|
||
|
meta.Meta,
|
||
|
),
|
||
|
)
|
||
|
|
||
|
log.Printf(`[operation.Frontmatter] reading frontmatter`)
|
||
|
|
||
|
context := parser.NewContext()
|
||
|
if err := md.Convert(content.Data, io.Discard, parser.WithContext(context)); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
frontmatter := meta.Get(context)
|
||
|
for k, v := range frontmatter {
|
||
|
content.Metadata[strcase.ToCamel(k)] = v
|
||
|
}
|
||
|
|
||
|
return &content, nil
|
||
|
}
|