SwiftDocOrg / CommonMark

Create, parse, and render Markdown text according to the CommonMark specification
MIT License
179 stars 10 forks source link

Add NSAttributedString render format #33

Open larryonoff opened 3 years ago

larryonoff commented 3 years ago

Hey @mattt!

What do you think about adding support for NSAttributedString?

mattt commented 3 years ago

I have another project that does just that: https://github.com/mattt/commonmarkattributedstring.

larryonoff commented 3 years ago

I have another project that does just that: https://github.com/mattt/commonmarkattributedstring.

It looks great, but there's no way to style CommonMark elements separately in one string, e.g.

var attributes = CommonMark.Attributes()
attributes.body = [
    .font: NSFont.systemFont(ofSize: NSFont.systemFontSize),
    .foregroundColor: NSColor.textColor,
    .backgroundColor: NSColor.textBackgroundColor,
]
attributes.h1 = [
    .font: NSFont.systemFont(ofSize: NSFont.systemFontSize),
    .foregroundColor: NSColor.textColor,
    .backgroundColor: NSColor.textBackgroundColor,
]

let attributedString = try NSAttributedString(commonmark: commonmark, attributes: attributes)
mattt commented 3 years ago

@larryonoff There's no API for it (yet), but it would be pretty straightforward to customize how things render by modifying this file in a fork: https://github.com/mattt/CommonMarkAttributedString/blob/master/Sources/CommonMarkAttributedString/CommonMark%2BExtensions.swift

Alternatively, you could use the visitor API to build up an attributed string block by block.