Add autoID for definition terms

Fixes #13403
See #11566

Co-authored-by: Joe Mooring <joe@mooring.com>
This commit is contained in:
Bjørn Erik Pedersen
2025-02-15 17:13:20 +01:00
parent 9c2f8ec61b
commit 157d3703c3
9 changed files with 262 additions and 47 deletions

View File

@@ -1,8 +1,11 @@
package attributes
import (
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
"github.com/gohugoio/hugo/markup/goldmark/internal/render"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
east "github.com/yuin/goldmark/extension/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
@@ -14,24 +17,29 @@ import (
var (
kindAttributesBlock = ast.NewNodeKind("AttributesBlock")
attrNameID = []byte("id")
defaultParser = new(attrParser)
defaultTransformer = new(transformer)
attributes goldmark.Extender = new(attrExtension)
defaultParser = new(attrParser)
)
func New() goldmark.Extender {
return attributes
func New(cfg goldmark_config.Parser) goldmark.Extender {
return &attrExtension{cfg: cfg}
}
type attrExtension struct{}
type attrExtension struct {
cfg goldmark_config.Parser
}
func (a *attrExtension) Extend(m goldmark.Markdown) {
if a.cfg.Attribute.Block {
m.Parser().AddOptions(
parser.WithBlockParsers(
util.Prioritized(defaultParser, 100)),
)
}
m.Parser().AddOptions(
parser.WithBlockParsers(
util.Prioritized(defaultParser, 100)),
parser.WithASTTransformers(
util.Prioritized(defaultTransformer, 100),
util.Prioritized(&transformer{cfg: a.cfg}, 100),
),
)
}
@@ -92,18 +100,47 @@ func (a *attributesBlock) Kind() ast.NodeKind {
return kindAttributesBlock
}
type transformer struct{}
type transformer struct {
cfg goldmark_config.Parser
}
func (a *transformer) isFragmentNode(n ast.Node) bool {
switch n.Kind() {
case east.KindDefinitionTerm, ast.KindHeading:
return true
default:
return false
}
}
func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
attributes := make([]ast.Node, 0, 500)
var attributes []ast.Node
if a.cfg.Attribute.Block {
attributes = make([]ast.Node, 0, 500)
}
ast.Walk(node, func(node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering && node.Kind() == kindAttributesBlock {
if !entering {
return ast.WalkContinue, nil
}
if a.isFragmentNode(node) {
if id, found := node.Attribute(attrNameID); !found {
a.generateAutoID(node, reader, pc)
} else {
pc.IDs().Put(id.([]byte))
}
}
if a.cfg.Attribute.Block && node.Kind() == kindAttributesBlock {
// Attributes for fenced code blocks are handled in their own extension,
// but note that we currently only support code block attributes when
// CodeFences=true.
if node.PreviousSibling() != nil && node.PreviousSibling().Kind() != ast.KindFencedCodeBlock && !node.HasBlankPreviousLines() {
attributes = append(attributes, node)
return ast.WalkSkipChildren, nil
} else {
// remove attributes node
node.Parent().RemoveChild(node.Parent(), node)
}
}
@@ -123,3 +160,33 @@ func (a *transformer) Transform(node *ast.Document, reader text.Reader, pc parse
attr.Parent().RemoveChild(attr.Parent(), attr)
}
}
func (a *transformer) generateAutoID(n ast.Node, reader text.Reader, pc parser.Context) {
var text []byte
switch n := n.(type) {
case *ast.Heading:
if a.cfg.AutoHeadingID {
text = textHeadingID(n, reader)
}
case *east.DefinitionTerm:
if a.cfg.AutoDefinitionTermID {
text = []byte(render.TextPlain(n, reader.Source()))
}
}
if len(text) > 0 {
headingID := pc.IDs().Generate(text, n.Kind())
n.SetAttribute(attrNameID, headingID)
}
}
// Markdown settext headers can have multiple lines, use the last line for the ID.
func textHeadingID(node *ast.Heading, reader text.Reader) []byte {
var line []byte
lastIndex := node.Lines().Len() - 1
if lastIndex > -1 {
lastLine := node.Lines().At(lastIndex)
line = lastLine.Value(reader.Source())
}
return line
}

View File

@@ -0,0 +1,74 @@
package attributes_test
import (
"testing"
"github.com/gohugoio/hugo/hugolib"
)
func TestDescriptionListAutoID(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
[markup.goldmark.parser]
autoHeadingID = true
autoDefinitionTermID = true
autoIDType = 'github-ascii'
-- content/p1.md --
---
title: "Title"
---
## Title with id set {#title-with-id}
## Title with id set duplicate {#title-with-id}
## My Title
Base Name
: Base name of the file.
Base Name
: Duplicate term name.
My Title
: Term with same name as title.
Foo@Bar
: The foo bar.
foo [something](/a/b/) bar
: A foo bar.
良善天父
: The good father.
Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď
: Testing accents.
Mutiline set text header
Second line
---------------
-- layouts/_default/single.html --
{{ .Content }}|Identifiers: {{ .Fragments.Identifiers }}|
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/p1/index.html",
`<dt id="base-name">Base Name</dt>`,
`<dt id="base-name-1">Base Name</dt>`,
`<dt id="foobar">Foo@Bar</dt>`,
`<h2 id="my-title">My Title</h2>`,
`<dt id="foo-something-bar">foo <a href="/a/b/">something</a> bar</dt>`,
`<h2 id="title-with-id">Title with id set</h2>`,
`<h2 id="title-with-id">Title with id set duplicate</h2>`,
`<dt id="my-title-1">My Title</dt>`,
`<dt id="term">良善天父</dt>`,
`<dt id="a-a-a-a-a-a-c-c-c-c-c-c-c-c-d">Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď</dt>`,
`<h2 id="second-line">Mutiline set text header`,
"|Identifiers: [a-a-a-a-a-a-c-c-c-c-c-c-c-c-d base-name base-name-1 foo-something-bar foobar my-title my-title-1 second-line term title-with-id title-with-id]|",
)
}