CommonMark icon indicating copy to clipboard operation
CommonMark copied to clipboard

Add NSAttributedString render format

Open larryonoff opened this issue 3 years ago • 3 comments

Hey @mattt!

What do you think about adding support for NSAttributedString?

larryonoff avatar Jul 29 '21 06:07 larryonoff

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

mattt avatar Jul 29 '21 16:07 mattt

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)

larryonoff avatar Jul 29 '21 18:07 larryonoff

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

mattt avatar Jul 29 '21 18:07 mattt