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

[Attributed Strings] Can the Styler know which list item marker was parsed? #176

Closed
bgoncal opened this issue Sep 11, 2019 · 5 comments
Closed

Comments

@bgoncal
Copy link

bgoncal commented Sep 11, 2019

Hi, I need to customise a specific part of my markdown string, can I customise just it in some way? Or do I need to create a styler and implement the entire protocol?

Also I think I can't use CSS.

Basically what I need is when I have a list that starts with "+" instead of "*" I need to put a different list style with a specific image

@johnxnguyen
Copy link
Owner

Hi @cpvbruno , unfortunately it isn't possible to differentiate between list items like the way you need to. The reason being is that cmark, the markdown parser that Down uses, does not keep track of which item marker (*, -, or +) it finds, it only cares if one of the three are present. As a result, the styler can't know the which item marker was used to create the item.

If I may ask, what are you trying to achieve? Perhaps I can help you further.

@johnxnguyen johnxnguyen changed the title Can I create a Styler without having to implement everything again? [Attributed Strings] Can I create a Styler without having to implement everything again? Sep 11, 2019
@bgoncal
Copy link
Author

bgoncal commented Sep 11, 2019

It’s a behavior specifically for my company project, they use the “+” and “-“ to create a “ pro and cons” list, putting an image for each of case... so basically a bullet list with special bullets

@johnxnguyen
Copy link
Owner

If you're only concerned with lists and no other markdown, you could forego the use of Down completely. Instead you could...

  1. parse the block of text, splitting it up into the different pro and con items,
  2. build an attributed string, prepending each item with a text attachment to hold the appropriate image, and
  3. adjust the paragraph styles of the attributed string so that it looks like a list.

This wouldn't be hard to do, plus you wouldn't need to depend on another framework that does far more than you need.

If however you do need full markdown support, you could still use the aforementioned approach but it would require a more complicated workaround. You'd need to prevent cmark from parsing the list (however it could still parse the inline elements (bold, italics etc) contained in the list). This could be achieved by using Down to render sections of text (excluding the list) as usual, and then using the method above to render the sections of text exclusively containing the list. You could then join the rendered components back together again to get the final result.

If either of these approaches seem worth trying, let me know and I would be happy to share some tips to point you in the right direction.

@johnxnguyen
Copy link
Owner

@cpvbruno Just a quick follow up, there is an open PR (commonmark/cmark#226) on the cmark repo that would make it possible to determine which list marker was parsed. It's been open for a long time and doesn't seem to be getting any attention, but you could make some noise there to try to get a maintainer to look at it.

@bgoncal
Copy link
Author

bgoncal commented Sep 12, 2019

I'll do it thanks, the first case you mentioned didn't work in my case :/ but thanks anyway

@johnxnguyen johnxnguyen changed the title [Attributed Strings] Can I create a Styler without having to implement everything again? [Attributed Strings] Can the Styler know which list item marker was parsed? Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants