Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
document: extract metadata from document and use it
go.mod | 1 + go.sum | 4 ++++ main.go | 15 +++++++++++++-- templates/layout.html | 4 ++++
diff --git a/go.mod b/go.mod index c3a455513200ad612c279c36b5dc6d457be97ec1..12c85197a0145a2ee6686e97900e06915f0ade28 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ require ( github.com/go-chi/chi/v5 v5.0.4 // indirect github.com/yuin/goldmark v1.4.0 // indirect + github.com/yuin/goldmark-meta v1.0.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 2c8ebe6ff5ab5124d20daf60711a361759070027..ddc224d60ed49556458d323c1907e01211191446 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,11 @@ github.com/go-chi/chi/v5 v5.0.4 h1:5e494iHzsYBiyXQAHHuI4tyJS9M3V84OuX3ufIIGHFo= github.com/go-chi/chi/v5 v5.0.4/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.0 h1:OtISOGfH6sOWa1/qXqqAiOIAO6Z5J3AEAE18WAq6BiQ= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark-meta v1.0.0 h1:ScsatUIT2gFS6azqzLGUjgOnELsBOxMXerM3ogdJhAM= +github.com/yuin/goldmark-meta v1.0.0/go.mod h1:zsNNOrZ4nLuyHAJeLQEZcQat8dm70SmB2kHbls092Gc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go index 0f4dee1af426eec3f6d954890eb419a291aa120c..917dfe73b7037404c596d943eb685475cbbf2edb 100644 --- a/main.go +++ b/main.go @@ -14,11 +14,13 @@ "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer/html" + "github.com/yuin/goldmark-meta" ) type Document struct { Title string Content template.HTML + Meta map[string]interface{} } var ( @@ -64,6 +66,7 @@ extension.TaskList, extension.DefinitionList, extension.Strikethrough, extension.Footnote, + meta.Meta, ), goldmark.WithParserOptions( parser.WithAutoHeadingID(), @@ -81,15 +84,23 @@ return } - document := &Document{Title: route} + document := &Document{} + context := parser.NewContext() - if err := md.Convert(doc, &buf); err != nil { + if err := md.Convert(doc, &buf, parser.WithContext(context)); err != nil { requestError(fmt.Sprintf("Couldn't convert markdown due: %v", err), w) return } document.Content = template.HTML(buf.String()) + document.Meta = meta.Get(context) + + title, ok := document.Meta["title"].(string) + if ok { + document.Title = title + } + parsedTemplate, _ := template.ParseFiles("templates/layout.html") err = parsedTemplate.Execute(w, document) if err != nil { diff --git a/templates/layout.html b/templates/layout.html index 418c8941af85b3c2774309ae6ec4f74b74b4d3b9..243c3312c29627b3e5bb77923e4b49e53cae4741 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -43,6 +43,10 @@ <body> <article> + {{ if .Title }} + <h1> {{ .Title }} </h1> + {{ end }} + {{ .Content }} </article> </body>