Skip to content

Commit

Permalink
Merge pull request #54 from cpvbruno/bugfix/multiLevelList
Browse files Browse the repository at this point in the history
Bugfix - Multi level list
  • Loading branch information
bmoliveira authored Jan 26, 2020
2 parents 023cb85 + da0ec3f commit 7a5bb9c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions MarkdownKit/Sources/Common/Elements/List/MarkdownList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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
Expand All @@ -32,10 +32,20 @@ open class MarkdownList: MarkdownLevelElement {
}

open func formatText(_ attributedString: NSMutableAttributedString, range: NSRange, level: Int) {
var string = (0..<level).reduce("") { (string, _) -> 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
}
}

0 comments on commit 7a5bb9c

Please sign in to comment.