Skip to content

Commit

Permalink
Merge branch 'master' into crlf
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSundell authored Apr 7, 2023
2 parents d2435b9 + fb342e1 commit 2b1a569
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 10 deletions.
4 changes: 1 addition & 3 deletions Sources/Ink/API/MarkdownParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ public struct MarkdownParser {
let type = fragmentType(for: reader.currentCharacter,
nextCharacter: reader.nextCharacter)

#if swift(>=5.4)
#warning("review compiler crash work-around below")
#elseif swift(>=5.3) && os(Linux)
#if swift(>=5.3) && swift(<5.4) && os(Linux)
// inline function call to work around https://bugs.swift.org/browse/SR-13645
let fragment: ParsedFragment = try {
let startIndex = reader.currentIndex
Expand Down
4 changes: 2 additions & 2 deletions Sources/Ink/Internal/FormattedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ private extension FormattedText {
break
}

guard reader.previousCharacter != "\\" && !(sequentialSpaceCount >= 2) else {
guard reader.previousCharacter != "\\" && sequentialSpaceCount < 2 else {
text.components.append(.linebreak)
skipCharacter()
continue
}

guard !nextCharacter.isAny(of: ["\n", "#", "<", "`", "\r\n"]) else {
guard !nextCharacter.isAny(of: ["\n", "#", "<", "`", "-", "\r\n"]) else {
break
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Ink/Internal/Link.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal struct Link: Fragment {

if reader.currentCharacter == "(" {
reader.advanceIndex()
let url = try reader.read(until: ")")
let url = try reader.read(until: ")", balanceAgainst: "(")
return Link(target: .url(url), text: text)
} else {
try reader.read("[")
Expand Down
10 changes: 9 additions & 1 deletion Sources/Ink/Internal/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ internal struct List: Fragment {
private var items = [Item]()

static func read(using reader: inout Reader) throws -> List {
try read(using: &reader, indentationLength: 0)
// Calculate initial indentation
var indentationLength = 0
while reader.previousCharacter?.isSameLineWhitespace == true {
indentationLength += 1
reader.rewindIndex()
}
reader.advanceIndex(by: indentationLength)

return try read(using: &reader, indentationLength: indentationLength)
}

private static func read(using reader: inout Reader,
Expand Down
16 changes: 14 additions & 2 deletions Sources/Ink/Internal/Reader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ extension Reader {
mutating func read(until character: Character,
required: Bool = true,
allowWhitespace: Bool = true,
allowLineBreaks: Bool = false) throws -> Substring {
allowLineBreaks: Bool = false,
balanceAgainst balancingCharacter: Character? = nil) throws -> Substring {
let startIndex = currentIndex
var characterBalance = 0

while !didReachEnd {
guard currentCharacter != character else {
guard currentCharacter != character || characterBalance > 0 else {
let result = string[startIndex..<currentIndex]
advanceIndex()
return result
Expand All @@ -55,6 +57,16 @@ extension Reader {
break
}

if let balancingCharacter = balancingCharacter {
if currentCharacter == balancingCharacter {
characterBalance += 1
}

if currentCharacter == character {
characterBalance -= 1
}
}

advanceIndex()
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/InkTests/LinkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ final class LinkTests: XCTestCase {
XCTAssertEqual(html, "<p><a href=\"/he_llo\">He_llo</a></p>")
}

func testLinkWithParenthesis() {
let html = MarkdownParser().html(from: "[Hello](/(hello))")
XCTAssertEqual(html, "<p><a href=\"/(hello)\">Hello</a></p>")
}

func testLinkWithNestedParenthesis() {
let html = MarkdownParser().html(from: "[Hello](/(h(e(l(l(o()))))))")
XCTAssertEqual(html, "<p><a href=\"/(h(e(l(l(o())))))\">Hello</a></p>")
}

func testLinkWithParenthesisAndClosingParenthesisInContent() {
let html = MarkdownParser().html(from: "[Hello](/(hello)))")
XCTAssertEqual(html, "<p><a href=\"/(hello)\">Hello</a>)</p>")
}

func testUnterminatedLink() {
let html = MarkdownParser().html(from: "[Hello]")
XCTAssertEqual(html, "<p>[Hello]</p>")
Expand All @@ -81,6 +96,9 @@ extension LinkTests {
("testBoldLinkWithInternalMarkers", testBoldLinkWithInternalMarkers),
("testBoldLinkWithExternalMarkers", testBoldLinkWithExternalMarkers),
("testLinkWithUnderscores", testLinkWithUnderscores),
("testLinkWithParenthesis", testLinkWithParenthesis),
("testLinkWithNestedParenthesis", testLinkWithNestedParenthesis),
("testLinkWithParenthesisAndClosingParenthesisInContent", testLinkWithParenthesisAndClosingParenthesisInContent),
("testUnterminatedLink", testUnterminatedLink),
("testLinkWithEscapedSquareBrackets", testLinkWithEscapedSquareBrackets)
]
Expand Down
23 changes: 22 additions & 1 deletion Tests/InkTests/ListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ final class ListTests: XCTestCase {

XCTAssertEqual(html, "<ul><li>One -Two</li><li>Three</li></ul>")
}

func testOrderedIndentedList() {
let html = MarkdownParser().html(from: """
1. One
2. Two
""")

XCTAssertEqual(html, #"<ol><li>One</li><li>Two</li></ol>"#)
}

func testUnorderedIndentedList() {
let html = MarkdownParser().html(from: """
- One
- Two
- Three
""")

XCTAssertEqual(html, "<ul><li>One</li><li>Two</li><li>Three</li></ul>")
}
}

extension ListTests {
Expand All @@ -148,7 +167,9 @@ extension ListTests {
("testMixedList", testMixedList),
("testUnorderedListWithMultiLineItem", testUnorderedListWithMultiLineItem),
("testUnorderedListWithNestedList", testUnorderedListWithNestedList),
("testUnorderedListWithInvalidMarker", testUnorderedListWithInvalidMarker)
("testUnorderedListWithInvalidMarker", testUnorderedListWithInvalidMarker),
("testOrderedIndentedList", testUnorderedIndentedList),
("testUnorderedIndentedList", testUnorderedIndentedList),
]
}
}
14 changes: 14 additions & 0 deletions Tests/InkTests/TextFormattingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ final class TextFormattingTests: XCTestCase {
XCTAssertEqual(html, "<p># Not a title *Not italic*</p>")
}


func testListAfterFormattedText() {
let html = MarkdownParser().html(from: """
This is a test
- One
- Two
""")

XCTAssertEqual(html, """
<p>This is a test</p><ul><li>One</li><li>Two</li></ul>
""")
}

func testDoubleSpacedHardLinebreak() {
let html = MarkdownParser().html(from: "Line 1 \nLine 2")

Expand Down Expand Up @@ -189,6 +202,7 @@ extension TextFormattingTests {
("testSingleLineBlockquote", testSingleLineBlockquote),
("testMultiLineBlockquote", testMultiLineBlockquote),
("testEscapingSymbolsWithBackslash", testEscapingSymbolsWithBackslash),
("testListAfterFormattedText", testListAfterFormattedText),
("testDoubleSpacedHardLinebreak", testDoubleSpacedHardLinebreak),
("testEscapedHardLinebreak", testEscapedHardLinebreak)
]
Expand Down

0 comments on commit 2b1a569

Please sign in to comment.