commit 7a9c1b86088491252b0d3a55b52d969241ef7fb1
parent a9d6755b37ba094e052a3ecf6856ee22034dae4d
Author: FIGBERT <figbert@figbert.com>
Date: Sat, 1 Oct 2022 23:05:37 -0700
Encode URLs in link lines
Diffstat:
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/Sources/SwiftGemtext/SwiftGemtext.swift b/Sources/SwiftGemtext/SwiftGemtext.swift
@@ -48,7 +48,10 @@ public struct Gemtext {
let url = base.components(separatedBy: .whitespaces).first!
let caption = base.dropFirst(url.count).trimmingCharacters(in: .whitespaces)
- return LineType.Link(URL(string: url, relativeTo: rel)!, caption.isEmpty ? nil : caption)
+ return LineType.Link(
+ URL(string: url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!, relativeTo: rel)!,
+ caption.isEmpty ? nil : caption
+ )
} else if line.starts(with: "* ") { // Unordered List Line
return LineType.UnorderedList(line.dropFirst(2).trimmingCharacters(in: .whitespacesAndNewlines))
} else if line.starts(with: ">") { // Quote Line
diff --git a/Tests/SwiftGemtextTests/SwiftGemtextTests.swift b/Tests/SwiftGemtextTests/SwiftGemtextTests.swift
@@ -101,10 +101,23 @@ final class SwiftGemtextTests: XCTestCase {
XCTAssertEqual(manual, Gemtext().parse(source))
}
+ func testUnicodeURLs() {
+ let source = """
+ => gemini://ostov-larion.srht.site/articles/педо.gmi
+ => gemini://basnja.ru/fables/babrius/лошадь-и-осел-r5858/ hello
+ """
+ let run = Gemtext().parse(source)
+ XCTAssertEqual([
+ LineType.Link(URL(string: "gemini://ostov-larion.srht.site/articles/%D0%BF%D0%B5%D0%B4%D0%BE.gmi")!, nil),
+ LineType.Link(URL(string: "gemini://basnja.ru/fables/babrius/%D0%BB%D0%BE%D1%88%D0%B0%D0%B4%D1%8C-%D0%B8-%D0%BE%D1%81%D0%B5%D0%BB-r5858/")!, "hello"),
+ ], run)
+ }
+
static var allTests = [
("testSingleLine", testSingleLine),
("testPlainGemini", testPlainGemini),
("testRelativeURLs", testRelativeURLs),
- ("testComplexGemini", testComplexGemini)
+ ("testComplexGemini", testComplexGemini),
+ ("testUnicodeURLs", testUnicodeURLs),
]
}