You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I needed to patch SwiftyMarkdown into existing code. Styles needed to be compatible, strongly typed, consistent and readable. Here's my example of usage. Hope it helps someone.
Detail:
init() takes a list of markdown styles such as body and applies StyleProperty to them. func applyStyles copies the custom properties to the styles. enum StyleProperty provides the custom properties structure.
Code:
import AppKit
import SwiftUI
import SwiftyMarkdown
classMarkdownHelper{staticvarshared:MarkdownHelper=MarkdownHelper()staticletcustomFontName="Bitstream Vera Sans Mono"varmd:SwiftyMarkdownprivateinit(){letmd:SwiftyMarkdown=SwiftyMarkdown(string:"")self.md = md
letsize=14applyStyles(stylesMapping:[
md.body:[ // the default style
.fontName(customFontName),.fontStyle(.normal),.fontSize(size),.color(PhosphorColors.white)],
md.code:[ // the code style
.fontName(customFontName),.fontStyle(.normal),.fontSize(size),.color(PhosphorColors.white),],
md.h1:[.fontName(customFontName),.fontStyle(.bold),.fontSize(size),.color(PhosphorColors.brightWhite)],
md.h2:[.fontName(customFontName),.fontStyle(.bold),.fontSize(size),.color(PhosphorColors.brightAmber)],
md.h3:[.fontName(customFontName),.fontStyle(.bold),.fontSize(size),.color(PhosphorColors.brightCyan)],
md.italic:[.fontName(customFontName),.fontStyle(.italic),.fontSize(size),.color(PhosphorColors.green)],
md.bold:[.fontName(customFontName),.fontStyle(.bold),.fontSize(size),.color(PhosphorColors.overBrightGreen)]])}enumStyleProperty{case fontName(String)case color(NSColor)case fontSize(CGFloat)case fontStyle(FontStyle)case alignment(NSTextAlignment)case lineSpacing(CGFloat)case paragraphSpacing(CGFloat)case underlineStyle(NSUnderlineStyle)case underlineColor(NSColor)}func applyStyles(stylesMapping:[NSObject:[StyleProperty]]){
// Applying styles
for(instance, properties)in stylesMapping {forpropertyin properties {switch property {case.fontName(let value):(instance as?BasicStyles)?.fontName = value
(instance as?LineStyles)?.fontName = value
case.color(let value):(instance as?BasicStyles)?.color = value
(instance as?LineStyles)?.color = value
case.fontSize(let value):(instance as?BasicStyles)?.fontSize = value
(instance as?LineStyles)?.fontSize = value
case.fontStyle(let value):(instance as?BasicStyles)?.fontStyle = value
(instance as?LineStyles)?.fontStyle = value
case.alignment(let value):(instance as?LineStyles)?.alignment = value
case.lineSpacing(let value):(instance as?LineStyles)?.lineSpacing = value
case.paragraphSpacing(let value):(instance as?LineStyles)?.paragraphSpacing = value
case.underlineStyle(let value):(instance as?LinkStyles)?.underlineStyle = value
case.underlineColor(let value):(instance as?LinkStyles)?.underlineColor = value
}}}}func parse(markdown:String)->NSAttributedString{
md.string = markdown.replacingOccurrences(of:"\r\n", with:"\n") // the presumption is that styles do not change, otherwise we'd reinitialise all styles each time.
return md.attributedString()}structPhosphorColors{staticletwhite:NSColor=NSColor(Color(#colorLiteral(red:0.9159484512, green:0.9159484512, blue:0.9463420244, alpha:1)))staticletamber:NSColor=NSColor(Color(#colorLiteral(red:0.990, green:0.857, blue:0.205, alpha:1)))staticletdarkAmber:NSColor=NSColor(Color(#colorLiteral(red:0.937, green:0.622, blue:0.227, alpha:1)))staticletmediumBrightBlue:NSColor=NSColor(Color(#colorLiteral(red:0.699, green:0.834, blue:0.929, alpha:1)))staticletblue:NSColor=NSColor(Color(#colorLiteral(red:0.623, green:0.826, blue:0.973, alpha:1)))staticletdarkBlue:NSColor=NSColor(Color(#colorLiteral(red:0.343, green:0.559, blue:0.731, alpha:1)))staticletoverBrightGreen:NSColor=NSColor(Color(#colorLiteral(red:0.596, green:0.991, blue:0.988, alpha:1)))staticletdarkGreen:NSColor=NSColor(Color(#colorLiteral(red:0.400, green:0.704, blue:0.386, alpha:1)))staticletgreen:NSColor=NSColor(Color(#colorLiteral(red:0.231, green:0.925, blue:0.349, alpha:1)))staticletnoSignal:NSColor=NSColor(Color(#colorLiteral(red:0.017, green:0.198, blue:1.000, alpha:1)))staticletred:NSColor=NSColor(Color(#colorLiteral(red:0.940, green:0.300, blue:0.280, alpha:1)))staticletmagenta:NSColor=NSColor(Color(#colorLiteral(red:0.780, green:0.360, blue:0.780, alpha:1)))staticletcyan:NSColor=NSColor(Color(#colorLiteral(red:0.420, green:0.840, blue:0.780, alpha:1)))staticletyellow:NSColor=NSColor(Color(#colorLiteral(red:0.980, green:0.880, blue:0.240, alpha:1)))
// Bright colors based on your existing colors
staticletbrightWhite:NSColor=NSColor(Color(#colorLiteral(red:1.000, green:1.000, blue:1.000, alpha:1)))staticletbrightAmber:NSColor=NSColor(Color(#colorLiteral(red:1.000, green:0.922, blue:0.531, alpha:1)))staticletbrightBrightBlue:NSColor=NSColor(Color(#colorLiteral(red:0.792, green:0.937, blue:1.000, alpha:1)))staticletbrightBlue:NSColor=NSColor(Color(#colorLiteral(red:0.792, green:0.923, blue:1.000, alpha:1)))staticletbrightDarkBlue:NSColor=NSColor(Color(#colorLiteral(red:0.517, green:0.735, blue:1.000, alpha:1)))staticletbrightOverBrightGreen:NSColor=NSColor(Color(#colorLiteral(red:0.800, green:1.000, blue:1.000, alpha:1)))staticletbrightDarkGreen:NSColor=NSColor(Color(#colorLiteral(red:0.543, green:0.893, blue:0.756, alpha:1)))staticletbrightGreen:NSColor=NSColor(Color(#colorLiteral(red:0.447, green:1.000, blue:0.752, alpha:1)))staticletbrightNoSignal:NSColor=NSColor(Color(#colorLiteral(red:0.042, green:0.556, blue:1.000, alpha:1)))staticletbrightRed:NSColor=NSColor(Color(#colorLiteral(red:1.000, green:0.440, blue:0.420, alpha:1)))staticletbrightMagenta:NSColor=NSColor(Color(#colorLiteral(red:0.896, green:0.501, blue:0.896, alpha:1)))staticletbrightCyan:NSColor=NSColor(Color(#colorLiteral(red:0.504, green:0.954, blue:0.934, alpha:1)))staticletbrightYellow:NSColor=NSColor(Color(#colorLiteral(red:1.000, green:0.930, blue:0.300, alpha:1)))}}extensionColor{init(_ nsColor:NSColor){self.init(
red:Double(nsColor.redComponent),
green:Double(nsColor.greenComponent),
blue:Double(nsColor.blueComponent),
opacity:Double(nsColor.alphaComponent))}}extensionNSColor{varasColor:Color{letr=Double(redComponent)letg=Double(greenComponent)letb=Double(blueComponent)leta=Double(alphaComponent)returnColor(.sRGB, red: r, green: g, blue: b, opacity: a)}}
Caveat: original code works fine but I've removed various code unneeded for the example.
The text was updated successfully, but these errors were encountered:
I needed to patch SwiftyMarkdown into existing code. Styles needed to be compatible, strongly typed, consistent and readable. Here's my example of usage. Hope it helps someone.
Detail:
init()
takes a list of markdown styles such asbody
and appliesStyleProperty
to them.func applyStyles
copies the custom properties to the styles.enum StyleProperty
provides the custom properties structure.Code:
Caveat: original code works fine but I've removed various code unneeded for the example.
The text was updated successfully, but these errors were encountered: