swiftlang/swift-markdown

How to get item index in OrderedList?

fsy2001 opened this issue · 1 comments

I didn't found any property in ListItem referring to the item's index in OrderedList. When calling format() on OrderedList, it converts all the indices to "1. ". This problem arises when I try to parse a ordered list starting from a number other than 1, e.g., 3. 4. 5.

A specific list item's index is meant to be calculated based on the list's starting index and its position in the list. You can use the .indexInParent property to determine what the list item's position is, but that will always start from 0. The format() call echoes the Markdown syntax of not caring about how the list is numbered in the source, just what the list started with. You can use the orderedListNumerals field of MarkupFormatter.Options to change how format() renders a list, but it requires you to give the starting index yourself. (I've filed an issue to allow it to defer to the markdown.)

Specifically starting lists with something other than 1 will be fixed by #22, though that's only meant to add a property to track what the list as a whole started with. You'll still need to do the individual-item calculation yourself.