Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Markdown widget isn't handling ordered lists correctly #2002

Closed
davep opened this issue Mar 9, 2023 · 5 comments · Fixed by #2177
Closed

The Markdown widget isn't handling ordered lists correctly #2002

davep opened this issue Mar 9, 2023 · 5 comments · Fixed by #2177
Assignees
Labels
bug Something isn't working Task

Comments

@davep
Copy link
Contributor

davep commented Mar 9, 2023

In Markdown the number prefix for an item in an ordered list normally isn't important. Every item can start with any valid numeric value; rendering will have them as an ordered sequence of numbers starting with the number of the first item.

For example:

1. One
1. Two
1. Three
1. Four
1. Five

comes out as:

  1. One
  2. Two
  3. Three
  4. Four
  5. Five

However, in our Markdown widget at the moment, it appears like this:

Screenshot 2023-03-09 at 13 04 05

This may be down to the underlying markdown parser we're using. It says it conforms to the CommonMark specification, and that specification does state:

The start number of an ordered list is determined by the list number of its initial list item. The numbers of subsequent list items are disregarded.
-- Section 5.3 of v0.30

so either we need to do something at our end to do that or enable that, or we may need to raise an issue upstream.

@davep davep added bug Something isn't working Task labels Mar 9, 2023
@davep
Copy link
Contributor Author

davep commented Mar 9, 2023

Trying the live demo it looks like that does the right thing; which would suggest that the handling of this might need to be done in the code that uses the library (I'm not saying that for certain; just that on the surface it suggests that).

@TomJGooding
Copy link
Contributor

TomJGooding commented Mar 9, 2023

Rendering html as shown in the live demo would only involve creating the appropriate <ol> and <li> tags wouldn't it, rather than any actual numbering logic?

According to the docs for token.info, this is "The string value of the item marker for ordered-list “list_item_open” tokens".

So possibly the code here is actually just using the number from the original?

        ...
        for token in parser.parse(markdown):
            ...
            elif token.type == "list_item_open":
                if token.info:
                    stack.append(MarkdownOrderedListItem(f"{token.info}. "))

@davep
Copy link
Contributor Author

davep commented Mar 9, 2023

Yup, I think we'll need to pull out the initial value and do some increment of our own.

@TomJGooding
Copy link
Contributor

TomJGooding commented Mar 9, 2023

I suppose an alternative if you wanted to avoid adding your own code to handle this would be to run the markdown through a formatter before trying to parse it?

I haven't read the docs in much detail yet but something like...

import mdformat

unformatted = """
1. One
1. Two
1. Three
1. Four
1. Five
"""

formatted = mdformat.text(
    unformatted,
    options={
        "number": True,  # switch on consecutive numbering of ordered lists
    },
)

print(formatted)
# 1. One
# 2. Two
# 3. Three
# 4. Four
# 5. Five

@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Task
Projects
None yet
2 participants