Skip to content

Commit

Permalink
Fix issue where indentation was removed when removing duplicate empty…
Browse files Browse the repository at this point in the history
… lines.
  • Loading branch information
eneko committed Oct 25, 2017
1 parent 0a97a7c commit c1245e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Sources/MarkdownGenerator/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ extension String: MarkdownConvertible {

/// Remove consecutive blank lines from a string output
public var removingConsecutiveBlankLines: String {
let lines = components(separatedBy: .newlines).map { $0.trimmingCharacters(in: .whitespaces) }
let lines = components(separatedBy: .newlines)
let filtered: [(offset: Int, element: String)] = lines.enumerated().filter { line in
return line.offset == 0 || lines[line.offset].isEmpty == false || lines[line.offset - 1].isEmpty == false
return line.offset == 0 ||
lines[line.offset].trimmingCharacters(in: .whitespaces).isEmpty == false ||
lines[line.offset - 1].trimmingCharacters(in: .whitespaces).isEmpty == false
}
return filtered.map { $0.element }.joined(separator: String.newLine)
}
Expand Down
32 changes: 31 additions & 1 deletion Tests/MarkdownGeneratorTests/ConsecutiveBlankLinesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ class ConsecutiveBlankLinesTests: XCTestCase {
XCTAssertEqual(input.removingConsecutiveBlankLines, output)
}

func testLists() {
let input = """
- something
- something else
- nested
- nested too
- back again
- final item
"""
let output = """
- something
- something else
- nested
- nested too
- back again
- final item
"""
XCTAssertEqual(input.removingConsecutiveBlankLines, output)
}

static var allTests = [
("testEmpty", testEmpty),
("testSingle", testSingle),
Expand All @@ -116,7 +145,8 @@ class ConsecutiveBlankLinesTests: XCTestCase {
("testLeadingAndTrailing", testLeadingAndTrailing),
("testLeadingAndTrailingConsecutive", testLeadingAndTrailingConsecutive),
("testInBetweenText", testInBetweenText),
("testTripleBlankLines", testTripleBlankLines)
("testTripleBlankLines", testTripleBlankLines),
("testLists", testLists)
]

}

0 comments on commit c1245e1

Please sign in to comment.