neilalexander / seaglass

A truly native Matrix client for macOS - written in Swift/Cocoa, with E2E encryption support
Other
524 stars 37 forks source link

Display rich text from formatted_body rather than guessing at MD from the plain body #52

Open ara4n opened 6 years ago

ara4n commented 6 years ago

There's no guarantee that the plain body will contain MD, and despite formatted_body not being properly specified yet, you really should be displaying rich text bodies by parsing the HTML rather than parsing MD from the plain-text body in the desperate hope that it actually contains MD.

Parsing the HTML subset we use should be no harder and in fact substantially less magical than parsing MD.

You could use something like https://github.com/scinfu/SwiftSoup, and just build out the required NSAttributedString components from the resulting parse tree. The only tags you need to do are:

        'font', // custom to matrix for IRC-style font coloring
        'del', // for markdown
        'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol', 'sup', 'sub',
        'nl', 'li', 'b', 'i', 'u', 'strong', 'em', 'strike', 'code', 'hr', 'br', 'div',
        'pre', 'span', 'img',

...all of which should map straight through to NSAttributedString stuff, with the possible exception of img and hr.

I'd ignore the table tags ('table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td') given commonmark can't generate them atm.

This would then pave the way for things like Replies to work correctly (as per #51).

neilalexander commented 6 years ago

SwiftSoup looks very promising - I'll have to take a look at this.

The built-in WebKit HTML-to-NSAttributedString parser worked fine when it worked but created random crashes that I couldn't trace back to anywhere specific, hence why we're currently using the Markdown instead.

ara4n commented 6 years ago

ftr one can follow the spec bug for finally speccing the html subset that org.matrix.custom.html supports over at https://github.com/matrix-org/matrix-doc/issues/1560. although we still want to replace this with a better fallback solution in future.

-- Matthew Hodgson Matrix.org

On 26 Aug 2018, at 11:53, Neil Alexander notifications@github.com wrote:

SwiftSoup looks very promising - I'll have to take a look at this.

The built-in WebKit HTML-to-NSAttributedString parser worked fine when it worked but created random crashes that I couldn't trace back to anywhere specific, hence why we're currently using the Markdown instead.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.