Replies: 1 comment
-
I ran into the same issue, and I think it's not possible to make this work with the current parser.IDs interface, because its Generate method takes the raw
I implemented a workaround by completely bypassing “auto ids” functionality, and instead doing:
func run() error {
body := []byte(src)
var seen map[string]struct{} // keeps track of seen slugs to avoid duplicate ids
fn := func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering || n.Kind() != ast.KindHeading {
return ast.WalkContinue, nil
}
if name := slugify(nodeText(n, body)); name != "" {
if seen == nil {
seen = make(map[string]struct{})
}
for i := 0; i < 100; i++ {
var cand string
if i == 0 {
cand = name
} else {
cand = fmt.Sprintf("%s-%d", name, i)
}
if _, ok := seen[cand]; !ok {
seen[cand] = struct{}{}
n.SetAttributeString("id", []byte(cand))
break
}
}
}
return ast.WalkContinue, nil
}
doc := goldmark.DefaultParser().Parse(text.NewReader(body))
if err := ast.Walk(doc, fn); err != nil {
return err
}
return goldmark.DefaultRenderer().Render(os.Stdout, body, doc)
} See the full code at https://gist.github.com/artyom/26c2674d459669a38eb8b84f95fa30fb |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It looks like the current logic for automatic header ID generation serializes the entire HTML content of the header.
In my opinion, this is a slightly dangerous practice and certainly not what you would expect from an identifier.
As an example:
Renders as:
Ideally, header identifiers should be based purely on text nodes:
Usually markdown headers are pure text; but having an implementation that behaves strangely like this for the rare cases where content is complex seems dangerous.
Beta Was this translation helpful? Give feedback.
All reactions