Skip to content

Commit

Permalink
Fix mixing elements and inheriting attributes, fixes #97
Browse files Browse the repository at this point in the history
  • Loading branch information
gaebel committed Aug 20, 2021
1 parent 072e192 commit 761a30e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
22 changes: 8 additions & 14 deletions MarkdownKit/Sources/Common/Elements/Bold/MarkdownBold.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,14 @@ open class MarkdownBold: MarkdownCommonElement {
public func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
attributedString.deleteCharacters(in: match.range(at: 4))

let currentAttributes = attributedString.attributes(
at: match.range(at: 3).location,
longestEffectiveRange: nil,
in: match.range(at: 3)
)

addAttributes(attributedString, range: match.range(at: 3))

if let font = currentAttributes[.font] as? MarkdownFont {
attributedString.addAttribute(
NSAttributedString.Key.font,
value: font.bold(),
range: match.range(at: 3)
)
attributedString.enumerateAttribute(.font, in: match.range(at: 3)) { value, range, stop in
if let font = value as? MarkdownFont {
attributedString.addAttribute(
NSAttributedString.Key.font,
value: font.bold(),
range: range
)
}
}

attributedString.deleteCharacters(in: match.range(at: 2))
Expand Down
24 changes: 9 additions & 15 deletions MarkdownKit/Sources/Common/Elements/Italic/MarkdownItalic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

open class MarkdownItalic: MarkdownCommonElement {

fileprivate static let regex = "(\\s|^)(\\*|_)(?![\\*_\\s])(.+?)(?<![\\*_\\s])(\\2)"
fileprivate static let regex = "(.?|^)(\\*|_)(?=\\S)(.+?)(?<![\\*_\\s])(\\2)"

open var font: MarkdownFont?
open var color: MarkdownColor?
Expand All @@ -26,20 +26,14 @@ open class MarkdownItalic: MarkdownCommonElement {
public func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
attributedString.deleteCharacters(in: match.range(at: 4))

let currentAttributes = attributedString.attributes(
at: match.range(at: 3).location,
longestEffectiveRange: nil,
in: match.range(at: 3)
)

addAttributes(attributedString, range: match.range(at: 3))

if let font = currentAttributes[.font] as? MarkdownFont {
attributedString.addAttribute(
NSAttributedString.Key.font,
value: font.italic(),
range: match.range(at: 3)
)
attributedString.enumerateAttribute(.font, in: match.range(at: 3)) { value, range, stop in
if let font = value as? MarkdownFont {
attributedString.addAttribute(
NSAttributedString.Key.font,
value: font.italic(),
range: range
)
}
}

attributedString.deleteCharacters(in: match.range(at: 2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@ open class MarkdownStrikethrough: MarkdownCommonElement {
public func match(_ match: NSTextCheckingResult, attributedString: NSMutableAttributedString) {
attributedString.deleteCharacters(in: match.range(at: 4))

var attributes = attributedString.attributes(
at: match.range(at: 3).location,
longestEffectiveRange: nil,
in: match.range(at: 3)
attributedString.addAttribute(
NSAttributedString.Key.strikethroughStyle,
value: NSNumber.init(value: NSUnderlineStyle.single.rawValue),
range: match.range(at: 3)
)

attributes[NSAttributedString.Key.strikethroughStyle] = NSNumber.init(value: NSUnderlineStyle.single.rawValue)

attributedString.addAttributes(attributes, range: match.range(at: 3))

attributedString.deleteCharacters(in: match.range(at: 2))
}
}
2 changes: 1 addition & 1 deletion MarkdownKit/Sources/Common/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ open class MarkdownParser {
(.header, header),
(.list, list),
(.quote, quote),
(.strikethrough, strikethrough),
(.bold, bold),
(.italic, italic),
(.strikethrough, strikethrough),
(.link, link),
(.automaticLink, automaticLink),
(.code, code),
Expand Down
23 changes: 23 additions & 0 deletions MarkdownKit/Tests/MarkdownParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ class Tests: XCTestCase {
sut = MarkdownParser()
}

func testShouldNotParseWithWhitespace() {
let variations = [
"__ Bold __",
"_ Italic _",
"** Bold **",
"X * Italic *",
"~~ Strikethrough ~~"
]

variations.forEach {
let attributedString = sut.parse($0)
let attributes = attributedString.attributes(at: 0, effectiveRange: nil)
guard let font = attributes[NSAttributedString.Key.font] as? MarkdownFont else {
XCTFail("Font attributes missing")
return
}

XCTAssertFalse(font.contains(attribute: .bold))
XCTAssertFalse(font.contains(attribute: .italic))
XCTAssertNil(attributes[NSAttributedString.Key.strikethroughStyle])
}
}

func testParseBoldItalicStrikethrough() throws {
let combinations = [
"___~~Bold-Italic-Strikethrough~~___",
Expand Down

0 comments on commit 761a30e

Please sign in to comment.