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

Ordered kv's in dumps #463

Open Andrew-Morozko opened 2 months ago

Andrew-Morozko commented 2 months ago

I was writing a serializer/deserializer for goldmark AST and was comparing .Dump's output to quickly check that AST survives the roundtrip encoding intact. But since DumpHelper uses Golang's map for the kv's, the output is not deterministic (kv's are swapped randomly).

This PR solves this (admittedly very niche) issue by sorting the kv's by key before printing them out. I used sort.Slice instead of more modern slices.Sort to preserve the minimal supported go version.

I don't think that this issue is worth the pain of changing DumpHelper's signature to use slices for kv, especially since it is a public func, but perhaps it's worth the tiny inefficiency of sorting the values. What do you think?

P.S.: thanks for the great library!