Skip to content

Commit

Permalink
Introduce case-insensitivity for reference link labels (#16)
Browse files Browse the repository at this point in the history
Merge PR from @ezfe
* Introduce case-insensitivity for reference link labels
* Moved `.lowercased()`
  • Loading branch information
ezfe authored and JohnSundell committed Nov 29, 2019
1 parent d2c8dca commit af743ad
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/Ink/API/MarkdownParser.swift
Expand Up @@ -42,7 +42,7 @@ public struct MarkdownParser {
public func parse(_ markdown: String) -> Markdown {
var reader = Reader(string: markdown)
var fragments = [ParsedFragment]()
var urlsByName = [Substring : URL]()
var urlsByName = [String : URL]()
var metadata: Metadata?

while !reader.didReachEnd {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Ink/Internal/NamedURLCollection.swift
Expand Up @@ -5,13 +5,13 @@
*/

internal struct NamedURLCollection {
private let urlsByName: [Substring : URL]
private let urlsByName: [String : URL]

init(urlsByName: [Substring : URL]) {
init(urlsByName: [String : URL]) {
self.urlsByName = urlsByName
}

func url(named name: Substring) -> URL? {
urlsByName[name]
urlsByName[name.lowercased()]
}
}
4 changes: 2 additions & 2 deletions Sources/Ink/Internal/URLDeclaration.swift
Expand Up @@ -5,7 +5,7 @@
*/

internal struct URLDeclaration: Readable {
var name: Substring
var name: String
var url: URL

static func read(using reader: inout Reader) throws -> Self {
Expand All @@ -15,6 +15,6 @@ internal struct URLDeclaration: Readable {
try reader.readWhitespaces()
let url = reader.readUntilEndOfLine()

return URLDeclaration(name: name, url: url)
return URLDeclaration(name: name.lowercased(), url: url)
}
}
13 changes: 13 additions & 0 deletions Tests/InkTests/LinkTests.swift
Expand Up @@ -23,6 +23,18 @@ final class LinkTests: XCTestCase {
XCTAssertEqual(html, #"<p><a href="swiftbysundell.com">Title</a></p>"#)
}

func testCaseMismatchedLinkWithReference() {
let html = MarkdownParser().html(from: """
[Title][Foo]
[Title][αγω]
[FOO]: /url
[ΑΓΩ]: /φου
""")

XCTAssertEqual(html, #"<p><a href="/url">Title</a> <a href="/φου">Title</a></p>"#)
}

func testNumericLinkWithReference() {
let html = MarkdownParser().html(from: """
[1][1]
Expand Down Expand Up @@ -59,6 +71,7 @@ extension LinkTests {
return [
("testLinkWithURL", testLinkWithURL),
("testLinkWithReference", testLinkWithReference),
("testCaseMismatchedLinkWithReference", testCaseMismatchedLinkWithReference),
("testNumericLinkWithReference", testNumericLinkWithReference),
("testBoldLinkWithInternalMarkers", testBoldLinkWithInternalMarkers),
("testBoldLinkWithExternalMarkers", testBoldLinkWithExternalMarkers),
Expand Down

0 comments on commit af743ad

Please sign in to comment.