marked icon indicating copy to clipboard operation
marked copied to clipboard

Extend the ListItem interface with ordered and index

Open pejas opened this issue 1 year ago • 7 comments

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

{
  ordered: boolean;
  index: number;
}
  • ordered: Indicates whether the parent List is ordered.
  • index: Represents the starting index plus the item's index.

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.

pejas avatar Jul 04 '24 12:07 pejas

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

UziTech avatar Jul 04 '24 15:07 UziTech

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

UziTech avatar Jul 04 '24 16:07 UziTech

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.

pejas avatar Jul 05 '24 08:07 pejas

@pejas Have you fixed this issue ?

tmatheas avatar Oct 02 '24 03:10 tmatheas

@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

UziTech avatar Oct 02 '24 14:10 UziTech

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

KrishDave1 avatar Oct 10 '24 00:10 KrishDave1

@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.

pejas avatar Oct 13 '24 13:10 pejas

closing this as it can easily be an extension so users can opt-in if they need it. see https://github.com/markedjs/marked/issues/3359#issuecomment-2209320597

UziTech avatar May 18 '25 14:05 UziTech