mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-20 23:11:43 +00:00
Allow markdown attribute lists to be used in title render hooks
Fixes #8270
This commit is contained in:
parent
e7e194435b
commit
cd0c5d7ef3
4 changed files with 54 additions and 7 deletions
|
@ -179,6 +179,9 @@ Text
|
||||||
PlainText
|
PlainText
|
||||||
: The plain variant of the above.
|
: The plain variant of the above.
|
||||||
|
|
||||||
|
Attributes (map) {{< new-in "0.82.0" >}}
|
||||||
|
: A map of attributes (e.g. `id`, `class`)
|
||||||
|
|
||||||
#### Link with title Markdown example:
|
#### Link with title Markdown example:
|
||||||
|
|
||||||
```md
|
```md
|
||||||
|
|
|
@ -60,6 +60,16 @@ func TestRenderHooks(t *testing.T) {
|
||||||
config := `
|
config := `
|
||||||
baseURL="https://example.org"
|
baseURL="https://example.org"
|
||||||
workingDir="/mywork"
|
workingDir="/mywork"
|
||||||
|
|
||||||
|
[markup]
|
||||||
|
[markup.goldmark]
|
||||||
|
[markup.goldmark.parser]
|
||||||
|
autoHeadingID = true
|
||||||
|
autoHeadingIDType = "github"
|
||||||
|
[markup.goldmark.parser.attribute]
|
||||||
|
block = true
|
||||||
|
title = true
|
||||||
|
|
||||||
`
|
`
|
||||||
b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running()
|
b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running()
|
||||||
b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`)
|
b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`)
|
||||||
|
@ -85,7 +95,7 @@ Inner Block: {{ .Inner | .Page.RenderString (dict "display" "block" ) }}
|
||||||
b.WithTemplatesAdded("_default/_markup/render-link.html", `{{ with .Page }}{{ .Title }}{{ end }}|{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
|
b.WithTemplatesAdded("_default/_markup/render-link.html", `{{ with .Page }}{{ .Title }}{{ end }}|{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
|
||||||
b.WithTemplatesAdded("docs/_markup/render-link.html", `Link docs section: {{ .Text | safeHTML }}|END`)
|
b.WithTemplatesAdded("docs/_markup/render-link.html", `Link docs section: {{ .Text | safeHTML }}|END`)
|
||||||
b.WithTemplatesAdded("_default/_markup/render-image.html", `IMAGE: {{ .Page.Title }}||{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
|
b.WithTemplatesAdded("_default/_markup/render-image.html", `IMAGE: {{ .Page.Title }}||{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
|
||||||
b.WithTemplatesAdded("_default/_markup/render-heading.html", `HEADING: {{ .Page.Title }}||Level: {{ .Level }}|Anchor: {{ .Anchor | safeURL }}|Text: {{ .Text | safeHTML }}|END`)
|
b.WithTemplatesAdded("_default/_markup/render-heading.html", `HEADING: {{ .Page.Title }}||Level: {{ .Level }}|Anchor: {{ .Anchor | safeURL }}|Text: {{ .Text | safeHTML }}|Attributes: {{ .Attributes }}|END`)
|
||||||
b.WithTemplatesAdded("docs/_markup/render-heading.html", `Docs Level: {{ .Level }}|END`)
|
b.WithTemplatesAdded("docs/_markup/render-heading.html", `Docs Level: {{ .Level }}|END`)
|
||||||
|
|
||||||
b.WithContent("customview/p1.md", `---
|
b.WithContent("customview/p1.md", `---
|
||||||
|
@ -108,6 +118,10 @@ Image:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
|
||||||
|
## Some Heading {.text-serif #a-heading title="Hovered"}
|
||||||
|
|
||||||
|
|
||||||
`, "blog/p2.md", `---
|
`, "blog/p2.md", `---
|
||||||
title: Cool Page2
|
title: Cool Page2
|
||||||
|
@ -238,7 +252,7 @@ SHORT3|
|
||||||
// We may add type template support later, keep this for then. b.AssertFileContent("public/docs/docs1/index.html", `DOCS EDITED: https://www.google.com|</p>`)
|
// We may add type template support later, keep this for then. b.AssertFileContent("public/docs/docs1/index.html", `DOCS EDITED: https://www.google.com|</p>`)
|
||||||
b.AssertFileContent("public/blog/p4/index.html", `IMAGE EDITED: /images/Dragster.jpg|`)
|
b.AssertFileContent("public/blog/p4/index.html", `IMAGE EDITED: /images/Dragster.jpg|`)
|
||||||
b.AssertFileContent("public/blog/p6/index.html", "<p>Inner Link: EDITED: https://www.gohugo.io|</p>")
|
b.AssertFileContent("public/blog/p6/index.html", "<p>Inner Link: EDITED: https://www.gohugo.io|</p>")
|
||||||
b.AssertFileContent("public/blog/p7/index.html", "HEADING: With Headings||Level: 1|Anchor: heading-level-1|Text: Heading Level 1|END<p>some text</p>\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]|END<p>some text</p>\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
|
// https://github.com/gohugoio/hugo/issues/7349
|
||||||
b.AssertFileContent("public/docs/p8/index.html", "Docs Level: 1")
|
b.AssertFileContent("public/docs/p8/index.html", "Docs Level: 1")
|
||||||
|
|
|
@ -19,6 +19,10 @@ import (
|
||||||
"github.com/gohugoio/hugo/identity"
|
"github.com/gohugoio/hugo/identity"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type AttributesProvider interface {
|
||||||
|
Attributes() map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
type LinkContext interface {
|
type LinkContext interface {
|
||||||
Page() interface{}
|
Page() interface{}
|
||||||
Destination() string
|
Destination() string
|
||||||
|
@ -45,6 +49,9 @@ type HeadingContext interface {
|
||||||
Text() string
|
Text() string
|
||||||
// PlainText is the unrendered version of Text.
|
// PlainText is the unrendered version of Text.
|
||||||
PlainText() string
|
PlainText() string
|
||||||
|
|
||||||
|
// Attributes (e.g. CSS classes)
|
||||||
|
AttributesProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
// HeadingRenderer describes a uniquely identifiable rendering hook.
|
// HeadingRenderer describes a uniquely identifiable rendering hook.
|
||||||
|
|
|
@ -14,6 +14,8 @@
|
||||||
package goldmark
|
package goldmark
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/markup/converter/hooks"
|
"github.com/gohugoio/hugo/markup/converter/hooks"
|
||||||
|
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
|
@ -38,6 +40,25 @@ func newLinks() goldmark.Extender {
|
||||||
return &links{}
|
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)] = string(util.EscapeHTML(attr.Value.([]byte)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return a.attributes
|
||||||
|
}
|
||||||
|
|
||||||
type linkContext struct {
|
type linkContext struct {
|
||||||
page interface{}
|
page interface{}
|
||||||
destination string
|
destination string
|
||||||
|
@ -76,6 +97,7 @@ type headingContext struct {
|
||||||
anchor string
|
anchor string
|
||||||
text string
|
text string
|
||||||
plainText string
|
plainText string
|
||||||
|
*attributesHolder
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx headingContext) Page() interface{} {
|
func (ctx headingContext) Page() interface{} {
|
||||||
|
@ -301,11 +323,12 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast
|
||||||
err := h.HeadingRenderer.RenderHeading(
|
err := h.HeadingRenderer.RenderHeading(
|
||||||
w,
|
w,
|
||||||
headingContext{
|
headingContext{
|
||||||
page: ctx.DocumentContext().Document,
|
page: ctx.DocumentContext().Document,
|
||||||
level: n.Level,
|
level: n.Level,
|
||||||
anchor: string(anchor),
|
anchor: string(anchor),
|
||||||
text: string(text),
|
text: string(text),
|
||||||
plainText: string(n.Text(source)),
|
plainText: string(n.Text(source)),
|
||||||
|
attributesHolder: &attributesHolder{astAttributes: n.Attributes()},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue