Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CDMarkdownKitTests/TestQuotes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,22 @@ final class TestQuotes: XCTestCase {
XCTAssertEqual(TestHelpers.getQuoteLevel(testString: parsed, at: 14), 1)
}

func testQuoteWithIndicator() throws {
let parser = CDMarkdownParser()

let parsed = parser.parse("> This <-> That")
XCTAssertEqual(parsed.string, "This <-> That")
XCTAssertGreaterThan(TestHelpers.getHeadIndent(testString: parsed, at: 0), 0)
XCTAssertEqual(TestHelpers.getQuoteLevel(testString: parsed, at: 0), 0)

let paragraphStyleStart = parsed.attribute(.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle
let paragraphStyleEnd = parsed.attribute(.paragraphStyle, at: 12, effectiveRange: nil) as? NSParagraphStyle

XCTAssertNotNil(paragraphStyleStart)

// Ensure the paragraphStyle is for the whole line and not split at ">"
XCTAssertEqual(paragraphStyleStart, paragraphStyleEnd)
}


}
18 changes: 15 additions & 3 deletions Source/CDMarkdownLayoutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,35 @@ open class CDMarkdownLayoutManager: NSLayoutManager {
let textRectFirstLine = self.boundingRect(forGlyphRange: glyphRangeFirstLine, in: textContainer)

// Create a rect that would later become our quote indicator (the bar on the left side of the quote)
let newRect = CGRect(x: textRectFirstLine.origin.x - 9, y: textRect.origin.y + 2, width: 4, height: textRect.size.height - 4)
let yPadding = 2.0
let newRect = CGRect(x: textRectFirstLine.origin.x - 9, y: textRect.origin.y + yPadding, width: 4, height: textRect.size.height - (2 * yPadding))

var addCurrentRect = true

for rectIdx in previousRects.indices.reversed() {
var (level, rect) = previousRects[rectIdx]

if level < currentLevel {
let rectBottom = rect.maxY + (2 * yPadding)

if level <= currentLevel, newRect.minY <= rectBottom {
// If there are lower levels than the current one, we want to adjust the height to include the current level
rect = CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height + textRect.size.height)
previousRects[rectIdx] = (level, rect)

// If it's the same level, we don't want to draw the same rect twice, but we need to continue to adjust other previousRects as well
if level == currentLevel {
addCurrentRect = false
}
} else {
// If there are higher levels than the current one, we want to draw these rects now
UIBezierPath(roundedRect: rect, cornerRadius: 2).fill()
previousRects.remove(at: rectIdx)
}
}

previousRects.append((currentLevel, newRect))
if addCurrentRect {
previousRects.append((currentLevel, newRect))
}
})

// Any remaining rects need to be drawn now
Expand Down
2 changes: 1 addition & 1 deletion Source/CDMarkdownQuote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

open class CDMarkdownQuote: CDMarkdownLevelElement {

fileprivate static let regex = "(?:(?<=\\n)|^)(\\>{1,%@})\\s*(.+?)(?:(?=\\n\\n)|$|(?=\\>))"
fileprivate static let regex = "(?:(?<=\\n)|^)(\\>{1,%@})\\s*(.+?)(?:(?=\\n\\n)|$|(?=\\n\\>))"

open var font: CDFont?
open var maxLevel: Int
Expand Down
Loading