@@ -37,21 +37,12 @@ open class AttributedStringGenerator {
3737 /// Customized html generator to work around limitations of the current HTML to
3838 /// `NSAttributedString` conversion logic provided by the operating system.
3939 open class InternalHtmlGenerator : HtmlGenerator {
40- weak var outer : AttributedStringGenerator ?
40+ var outer : AttributedStringGenerator ?
4141
4242 public init ( outer: AttributedStringGenerator ) {
4343 self . outer = outer
4444 }
4545
46- open override func generate( textFragment fragment: TextFragment ) -> String {
47- switch fragment {
48- case . custom( let customTextFragment) :
49- return customTextFragment. generateHtml ( via: self , and: self . outer)
50- default :
51- return super. generate ( textFragment: fragment)
52- }
53- }
54-
5546 open override func generate( block: Block , tight: Bool = false ) -> String {
5647 switch block {
5748 case . list( _, _, _) :
@@ -121,6 +112,33 @@ open class AttributedStringGenerator {
121112 return super. generate ( block: block, tight: tight)
122113 }
123114 }
115+
116+ open override func generate( textFragment fragment: TextFragment ) -> String {
117+ switch fragment {
118+ case . image( let text, let uri, let title) :
119+ let titleAttr = title == nil ? " " : " title= \" \( title!) \" "
120+ if let uriStr = uri {
121+ let url = URL ( string: uriStr)
122+ Swift . print ( " === URL0 = \( url? . absoluteString ?? " none " ) , \( url? . scheme == nil ) , \( self . outer == nil ) " )
123+ if ( url? . scheme == nil ) || ( url? . isFileURL ?? false ) ,
124+ let baseUrl = self . outer? . imageBaseUrl {
125+ let url = URL ( fileURLWithPath: uriStr, relativeTo: baseUrl)
126+ Swift . print ( " URL2 = \( url. absoluteString) " )
127+ if url. isFileURL {
128+ return " <img src= \" \( url. absoluteURL. path) \" " +
129+ " alt= \" \( text. rawDescription) \" \( titleAttr) /> "
130+ }
131+ }
132+ return " <img src= \" \( uriStr) \" alt= \" \( text. rawDescription) \" \( titleAttr) /> "
133+ } else {
134+ return self . generate ( text: text)
135+ }
136+ case . custom( let customTextFragment) :
137+ return customTextFragment. generateHtml ( via: self , and: self . outer)
138+ default :
139+ return super. generate ( textFragment: fragment)
140+ }
141+ }
124142 }
125143
126144 /// Default `AttributedStringGenerator` implementation.
@@ -171,6 +189,19 @@ open class AttributedStringGenerator {
171189 /// The color of H4 headers.
172190 public let h4Color : String
173191
192+ /// The maximum width of an image
193+ public let maxImageWidth : String ?
194+
195+ /// The maximum height of an image
196+ public let maxImageHeight : String ?
197+
198+ /// Custom CSS style
199+ public let customStyle : String
200+
201+ /// If provided, this URL is used as a base URL for relative image links
202+ public let imageBaseUrl : URL ?
203+
204+
174205 /// Constructor providing customization options for the generated `NSAttributedString` markup.
175206 public init ( fontSize: Float = 14.0 ,
176207 fontFamily: String = " \" Times New Roman \" ,Times,serif " ,
@@ -187,7 +218,11 @@ open class AttributedStringGenerator {
187218 h1Color: String = mdDefaultColor,
188219 h2Color: String = mdDefaultColor,
189220 h3Color: String = mdDefaultColor,
190- h4Color: String = mdDefaultColor) {
221+ h4Color: String = mdDefaultColor,
222+ maxImageWidth: String ? = nil ,
223+ maxImageHeight: String ? = nil ,
224+ customStyle: String = " " ,
225+ imageBaseUrl: URL ? = nil ) {
191226 self . fontSize = fontSize
192227 self . fontFamily = fontFamily
193228 self . fontColor = fontColor
@@ -203,6 +238,10 @@ open class AttributedStringGenerator {
203238 self . h2Color = h2Color
204239 self . h3Color = h3Color
205240 self . h4Color = h4Color
241+ self . maxImageWidth = maxImageWidth
242+ self . maxImageHeight = maxImageHeight
243+ self . customStyle = customStyle
244+ self . imageBaseUrl = imageBaseUrl
206245 }
207246
208247 /// Generates an attributed string from the given Markdown document
@@ -266,14 +305,16 @@ open class AttributedStringGenerator {
266305 " td.codebox { \( self . codeBoxStyle) } \n " +
267306 " td.thematic { \( self . thematicBreakStyle) } \n " +
268307 " td.quote { \( self . quoteStyle) } \n " +
308+ " img { \( self . imgStyle) } \n " +
269309 " dt { \n " +
270310 " font-weight: bold; \n " +
271311 " margin: 0.6em 0 0.4em 0; \n " +
272312 " } \n " +
273313 " dd { \n " +
274314 " margin: 0.5em 0 1em 2em; \n " +
275315 " padding: 0.5em 0 1em 2em; \n " +
276- " } \n "
316+ " } \n " +
317+ " \( self . customStyle) \n "
277318 }
278319
279320 open var bodyStyle : String {
@@ -361,6 +402,23 @@ open class AttributedStringGenerator {
361402 " width: 0.4em; "
362403 }
363404
405+ open var imgStyle : String {
406+ if let maxWidth = self . maxImageWidth {
407+ if let maxHeight = self . maxImageHeight {
408+ return " max-width: \( maxWidth) ;max-height: \( maxHeight) ; " +
409+ " !important;width: auto;height: auto; "
410+ } else {
411+ return " max-width: \( maxWidth) ;max-height: 100%; " +
412+ " !important;width: auto;height: auto; "
413+ }
414+ } else if let maxHeight = self . maxImageHeight {
415+ return " max-width: 100%;max-height: \( maxHeight) ; " +
416+ " !important;width: auto;height: auto; "
417+ } else {
418+ return " "
419+ }
420+ }
421+
364422 open var tableStyle : String {
365423 return " border-collapse: collapse; " +
366424 " margin: 0.3em 0; " +
0 commit comments