@@ -19,14 +19,15 @@ public protocol MarkdownFormatter {
1919
2020/// Default implementation for the Markdown formatter
2121open class DefaultMarkdownFormatter : MarkdownFormatter {
22- enum Attributes {
23- enum Code {
24- static let fontName : String = " CourierNewPSMT "
25- }
26- }
22+ public var styles : MarkdownStyles
23+ public var markdownRegexPattern : String
2724
28- private let markdownRegexPattern : String =
29- " ((?: \\ `(.*?) \\ `)|(?: \\ *{1,2}(.*?) \\ *{1,2})|(?: \\ ~{2}(.*?) \\ ~{2})|(?: \\ _{1,2}(.*?) \\ _{1,2})|^(>){1}|(#){1,6}|(=){3,10}|(-){1,3}|( \\ d{1,3} \\ .)|(?: \\ [(.*?) \\ ])(?: \\ ((.*?) \\ ))|(?: \\ [(.*?) \\ ])(?: \\ [(.*?) \\ ])|( \\ ] \\ :))+ "
25+ private let defaultMarkdownRegex = " ((?: \\ `(.*?) \\ `)|(?: \\ *{1,2}(.*?) \\ *{1,2})|(?: \\ ~{2}(.*?) \\ ~{2})|(?: \\ _{1,2}(.*?) \\ _{1,2})|^(>){1}|(#){1,6}|(=){3,10}|(-){1,3}|( \\ d{1,3} \\ .)|(?: \\ [(.*?) \\ ])(?: \\ ((.*?) \\ ))|(?: \\ [(.*?) \\ ])(?: \\ [(.*?) \\ ])|( \\ ] \\ :))+ "
26+
27+ public init ( ) {
28+ styles = MarkdownStyles ( )
29+ markdownRegexPattern = defaultMarkdownRegex
30+ }
3031
3132 private lazy var regex : NSRegularExpression ? = {
3233 guard let regex = try ? NSRegularExpression ( pattern: markdownRegexPattern, options: . anchorsMatchLines) else {
@@ -43,7 +44,98 @@ open class DefaultMarkdownFormatter: MarkdownFormatter {
4344
4445 open func format( _ string: String ) -> NSAttributedString {
4546 let markdownFormatter = SwiftyMarkdown ( string: string)
46- markdownFormatter. code. fontName = Attributes . Code. fontName
47+ modify ( swiftyMarkdownFont: markdownFormatter. code, with: styles. codeFont)
48+ modify ( swiftyMarkdownFont: markdownFormatter. body, with: styles. bodyFont)
49+ modify ( swiftyMarkdownFont: markdownFormatter. link, with: styles. linkFont)
50+ modify ( swiftyMarkdownFont: markdownFormatter. h1, with: styles. h1Font)
51+ modify ( swiftyMarkdownFont: markdownFormatter. h2, with: styles. h2Font)
52+ modify ( swiftyMarkdownFont: markdownFormatter. h3, with: styles. h3Font)
53+ modify ( swiftyMarkdownFont: markdownFormatter. h4, with: styles. h4Font)
54+ modify ( swiftyMarkdownFont: markdownFormatter. h5, with: styles. h5Font)
55+ modify ( swiftyMarkdownFont: markdownFormatter. h6, with: styles. h6Font)
4756 return markdownFormatter. attributedString ( )
4857 }
58+
59+ private func modify( swiftyMarkdownFont: FontProperties , with font: MarkdownFont ) {
60+ if let fontName = font. name {
61+ swiftyMarkdownFont. fontName = fontName
62+ }
63+ if let fontSize = font. size {
64+ swiftyMarkdownFont. fontSize = fontSize
65+ }
66+ if let fontColor = font. color {
67+ swiftyMarkdownFont. color = fontColor
68+ }
69+ if let fontStyle = font. styling? . asSwiftyMarkdownFontStyle ( ) {
70+ swiftyMarkdownFont. fontStyle = fontStyle
71+ }
72+ }
73+ }
74+
75+ /// Configures the font style properties for base Markdown elements
76+ public struct MarkdownStyles {
77+ /// The regular paragraph font.
78+ public var bodyFont : MarkdownFont = . init( )
79+
80+ /// The font used for coding blocks in markdown text.
81+ public var codeFont : MarkdownFont = . init( )
82+
83+ /// The font used for links found in markdown text.
84+ public var linkFont : MarkdownFont = . init( )
85+
86+ /// The font used for H1 headers in markdown text.
87+ public var h1Font : MarkdownFont = . init( )
88+
89+ /// The font used for H2 headers in markdown text.
90+ public var h2Font : MarkdownFont = . init( )
91+
92+ /// The font used for H3 headers in markdown text.
93+ public var h3Font : MarkdownFont = . init( )
94+
95+ /// The font used for H4 headers in markdown text.
96+ public var h4Font : MarkdownFont = . init( )
97+
98+ /// The font used for H5 headers in markdown text.
99+ public var h5Font : MarkdownFont = . init( )
100+
101+ /// The font used for H6 headers in markdown text.
102+ public var h6Font : MarkdownFont = . init( )
103+
104+ public init ( ) {
105+ codeFont. name = " CourierNewPSMT "
106+ }
107+ }
108+
109+ public struct MarkdownFont {
110+ public var name : String ?
111+ public var size : Double ?
112+ public var color : UIColor ?
113+ public var styling : MarkdownFontStyle ?
114+
115+ public init ( ) {
116+ name = nil
117+ size = nil
118+ color = nil
119+ styling = nil
120+ }
121+ }
122+
123+ public enum MarkdownFontStyle : Int {
124+ case normal
125+ case bold
126+ case italic
127+ case boldItalic
128+
129+ func asSwiftyMarkdownFontStyle( ) -> FontStyle {
130+ switch self {
131+ case . normal:
132+ return . normal
133+ case . bold:
134+ return . bold
135+ case . italic:
136+ return . italic
137+ case . boldItalic:
138+ return . boldItalic
139+ }
140+ }
49141}
0 commit comments