mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-07 06:36:19 +00:00
markup/goldmark/codeblock: Fix attributes when no language identifier in CodeBlock
Fixes #10118
This commit is contained in:
parent
3fefea06b8
commit
cbdaff2135
2 changed files with 42 additions and 2 deletions
|
@ -265,6 +265,39 @@ Position: {{ .Position | safeHTML }}
|
||||||
b.AssertFileContent("public/p1/index.html", filepath.FromSlash("Position: \"/content/p1.md:7:1\""))
|
b.AssertFileContent("public/p1/index.html", filepath.FromSlash("Position: \"/content/p1.md:7:1\""))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 10118
|
||||||
|
func TestAttributes(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
files := `
|
||||||
|
-- config.toml --
|
||||||
|
-- content/p1.md --
|
||||||
|
---
|
||||||
|
title: "p1"
|
||||||
|
---
|
||||||
|
|
||||||
|
## Issue 10118
|
||||||
|
|
||||||
|
§§§ {foo="bar"}
|
||||||
|
Hello, World!
|
||||||
|
§§§
|
||||||
|
|
||||||
|
-- layouts/_default/single.html --
|
||||||
|
{{ .Content }}
|
||||||
|
-- layouts/_default/_markup/render-codeblock.html --
|
||||||
|
Attributes: {{ .Attributes }}|Type: {{ .Type }}|
|
||||||
|
`
|
||||||
|
|
||||||
|
b := hugolib.NewIntegrationTestBuilder(
|
||||||
|
hugolib.IntegrationTestConfig{
|
||||||
|
T: t,
|
||||||
|
TxtarString: files,
|
||||||
|
},
|
||||||
|
).Build()
|
||||||
|
|
||||||
|
b.AssertFileContent("public/p1/index.html", "<h2 id=\"issue-10118\">Issue 10118</h2>\nAttributes: map[foo:bar]|Type: |")
|
||||||
|
}
|
||||||
|
|
||||||
// Issue 9571
|
// Issue 9571
|
||||||
func TestAttributesChroma(t *testing.T) {
|
func TestAttributesChroma(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
|
@ -16,6 +16,7 @@ package codeblocks
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/alecthomas/chroma/v2/lexers"
|
"github.com/alecthomas/chroma/v2/lexers"
|
||||||
|
@ -69,7 +70,7 @@ func (r *htmlRenderer) renderCodeBlock(w util.BufWriter, src []byte, node ast.No
|
||||||
}
|
}
|
||||||
|
|
||||||
n := node.(*codeBlock)
|
n := node.(*codeBlock)
|
||||||
lang := string(n.b.Language(src))
|
lang := getLang(n.b, src)
|
||||||
renderer := ctx.RenderContext().GetRenderer(hooks.CodeBlockRendererType, lang)
|
renderer := ctx.RenderContext().GetRenderer(hooks.CodeBlockRendererType, lang)
|
||||||
if renderer == nil {
|
if renderer == nil {
|
||||||
return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
|
return ast.WalkStop, fmt.Errorf("no code renderer found for %q", lang)
|
||||||
|
@ -174,6 +175,12 @@ func (c *codeBlockContext) Position() htext.Position {
|
||||||
return c.pos
|
return c.pos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getLang(node *ast.FencedCodeBlock, src []byte) string {
|
||||||
|
langWithAttributes := string(node.Language(src))
|
||||||
|
lang, _, _ := strings.Cut(langWithAttributes, "{")
|
||||||
|
return lang
|
||||||
|
}
|
||||||
|
|
||||||
func getAttributes(node *ast.FencedCodeBlock, infostr []byte) []ast.Attribute {
|
func getAttributes(node *ast.FencedCodeBlock, infostr []byte) []ast.Attribute {
|
||||||
if node.Attributes() != nil {
|
if node.Attributes() != nil {
|
||||||
return node.Attributes()
|
return node.Attributes()
|
||||||
|
@ -188,7 +195,7 @@ func getAttributes(node *ast.FencedCodeBlock, infostr []byte) []ast.Attribute {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if attrStartIdx > 0 {
|
if attrStartIdx != -1 {
|
||||||
n := ast.NewTextBlock() // dummy node for storing attributes
|
n := ast.NewTextBlock() // dummy node for storing attributes
|
||||||
attrStr := infostr[attrStartIdx:]
|
attrStr := infostr[attrStartIdx:]
|
||||||
if attrs, hasAttr := parser.ParseAttributes(text.NewReader(attrStr)); hasAttr {
|
if attrs, hasAttr := parser.ParseAttributes(text.NewReader(attrStr)); hasAttr {
|
||||||
|
|
Loading…
Add table
Reference in a new issue