mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
Add autoID for definition terms
Fixes #13403 See #11566 Co-authored-by: Joe Mooring <joe@mooring.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user