mattt / CommonMarkAttributedString

Create NSAttributedStrings from Markdown Text
MIT License
424 stars 19 forks source link

Custom strategies #6

Open regexident opened 4 years ago

regexident commented 4 years ago

Hi @mattt,

I'm currently working on integrating CommonMarkAttributedString into Nio, a Matrix client.

Since Matrix supports rich-text messages using common-mark markdown it would be great if the app could preview the formatting of one's message before sending, similar to how WhatsApp does it:

whatsapp

It basically parses the message body and applies the corresponding attributes to the raw markdown input, while reserving the markup characters.

I wonder if a migration to CommonMark's upcoming visitor API could make custom strategies like this possible?

I'm not exactly sure what would be the best API for this, but one should be able to customize both, the output strings and corresponding attributes.

mattt commented 4 years ago

I'm really glad to hear that you're considering CommonMarkAttributedString for Nio, @regexident!

The feature you're describing looks really cool. My first reaction is to say that this is likely outside the scope of this library, and that making this work correctly — that is, performantly and correctly wrt/input methods — will require thoughtful coordination between NSTextStorage and NSLayoutManager. To that end, I suspect you'll have to drop down to using CommonMark for parsing and AST manipulation (for which the visitor API may be quite helpful).

If you haven't already, you should take a look at the excellent WWDC session from 2018 "TextKit Best Practices". Not only does it provide a great overview, but it uses a WYSIWYG-style Markdown editor as an example!

Beyond that, you might also take a look at projects like Marklight, which implement similar functionality.