Skip to content

Commit e0e5a2a

Browse files
committed
Renamed MarkdownAttributes to MarkdownStyles since "attributes" was easily conflatible with the attributes of NSAttributedString
1 parent f638e7c commit e0e5a2a

5 files changed

Lines changed: 53 additions & 53 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ This [is my website ](https://madebywindmill.com).
127127
]
128128
]
129129

130-
let markdownAttributes = MarkdownAttributes(baseAttributes: baseAttrs, styleAttributes: styleAttrs)
131-
let formatter = AttributedStringFormatter(markdown: md, attributes: markdownAttributes)
130+
let markdownStyles = MarkdownStyles(baseAttributes: baseAttrs, styleAttributes: styleAttrs)
131+
let formatter = AttributedStringFormatter(markdown: md, styles: markdownStyles)
132132
let attrStr = formatter.format()
133133
someLabel.attributedText = attrStr
134134

@@ -146,4 +146,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
146146

147147
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
148148

149-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
149+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sources/MarkdownToAttributedString/AttributedStringFormatter.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,40 @@ import Foundation
1010

1111
/// A formatter for converting Markdown strings into `NSAttributedString` objects with customizable styling.
1212
///
13-
/// Provides an interface for rendering Markdown content as rich text, allowing you to apply custom styles Markdown elements like headings, links, and lists. Uses `MarkdownAttributes` for style definitions and `AttributedStringVisitor` for traversing and rendering the Markdown structure.
13+
/// Provides an interface for rendering Markdown content as rich text, allowing you to apply custom styles Markdown elements like headings, links, and lists. Uses `MarkdownStyles` for style definitions and `AttributedStringVisitor` for traversing and rendering the Markdown structure.
1414
///
1515
public class AttributedStringFormatter {
1616

1717
public var options: FormattingOptions
1818

19-
private var attributes: MarkdownAttributes?
19+
private var styles: MarkdownStyles?
2020

21-
/// Initialize the formatter with a Markdown string and optional styling attributes.
21+
/// Initialize the formatter with a Markdown string and optional styling.
2222
///
2323
/// - Parameters:
2424
/// - markdown: The Markdown content to be converted.
25-
/// - attributes: An optional `MarkdownAttributes` object defining styles for the formatted output.
25+
/// - styles: An optional `MarkdownStyles` object defining styles for the formatted output.
2626
public init(
27-
attributes: MarkdownAttributes? = nil,
27+
styles: MarkdownStyles? = nil,
2828
options: FormattingOptions = FormattingOptions.default)
2929
{
30-
self.attributes = attributes
30+
self.styles = styles
3131
self.options = options
3232
}
3333

34-
/// Immediately converts a Markdown string into an `NSAttributedString` with the given styling attributes.
34+
/// Immediately converts a Markdown string into an `NSAttributedString` with the given styling.
3535
///
3636
/// - Parameters:
3737
/// - markdown: The Markdown content to be converted.
38-
/// - attributes: An optional `MarkdownAttributes` object defining styles for the Markdown elements.
38+
/// - styles: An optional `MarkdownStyles` object defining styles for the Markdown elements.
3939
/// - Returns: An `NSAttributedString` representing the formatted Markdown content.
4040
public static func format(
4141
markdown: String,
42-
attributes: MarkdownAttributes? = nil,
42+
styles: MarkdownStyles? = nil,
4343
options: FormattingOptions = FormattingOptions.default) -> NSAttributedString
4444
{
4545
let asf = AttributedStringFormatter(
46-
attributes: attributes,
46+
styles: styles,
4747
options: options)
4848
return asf.format(markdown: markdown)
4949
}
@@ -57,7 +57,7 @@ public class AttributedStringFormatter {
5757
public func format(markdown: String) -> NSAttributedString {
5858
var asv = AttributedStringVisitor(
5959
markdown: markdown,
60-
attributes: attributes,
60+
styles: styles,
6161
options: options)
6262

6363
var result = asv.convert()
@@ -84,7 +84,7 @@ public class AttributedStringFormatter {
8484
result = result.attributedSubstring(from: trimmedRange)
8585
} else {
8686
// The entire string is whitespace
87-
return NSAttributedString(string: "", attributes: attributes?.baseAttributes)
87+
return NSAttributedString(string: "", attributes: styles?.baseAttributes)
8888
}
8989
}
9090

Sources/MarkdownToAttributedString/AttributedStringVisitor.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Markdown
1414

1515
struct AttributedStringVisitor: MarkupVisitor {
1616
var markdown: String
17-
var markdownAttributes: MarkdownAttributes
17+
var markdownStyles: MarkdownStyles
1818

1919
private var attributedString = NSMutableAttributedString()
2020
private var currentAttributes: StringAttrs
@@ -25,11 +25,11 @@ struct AttributedStringVisitor: MarkupVisitor {
2525
private let loggingQ = DispatchQueue(label: "MTAS.logging")
2626

2727
init(markdown: String,
28-
attributes: MarkdownAttributes? = nil,
28+
styles: MarkdownStyles? = nil,
2929
options: FormattingOptions? = nil) {
3030
self.markdown = markdown
31-
self.markdownAttributes = attributes ?? MarkdownAttributes.default
32-
self.currentAttributes = self.markdownAttributes.baseAttributes.deepCopy()
31+
self.markdownStyles = styles ?? MarkdownStyles.default
32+
self.currentAttributes = self.markdownStyles.baseAttributes.deepCopy()
3333
self.formattingOptions = options
3434
}
3535

@@ -104,7 +104,7 @@ struct AttributedStringVisitor: MarkupVisitor {
104104
debugLog("Skipping unsupported: strong"); return
105105
}
106106
debugLog("<open>", file: "")
107-
let newAttributes = markdownAttributes.styleAttributes[.strong] ?? [:]
107+
let newAttributes = markdownStyles.styleAttributes[.strong] ?? [:]
108108
visitWithMergedAttributes(newAttributes, strong, markupType: .strong)
109109
debugLog("<close>", file: "")
110110
}
@@ -115,7 +115,7 @@ struct AttributedStringVisitor: MarkupVisitor {
115115
debugLog("Skipping unsupported: emphasis"); return
116116
}
117117
debugLog("<open>", file: "")
118-
let newAttributes = markdownAttributes.styleAttributes[.emphasis] ?? [:]
118+
let newAttributes = markdownStyles.styleAttributes[.emphasis] ?? [:]
119119
visitWithMergedAttributes(newAttributes, emphasis, markupType: .emphasis)
120120
debugLog("<close>", file: "")
121121
}
@@ -126,7 +126,7 @@ struct AttributedStringVisitor: MarkupVisitor {
126126
debugLog("Skipping unsupported: inlineCode"); return
127127
}
128128
debugLog("<open>", file: "")
129-
var styleAttrs = markdownAttributes.attributesForType(.inlineCode)
129+
var styleAttrs = markdownStyles.attributesForType(.inlineCode)
130130

131131
var currentParent = inlineCode.parent
132132
while let parent = currentParent {
@@ -173,7 +173,7 @@ struct AttributedStringVisitor: MarkupVisitor {
173173
debugLog("<open>", file: "")
174174

175175
let previousAttributes = currentAttributes.deepCopy()
176-
var styleAttrs = markdownAttributes.attributesForType(.codeBlock)
176+
var styleAttrs = markdownStyles.attributesForType(.codeBlock)
177177

178178
if shouldAddCustomAttr {
179179
styleAttrs.addMarkdownElementAttr(
@@ -200,7 +200,7 @@ struct AttributedStringVisitor: MarkupVisitor {
200200
}
201201
debugLog("<open>", file: "")
202202

203-
var styleAttrs = markdownAttributes.attributesForType(.unorderedList)
203+
var styleAttrs = markdownStyles.attributesForType(.unorderedList)
204204
let previousAttributes = currentAttributes.deepCopy()
205205

206206
if shouldAddCustomAttr {
@@ -235,7 +235,7 @@ struct AttributedStringVisitor: MarkupVisitor {
235235
return
236236
}
237237
debugLog("<open>", file: "")
238-
var styleAttrs = markdownAttributes.attributesForType(.orderedList)
238+
var styleAttrs = markdownStyles.attributesForType(.orderedList)
239239
let previousAttributes = currentAttributes.deepCopy()
240240

241241
if shouldAddCustomAttr {
@@ -269,7 +269,7 @@ struct AttributedStringVisitor: MarkupVisitor {
269269
return
270270
}
271271
debugLog("<open>", file: "")
272-
var styleAttrs = markdownAttributes.attributesForType(.listItem)
272+
var styleAttrs = markdownStyles.attributesForType(.listItem)
273273
let previousAttributes = currentAttributes.deepCopy()
274274

275275
currentAttributes.mergeAttributes(styleAttrs)
@@ -345,10 +345,10 @@ struct AttributedStringVisitor: MarkupVisitor {
345345

346346
let level = max(1, min(heading.level, 6))
347347

348-
var styleAttrs = markdownAttributes.attributesForType(.heading)
348+
var styleAttrs = markdownStyles.attributesForType(.heading)
349349

350350
let baseFont: CocoaFont = (styleAttrs[.font] as? CocoaFont) ?? CocoaFont.systemFont(ofSize: 15)
351-
let fontSize = markdownAttributes.headingPointSizes[level - 1]
351+
let fontSize = markdownStyles.headingPointSizes[level - 1]
352352
let headingFont: CocoaFont
353353
#if os(macOS)
354354
headingFont = CocoaFont(descriptor: baseFont.fontDescriptor, size: fontSize) ?? baseFont
@@ -383,7 +383,7 @@ struct AttributedStringVisitor: MarkupVisitor {
383383
}
384384
debugLog("<open>", file: "")
385385

386-
var styleAttrs = markdownAttributes.attributesForType(.link)
386+
var styleAttrs = markdownStyles.attributesForType(.link)
387387

388388
if let urlstr = link.destination {
389389
guard let url = URL(string: urlstr) else {
@@ -414,7 +414,7 @@ struct AttributedStringVisitor: MarkupVisitor {
414414
}
415415
debugLog("<open>", file: "")
416416

417-
var styleAttrs = markdownAttributes.styleAttributes[.strikethrough] ?? markdownAttributes.baseAttributes
417+
var styleAttrs = markdownStyles.styleAttributes[.strikethrough] ?? markdownStyles.baseAttributes
418418
if shouldAddCustomAttr {
419419
styleAttrs.addMarkdownElementAttr(
420420
MarkdownElementAttribute(elementType: .strikethrough)
@@ -458,7 +458,7 @@ struct AttributedStringVisitor: MarkupVisitor {
458458
)
459459
}
460460

461-
if let expectedFont = markdownAttributes.fontAttributeForType(markupType) as? CocoaFont,
461+
if let expectedFont = markdownStyles.fontAttributeForType(markupType) as? CocoaFont,
462462
let currentFont = currentAttributes[.font] as? CocoaFont {
463463
let newDescriptor = mergeFontDescriptors(base: currentFont.fontDescriptor, expected: expectedFont.fontDescriptor)
464464
mergedAttributes[.font] = CocoaFont(descriptor: newDescriptor, size: expectedFont.pointSize)
@@ -515,7 +515,7 @@ struct AttributedStringVisitor: MarkupVisitor {
515515
private func appendPlainText(_ plainText: String) {
516516
attributedString.append(NSAttributedString(
517517
string: plainText,
518-
attributes: markdownAttributes.baseAttributes))
518+
attributes: markdownStyles.baseAttributes))
519519
}
520520

521521
private func debugLog(_ message: String, file: String = #file, line: Int = #line, function: String = #function) {

Sources/MarkdownToAttributedString/MarkdownAttributes.swift renamed to Sources/MarkdownToAttributedString/MarkdownStyles.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// MarkdownAttributes.swift
2+
// MarkdownStyles.swift
33
// MarkdownToAttributedString
44
//
55
// Created by John Scalo on 1/18/25.
@@ -12,7 +12,7 @@ import UIKit
1212
#endif
1313
import Markdown
1414

15-
/// Encapsulates styling attributes for rendering Markdown elements with `AttributedStringFormatter`.
15+
/// Encapsulates styling for rendering Markdown elements with `AttributedStringFormatter`.
1616
///
1717
/// - Parameters:
1818
/// - baseAttributes: A dictionary of default text attributes (`StringAttrs`) applied to all Markdown content
@@ -24,7 +24,7 @@ import Markdown
2424
/// - headingPointSizes: An array of `CGFloat` values specifying font sizes for headings. The first value applies
2525
/// to level 1 headings (`#`), the second to level 2 headings (`##`), and so on. If there are fewer values than
2626
/// heading levels, the last size in the array is reused for remaining levels.
27-
public struct MarkdownAttributes {
27+
public struct MarkdownStyles {
2828
public var baseAttributes: StringAttrs
2929
public var styleAttributes: [MarkupType: StringAttrs]
3030
public var headingPointSizes: [CGFloat] = [22, 18, 15, 14, 13, 11]
@@ -58,14 +58,14 @@ public struct MarkdownAttributes {
5858
}
5959
}
6060

61-
/// A default set of MarkdownAttributes, intended mainly for tests, debugging, and demonstration.
62-
public extension MarkdownAttributes {
63-
static var `default`: MarkdownAttributes {
61+
/// A default set of MarkdownStyles, intended mainly for tests, debugging, and demonstration.
62+
public extension MarkdownStyles {
63+
static var `default`: MarkdownStyles {
6464
let indentedPStyle = NSMutableParagraphStyle()
6565
indentedPStyle.firstLineHeadIndent = 20
6666
indentedPStyle.headIndent = 20
6767

68-
return MarkdownAttributes(
68+
return MarkdownStyles(
6969
baseAttributes: [
7070
.font: CocoaFont.systemFont(ofSize: 13),
7171
.foregroundColor: CocoaColor.darkGray

Tests/MarkdownToAttributedStringTests/AttrStringFormattingTests.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import Markdown
1111

1212
final class MarkdownToAttributedStringTests: XCTestCase {
1313

14-
static let defaultMDAttrs = MarkdownAttributes.default
14+
static let defaultMDStyles = MarkdownStyles.default
1515
static let options = FormattingOptions(addCustomMarkdownElementAttributes: true, debugLogging: true)
1616
static let trimWhitespaceOptions = FormattingOptions(addCustomMarkdownElementAttributes: true, debugLogging: true, trimWhitespace: true)
1717

1818
var defaultFormatter: AttributedStringFormatter!
1919
var trimWhitespaceFormatter: AttributedStringFormatter!
2020

2121
override func setUp() {
22-
defaultFormatter = AttributedStringFormatter(attributes: Self.defaultMDAttrs, options: Self.options)
23-
trimWhitespaceFormatter = AttributedStringFormatter(attributes: Self.defaultMDAttrs, options: Self.trimWhitespaceOptions)
22+
defaultFormatter = AttributedStringFormatter(styles: Self.defaultMDStyles, options: Self.options)
23+
trimWhitespaceFormatter = AttributedStringFormatter(styles: Self.defaultMDStyles, options: Self.trimWhitespaceOptions)
2424
}
2525

2626
func testBoldText() {
@@ -31,7 +31,7 @@ final class MarkdownToAttributedStringTests: XCTestCase {
3131
var styledRange = (attrStr.string as NSString).range(of: "bold")
3232
var attributes = attrStr.attributes(at: styledRange.location, effectiveRange: nil)
3333
var font = attributes[.font] as! CocoaFont
34-
var expectedFont = Self.defaultMDAttrs.fontAttributeForType(.strong)
34+
var expectedFont = Self.defaultMDStyles.fontAttributeForType(.strong)
3535

3636
XCTAssertTrue(font.customIsEqual(to: expectedFont))
3737

@@ -42,7 +42,7 @@ final class MarkdownToAttributedStringTests: XCTestCase {
4242
styledRange = (attrStr.string as NSString).range(of: "bold 💯")
4343
attributes = attrStr.attributes(at: styledRange.location, effectiveRange: nil)
4444
font = attributes[.font] as! CocoaFont
45-
expectedFont = Self.defaultMDAttrs.fontAttributeForType(.strong)
45+
expectedFont = Self.defaultMDStyles.fontAttributeForType(.strong)
4646

4747
XCTAssertTrue(font.customIsEqual(to: expectedFont))
4848
}
@@ -55,7 +55,7 @@ final class MarkdownToAttributedStringTests: XCTestCase {
5555
let styledRange = (attrStr.string as NSString).range(of: "italic")
5656
let attributes = attrStr.attributes(at: styledRange.location, effectiveRange: nil)
5757
let font = attributes[.font] as! CocoaFont
58-
let expectedFont = Self.defaultMDAttrs.fontAttributeForType(.emphasis)
58+
let expectedFont = Self.defaultMDStyles.fontAttributeForType(.emphasis)
5959

6060
XCTAssertTrue(font.customIsEqual(to: expectedFont))
6161
}
@@ -99,7 +99,7 @@ final class MarkdownToAttributedStringTests: XCTestCase {
9999

100100
let styledRange = (attrStr.string as NSString).range(of: "inline code")
101101
let attributes = attrStr.attributes(at: styledRange.location, effectiveRange: nil)
102-
XCTAssertEqual(attributes[.font] as? CocoaFont, Self.defaultMDAttrs.fontAttributeForType(.inlineCode))
102+
XCTAssertEqual(attributes[.font] as? CocoaFont, Self.defaultMDStyles.fontAttributeForType(.inlineCode))
103103
}
104104

105105
func testNestedInlineCode() {
@@ -155,10 +155,10 @@ final class MarkdownToAttributedStringTests: XCTestCase {
155155
let attributes = attrStr.attributes(at: styleRange.location, effectiveRange: nil)
156156
XCTAssertEqual(attributes[.strikethroughStyle] as? Int, 1)
157157

158-
guard let expectedStrikeColor: CocoaColor = Self.defaultMDAttrs.valueForAttribute(.strikethroughColor, type: .strikethrough) else {
158+
guard let expectedStrikeColor: CocoaColor = Self.defaultMDStyles.valueForAttribute(.strikethroughColor, type: .strikethrough) else {
159159
XCTFail(); return
160160
}
161-
guard let expectedFontColor: CocoaColor = Self.defaultMDAttrs.valueForAttribute(.foregroundColor, type: .strikethrough) else {
161+
guard let expectedFontColor: CocoaColor = Self.defaultMDStyles.valueForAttribute(.foregroundColor, type: .strikethrough) else {
162162
XCTFail(); return
163163
}
164164

@@ -274,15 +274,15 @@ final class MarkdownToAttributedStringTests: XCTestCase {
274274
// Validate attributes for each type
275275
let boldRange = (attrStr.string as NSString).range(of: "bold")
276276
let boldAttributes = attrStr.attributes(at: boldRange.location, effectiveRange: nil)
277-
XCTAssertEqual(boldAttributes[.font] as? CocoaFont, Self.defaultMDAttrs.fontAttributeForType(.strong))
277+
XCTAssertEqual(boldAttributes[.font] as? CocoaFont, Self.defaultMDStyles.fontAttributeForType(.strong))
278278

279279
let italicRange = (attrStr.string as NSString).range(of: "italic")
280280
let italicAttributes = attrStr.attributes(at: italicRange.location, effectiveRange: nil)
281-
XCTAssertEqual(italicAttributes[.font] as? CocoaFont, Self.defaultMDAttrs.fontAttributeForType(.emphasis))
281+
XCTAssertEqual(italicAttributes[.font] as? CocoaFont, Self.defaultMDStyles.fontAttributeForType(.emphasis))
282282

283283
let codeRange = (attrStr.string as NSString).range(of: "inline code")
284284
let codeAttributes = attrStr.attributes(at: codeRange.location, effectiveRange: nil)
285-
XCTAssertEqual(codeAttributes[.font] as? CocoaFont, Self.defaultMDAttrs.fontAttributeForType(.inlineCode))
285+
XCTAssertEqual(codeAttributes[.font] as? CocoaFont, Self.defaultMDStyles.fontAttributeForType(.inlineCode))
286286
}
287287

288288
func testHeadings() {
@@ -300,7 +300,7 @@ final class MarkdownToAttributedStringTests: XCTestCase {
300300
XCTAssertTrue(attrStr.string.contains("Heading \(i)"), "Heading \(i) not found in output string.")
301301
}
302302

303-
let headingFont = Self.defaultMDAttrs.fontAttributeForType(.heading)
303+
let headingFont = Self.defaultMDStyles.fontAttributeForType(.heading)
304304

305305
for i in 1...6 {
306306
let headingText = "Heading \(i)"
@@ -309,7 +309,7 @@ final class MarkdownToAttributedStringTests: XCTestCase {
309309

310310
let actualFont = attributes[.font] as! CocoaFont
311311
XCTAssertEqual(actualFont.fontDescriptor.postscriptName, headingFont.fontDescriptor.postscriptName)
312-
XCTAssertEqual(actualFont.pointSize, Self.defaultMDAttrs.headingPointSizes[i-1], "Font point size for \(headingText) doesn't match.")
312+
XCTAssertEqual(actualFont.pointSize, Self.defaultMDStyles.headingPointSizes[i-1], "Font point size for \(headingText) doesn't match.")
313313
}
314314
}
315315

0 commit comments

Comments
 (0)