markedjs / marked

A markdown parser and compiler. Built for speed.
https://marked.js.org
Other
33.01k stars 3.39k forks source link

Extend the ListItem interface with ordered and index #3359

Open pejas opened 3 months ago

pejas commented 3 months ago

Describe the feature Extend the ListItem interface with two additional fields:

{
  ordered: boolean;
  index: number;
}

Why is this feature necessary? Android's TextView does not support ordered lists natively. To work around this limitation, I need to simulate ordered lists using <span> and <br/> tags. This feature will facilitate that process. You can find more details in the Styling with HTML markup documentation.

Describe alternatives you've considered Currently, I have to parse the body of the list and manually mark each item in the listitem:

{
  listitem(text: string, task: boolean, checked: boolean): string {
    return text.trim() + '__LIST_ITEM__';
  },
  list(body: string, ordered: boolean, start: number | ''): string {
    const items = body.split('__LIST_ITEM__');
    // Ugly workaround
  }
}

I have reviewed the marked.js code and believe that this feature should be straightforward to implement.

UziTech commented 3 months ago

That sounds good to me. Do you want to create a PR to implement it?

UziTech commented 3 months ago

This could also be done with walkTokens. Something like:

const walkTokens = (token) => {
  if (token.type === 'list') {
    token.listItems.forEach((item, idx) => {
      item.ordered = token.ordered;
      item.index = idx;
    })
  }
}

marked use({walkTokens})

with marked v13+ the whole token is sent to the renderer so any changes in walkTokens will make it to the renderer

pejas commented 3 months ago

That sounds good to me. Do you want to create a PR to implement it?

Yes, I do. I will prepare a PR with the described change.

This could also be done with walkTokens. Something like:

Thank you, it will look much better for the time being.

tmatheas commented 2 weeks ago

@pejas Have you fixed this issue ?

UziTech commented 2 weeks ago

@tmatheas this is not fixed in marked

See https://github.com/markedjs/marked/issues/3359#issuecomment-2209320597 for a way to add properties with an extension. Or feel free to submit a PR to add the properties

KrishDave1 commented 1 week ago

@pejas @tmatheas @UziTech Has this issue been resolved.If not, I would like to work on this issue.

pejas commented 5 days ago

@pejas @tmatheas @UziTech Has this issue been resolved.If not, I would like to work on this issue.

Feel free I've used a walk tree workaround and this ticket just sits in my backlog with a low priority.