Fix extra newline/paragraphs issue with .RenderShortcodes

Fixes #13051
This commit is contained in:
Bjørn Erik Pedersen
2024-11-16 09:56:25 +01:00
parent 2c54c32986
commit d4de780edc
3 changed files with 88 additions and 7 deletions

View File

@@ -575,7 +575,7 @@ sc3_begin|{{ .Inner }}|sc3_end
// Issue #7332
"<span>:x:\n</span>",
// Issue #11587
"<p>&#x2714;&#xfe0f;\n</p>",
"<p>&#x2714;&#xfe0f;</p>",
// Should not be converted to emoji
"sc1_begin|:smiley:|sc1_end",
// Should be converted to emoji

View File

@@ -242,6 +242,39 @@ func (r *hugoContextRenderer) handleHugoContext(w util.BufWriter, source []byte,
return ast.WalkContinue, nil
}
type hugoContextTransformer struct{}
var _ parser.ASTTransformer = (*hugoContextTransformer)(nil)
func (a *hugoContextTransformer) Transform(n *ast.Document, reader text.Reader, pc parser.Context) {
ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
s := ast.WalkContinue
if !entering || n.Kind() != kindHugoContext {
return s, nil
}
if p, ok := n.Parent().(*ast.Paragraph); ok {
if p.ChildCount() == 1 {
// Avoid empty paragraphs.
p.Parent().ReplaceChild(p.Parent(), p, n)
} else {
if t, ok := n.PreviousSibling().(*ast.Text); ok {
// Remove the newline produced by the Hugo context markers.
if t.SoftLineBreak() {
if t.Segment.Len() == 0 {
p.RemoveChild(p, t)
} else {
t.SetSoftLineBreak(false)
}
}
}
}
}
return s, nil
})
}
type hugoContextExtension struct {
logger loggers.Logger
}
@@ -251,6 +284,7 @@ func (a *hugoContextExtension) Extend(m goldmark.Markdown) {
parser.WithInlineParsers(
util.Prioritized(&hugoContextParser{}, 50),
),
parser.WithASTTransformers(util.Prioritized(&hugoContextTransformer{}, 10)),
)
m.Renderer().AddOptions(