yuin / goldmark

:trophy: A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.
MIT License
3.68k stars 255 forks source link

Footnote in a footnote appears before other footnotes #410

Open kuruczgy opened 1 year ago

kuruczgy commented 1 year ago

goldmark has https://github.com/yuin/goldmark/discussions in github. You should post only issues here. Feature requests and questions should be posted at discussions.

Please answer the following before submitting your issue:

  1. What version of goldmark are you using? : goldmark: v1.5.4, https://github.com/kpym/gm: v0.11.4
  2. What version of Go are you using? : go version go1.21.0 linux/amd64
  3. What operating system and processor architecture are you using? : linux/amd64
  4. What did you do? : gm test.md Where test.md contains:
before[^before]

[^before]: before

parent[^parent]

[^parent]: parent[^child]

[^child]: child

after[^after]

[^after]: after
  1. What did you expect to see? : Using https://michelf.ca/projects/php-markdown/dingus/, the order of the footnotes is:
  2. before
  3. parent
  4. after
  5. child
  6. What did you see instead? : The order the footnotes appear in index.html is the following:
  7. before
  8. child
  9. parent
  10. after
  11. Did you confirm your output is different from CommonMark online demo or other official renderer correspond with an extension?: Yes, see above.
kuruczgy commented 1 year ago

Workaround for anyone finding this issue:

diff --git a/extension/footnote.go b/extension/footnote.go
index d1b67aa..b856d92 100644
--- a/extension/footnote.go
+++ b/extension/footnote.go
@@ -91,8 +91,8 @@ func (b *footnoteBlockParser) Close(node gast.Node, reader text.Reader, pc parse
    } else {
        list = ast.NewFootnoteList()
        pc.Set(footnoteListKey, list)
-       node.Parent().InsertBefore(node.Parent(), node, list)
    }
+   node.Parent().InsertBefore(node.Parent(), node, list)
    node.Parent().RemoveChild(node.Parent(), node)
    list.AppendChild(list, node)
 }

This solution seems kind of janky to me though, so I didn't want to submit it as a PR without prior discussion.

Also, the ideal order IMO would be before parent child after (which is what GitHub seems to do), but achieving that probably requires some additional logic.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] commented 12 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 11 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.

mwat56 commented 7 months ago

Ping