Mercurial > repos > public > wdown
comparison wdown.go @ 7:a5aa39557726
Add parsing of YAML frontmatter
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 17 Jun 2020 12:23:56 +0200 |
parents | a5f397e25cb7 |
children | 4a25b534c81c 1ffe6e4f933c |
comparison
equal
deleted
inserted
replaced
6:a5f397e25cb7 | 7:a5aa39557726 |
---|---|
9 "os" | 9 "os" |
10 | 10 |
11 "github.com/cbroglie/mustache" | 11 "github.com/cbroglie/mustache" |
12 "github.com/yuin/goldmark" | 12 "github.com/yuin/goldmark" |
13 highlighting "github.com/yuin/goldmark-highlighting" | 13 highlighting "github.com/yuin/goldmark-highlighting" |
14 meta "github.com/yuin/goldmark-meta" | |
14 "github.com/yuin/goldmark/extension" | 15 "github.com/yuin/goldmark/extension" |
15 "github.com/yuin/goldmark/parser" | 16 "github.com/yuin/goldmark/parser" |
16 ) | 17 ) |
17 | 18 |
18 func main() { | 19 func main() { |
45 | 46 |
46 md := goldmark.New( | 47 md := goldmark.New( |
47 goldmark.WithExtensions( | 48 goldmark.WithExtensions( |
48 extension.GFM, | 49 extension.GFM, |
49 highlighting.Highlighting, | 50 highlighting.Highlighting, |
51 meta.Meta, | |
50 ), | 52 ), |
51 goldmark.WithParserOptions( | 53 goldmark.WithParserOptions( |
52 parser.WithAutoHeadingID(), | 54 parser.WithAutoHeadingID(), |
53 ), | 55 ), |
54 ) | 56 ) |
55 | 57 |
56 var htmlContent bytes.Buffer | 58 var htmlContent bytes.Buffer |
57 err = md.Convert(source, &htmlContent) | 59 parserContext := parser.NewContext() |
60 err = md.Convert(source, &htmlContent, parser.WithContext(parserContext)) | |
58 if err != nil { | 61 if err != nil { |
59 log.Fatal(err) | 62 log.Fatal(err) |
60 } | 63 } |
61 | 64 |
62 if useTemplate { | 65 if useTemplate { |
63 context := map[string]string{ | 66 templateVars := meta.Get(parserContext) |
64 "content": htmlContent.String(), | 67 templateVars["content"] = htmlContent.String() |
65 } | 68 |
66 err := tmpl.FRender(os.Stdout, context) | 69 err := tmpl.FRender(os.Stdout, templateVars) |
67 if err != nil { | 70 if err != nil { |
68 log.Fatal(err) | 71 log.Fatal(err) |
69 } | 72 } |
70 } else { | 73 } else { |
71 fmt.Print(htmlContent) | 74 fmt.Print(htmlContent) |