|
8 | 8 |
|
9 | 9 | import Foundation |
10 | 10 |
|
11 | | -/// Returns formatted Markdown text as an attributed string. |
| 11 | +/// Parses Markdown text and eturns formatted text as an attributed string with custom markup attributes applied. |
| 12 | +/// If the parsing failed, specified Markdown tags are ignored. Yet, the rest of the style information is still applied. |
12 | 13 | /// |
13 | 14 | /// - Parameters: |
14 | | -/// - markdownText: String with Markdown tags. |
| 15 | +/// - text: Text. |
15 | 16 | /// - textStyle: Text style object containing style information. |
16 | | -/// - Returns: Formatted Markdown text. |
| 17 | +/// - markups: Markup information. |
| 18 | +/// - Returns: Formatted text. |
17 | 19 | public func attributedMarkdownString(from markdownText: String, |
18 | 20 | using textStyle: TextStyle) -> NSAttributedString { |
19 | | - guard let (parsedString, elements) = try? MarkdownParser.parse(markdownText) else { |
| 21 | + do { |
| 22 | + return try parsedMarkdownString(from: markdownText, using: textStyle) |
| 23 | + } catch { |
20 | 24 | return NSAttributedString(string: markdownText, textStyle: textStyle) |
21 | 25 | } |
| 26 | +} |
| 27 | + |
| 28 | +/// Parses custom mark up information and returns formatted text as an attributed string with custom markup attributes applied. |
| 29 | +/// If the parsing failed, custom markup attributes are ignored. Yet, style information from `textStyle` parameter is still applied. |
| 30 | +/// |
| 31 | +/// - Parameters: |
| 32 | +/// - text: Text. |
| 33 | +/// - textStyle: Text style object containing style information. |
| 34 | +/// - markups: Markup information. |
| 35 | +/// - Returns: Formatted text. |
| 36 | +public func attributedMarkupString(from text: String, |
| 37 | + using textStyle: TextStyle, |
| 38 | + customMarkup markups: Markup) -> NSAttributedString { |
| 39 | + do { |
| 40 | + return try parsedMarkupString(from: text, using: textStyle, customMarkup: markups) |
| 41 | + } catch { |
| 42 | + return NSAttributedString(string: text, textStyle: textStyle) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Returns formatted Markdown text as an attributed string. |
| 47 | +/// |
| 48 | +/// - Parameters: |
| 49 | +/// - markdownText: String with Markdown tags. |
| 50 | +/// - textStyle: Text style object containing style information. |
| 51 | +/// - Returns: Formatted Markdown text. |
| 52 | +/// - Throws: Parser error. |
| 53 | +public func parsedMarkdownString(from markdownText: String, |
| 54 | + using textStyle: TextStyle) throws -> NSAttributedString { |
| 55 | + let (parsedString, elements) = try MarkdownParser.parse(markdownText) |
22 | 56 |
|
23 | 57 | let attributedString = NSMutableAttributedString(string: textStyle.textTransform.applied(to: parsedString)) |
24 | 58 | attributedString.addAttributes(textStyle.attributes, |
@@ -62,14 +96,15 @@ public func attributedMarkdownString(from markdownText: String, |
62 | 96 | /// - textStyle: Text style object containing style information. |
63 | 97 | /// - markups: Markup information. |
64 | 98 | /// - Returns: Formatted text. |
65 | | -public func attributedMarkupString(from text: String, |
66 | | - using textStyle: TextStyle, |
67 | | - customMarkup markups: Markup) -> NSAttributedString { |
68 | | - guard |
69 | | - markups.count > 0, |
70 | | - let (parsedString, elements) = try? ElementParser.parse(text, for: markups.map { Symbol(character: $0.0) }) else { |
71 | | - return NSAttributedString(string: text, textStyle: textStyle) |
| 99 | +/// - Throws: Parser error. |
| 100 | +public func parsedMarkupString(from text: String, |
| 101 | + using textStyle: TextStyle, |
| 102 | + customMarkup markups: Markup) throws -> NSAttributedString { |
| 103 | + guard markups.count > 0 else { |
| 104 | + return NSAttributedString(string: text, textStyle: textStyle) |
72 | 105 | } |
| 106 | + |
| 107 | + let (parsedString, elements) = try ElementParser.parse(text, for: markups.map { Symbol(character: $0.0) }) |
73 | 108 |
|
74 | 109 | let attributedString = NSMutableAttributedString(string: textStyle.textTransform.applied(to: parsedString)) |
75 | 110 | attributedString.addAttributes(textStyle.attributes, |
|
0 commit comments