From 80865622974f3410788c59d8c8c438e8814305c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?=
Inner Link: EDITED: https://www.gohugo.io|
") - b.AssertFileContent("public/blog/p7/index.html", "HEADING: With Headings||Level: 1|Anchor: heading-level-1|Text: Heading Level 1|ENDsome text
\nHEADING: With Headings||Level: 2|Anchor: heading-level-2|Text: Heading Level 2|ENDHEADING: With Headings||Level: 3|Anchor: heading-level-3|Text: Heading Level 3|END") + b.AssertFileContent("public/blog/p7/index.html", "HEADING: With Headings||Level: 1|Anchor: heading-level-1|Text: Heading Level 1|Attributes: map[id:heading-level-1]|ENDsome text
\nHEADING: With Headings||Level: 2|Anchor: heading-level-2|Text: Heading Level 2|Attributes: map[id:heading-level-2]|ENDHEADING: With Headings||Level: 3|Anchor: heading-level-3|Text: Heading Level 3|Attributes: map[id:heading-level-3]|END") // https://github.com/gohugoio/hugo/issues/7349 b.AssertFileContent("public/docs/p8/index.html", "Docs Level: 1") diff --git a/markup/converter/hooks/hooks.go b/markup/converter/hooks/hooks.go index 622d503ef05..6f08a2161f9 100644 --- a/markup/converter/hooks/hooks.go +++ b/markup/converter/hooks/hooks.go @@ -19,6 +19,10 @@ import ( "github.com/gohugoio/hugo/identity" ) +type AttributesProvider interface { + Attributes() map[string]string +} + type LinkContext interface { Page() interface{} Destination() string @@ -45,6 +49,9 @@ type HeadingContext interface { Text() string // PlainText is the unrendered version of Text. PlainText() string + + // Attributes (e.g. CSS classes) + AttributesProvider } // HeadingRenderer describes a uniquely identifiable rendering hook. diff --git a/markup/goldmark/render_hooks.go b/markup/goldmark/render_hooks.go index f17e4e13909..da7835127fd 100644 --- a/markup/goldmark/render_hooks.go +++ b/markup/goldmark/render_hooks.go @@ -14,6 +14,10 @@ package goldmark import ( + "sync" + + "github.com/spf13/cast" + "github.com/gohugoio/hugo/markup/converter/hooks" "github.com/yuin/goldmark" @@ -38,6 +42,25 @@ func newLinks() goldmark.Extender { return &links{} } +type attributesHolder struct { + // What we get from Goldmark. + astAttributes []ast.Attribute + + // What we send to the the render hooks. + attributesInit sync.Once + attributes map[string]string +} + +func (a *attributesHolder) Attributes() map[string]string { + a.attributesInit.Do(func() { + a.attributes = make(map[string]string) + for _, attr := range a.astAttributes { + a.attributes[string(attr.Name)] = cast.ToString(attr.Value) + } + }) + return a.attributes +} + type linkContext struct { page interface{} destination string @@ -76,6 +99,7 @@ type headingContext struct { anchor string text string plainText string + *attributesHolder } func (ctx headingContext) Page() interface{} { @@ -301,11 +325,12 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast err := h.HeadingRenderer.RenderHeading( w, headingContext{ - page: ctx.DocumentContext().Document, - level: n.Level, - anchor: string(anchor), - text: string(text), - plainText: string(n.Text(source)), + page: ctx.DocumentContext().Document, + level: n.Level, + anchor: string(anchor), + text: string(text), + plainText: string(n.Text(source)), + attributesHolder: &attributesHolder{astAttributes: n.Attributes()}, }, )