diff --git a/MarkdownKit/Sources/Common/Elements/List/MarkdownList.swift b/MarkdownKit/Sources/Common/Elements/List/MarkdownList.swift index 96289c6..70248ba 100644 --- a/MarkdownKit/Sources/Common/Elements/List/MarkdownList.swift +++ b/MarkdownKit/Sources/Common/Elements/List/MarkdownList.swift @@ -9,7 +9,7 @@ import Foundation open class MarkdownList: MarkdownLevelElement { - fileprivate static let regex = "^([\\*\\+\\-]{1,%@})\\s+(.+)$" + fileprivate static let regex = "^( {0,%@}[\\*\\+\\-])\\s+(.+)$" open var maxLevel: Int open var font: MarkdownFont? @@ -22,7 +22,7 @@ open class MarkdownList: MarkdownLevelElement { return String(format: MarkdownList.regex, level) } - public init(font: MarkdownFont? = nil, maxLevel: Int = 0, indicator: String = "•", + public init(font: MarkdownFont? = nil, maxLevel: Int = 6, indicator: String = "●", separator: String = " ", color: MarkdownColor? = nil) { self.maxLevel = maxLevel self.indicator = indicator @@ -32,10 +32,20 @@ open class MarkdownList: MarkdownLevelElement { } open func formatText(_ attributedString: NSMutableAttributedString, range: NSRange, level: Int) { - var string = (0.. String in - return "\(string)\(separator)" - } - string = "\(string)\(indicator) " - attributedString.replaceCharacters(in: range, with: string) + let levelIndicatorList = [1: "\(indicator) ", 2: "\(indicator) ", 3: "◦ ", 4: "◦ ", 5: "▪︎ ", 6: "▪︎ "] + let levelIndicatorOffsetList = [1: "", 2: "", 3: " ", 4: " ", 5: " ", 6: " "] + guard let indicatorIcon = levelIndicatorList[level], + let offset = levelIndicatorOffsetList[level] else { return } + let indicator = "\(offset)\(indicatorIcon)" + attributedString.replaceCharacters(in: range, with: indicator) + attributedString.addAttributes([.paragraphStyle : defaultParagraphStyle()], range: range) + } + + private func defaultParagraphStyle() -> NSMutableParagraphStyle { + let paragraphStyle = NSMutableParagraphStyle() + paragraphStyle.firstLineHeadIndent = 0 + paragraphStyle.headIndent = 16 + paragraphStyle.paragraphSpacing = 4 + return paragraphStyle } }