From 761a30e8d5db9384d7bbaa9a5b83e6a7e8fd5443 Mon Sep 17 00:00:00 2001 From: gaebel Date: Fri, 20 Aug 2021 16:56:21 +0200 Subject: [PATCH] Fix mixing elements and inheriting attributes, fixes #97 --- .../Common/Elements/Bold/MarkdownBold.swift | 22 +++++++---------- .../Elements/Italic/MarkdownItalic.swift | 24 +++++++------------ .../Strikethrough/MarkdownStrikethrough.swift | 12 ++++------ .../Sources/Common/MarkdownParser.swift | 2 +- MarkdownKit/Tests/MarkdownParserTests.swift | 23 ++++++++++++++++++ 5 files changed, 45 insertions(+), 38 deletions(-) diff --git a/MarkdownKit/Sources/Common/Elements/Bold/MarkdownBold.swift b/MarkdownKit/Sources/Common/Elements/Bold/MarkdownBold.swift index bd42d86..2d5c4fc 100644 --- a/MarkdownKit/Sources/Common/Elements/Bold/MarkdownBold.swift +++ b/MarkdownKit/Sources/Common/Elements/Bold/MarkdownBold.swift @@ -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)) diff --git a/MarkdownKit/Sources/Common/Elements/Italic/MarkdownItalic.swift b/MarkdownKit/Sources/Common/Elements/Italic/MarkdownItalic.swift index 708cfdd..b8cc71d 100644 --- a/MarkdownKit/Sources/Common/Elements/Italic/MarkdownItalic.swift +++ b/MarkdownKit/Sources/Common/Elements/Italic/MarkdownItalic.swift @@ -9,7 +9,7 @@ import Foundation open class MarkdownItalic: MarkdownCommonElement { - fileprivate static let regex = "(\\s|^)(\\*|_)(?![\\*_\\s])(.+?)(?