Skip to content

Commit 62006b3

Browse files
authored
Interpret issue/pr links like GitHub does in markdown (#165)
* use 'package' instead of 'public' * move 'Fake' into 'PennyTests' * remove 'package' attributes in 'Fake' folder * remove import Fake s * done * make the regex more resistant to failure
1 parent 4f9a257 commit 62006b3

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Lambdas/GHHooks/Extensions/String+Document.swift

+16-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,23 @@ extension String {
1515
assert(hardLimit > 0, "Hard length limit must be greater than zero (got \(hardLimit)).")
1616
assert(hardLimit >= maxVisualLength, "maxVisualLength '\(maxVisualLength)' can't be more than hardLimit '\(hardLimit)'.")
1717

18+
/// Interpret urls like GitHub does.
19+
/// For example GitHub changes `https://github.com/vapor/penny-bot/issues/99` to
20+
/// `[vapor/penny-bot#99](https://github.com/vapor/penny-bot/issues/99)` which
21+
/// ends up looking like a blue `vapor/penny-bot#99` text linked to the url.
22+
let regex = #/
23+
https://(?:www\.)?github\.com
24+
/(?<org>[A-Za-z0-9](?:[A-Za-z0-9\-]*[A-Za-z0-9])?)
25+
/(?<repo>[A-Za-z0-9.\-_]+)
26+
/(?:pull|issues)
27+
/(?<number>\d+)
28+
/#
29+
let withModifiedLinks = self.replacing(regex) { match in
30+
"[\(match.output.org)/\(match.output.repo)#\(match.output.number)](\(self[match.range]))"
31+
}
32+
1833
/// Remove all HTML elements and all links lacking a destination; they don't look good in Discord.
19-
let document1 = Document(parsing: self)
34+
let document1 = Document(parsing: withModifiedLinks)
2035
var htmlRemover = HTMLAndImageRemover()
2136
guard let markup1 = htmlRemover.visit(document1) else { return "" }
2237
var emptyLinksRemover = EmptyLinksRemover()

Tests/PennyTests/Tests/+XCTest.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ extension XCTestCase {
2828
XCTFail(
2929
"""
3030
Not equal at line \(idx + 1):
31-
Expected: \(line1.debugDescription)
32-
Got: \(line2.debugDescription)
31+
Got: \(line1.debugDescription)
32+
Expected: \(line2.debugDescription)
3333
""",
3434
file: file,
3535
line: line

Tests/PennyTests/Tests/GHHooksTests.swift

+21
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,27 @@ class GHHooksTests: XCTestCase {
502502
There should be no more `Sendable` warnings in Vapor, even with complete concurrency checking turned on.
503503
""")
504504
}
505+
506+
/// Test modifying GitHub links
507+
do {
508+
let text = """
509+
Final stage of Vapor’s `Sendable` journey as `Request` is now `Sendable` at https://github.com/swift-server/swiftly/pull/9.
510+
511+
There should https://github.com/vapor-bad-link/issues/44 be no more `Sendable` warnings in Vaporhttps://github.com/vapor/penny-bot/issues/98, even with complete concurrency checking turned on.
512+
"""
513+
514+
let formatted = text.formatMarkdown(
515+
maxVisualLength: 512,
516+
hardLimit: 2_048,
517+
trailingTextMinLength: 128
518+
)
519+
520+
XCTAssertMultilineStringsEqual(formatted, """
521+
Final stage of Vapor’s `Sendable` journey as `Request` is now `Sendable` at [swift-server/swiftly#9](https://github.com/swift-server/swiftly/pull/9).
522+
523+
There should https://github.com/vapor-bad-link/issues/44 be no more `Sendable` warnings in Vapor[vapor/penny-bot#98](https://github.com/vapor/penny-bot/issues/98), even with complete concurrency checking turned on.
524+
""")
525+
}
505526
}
506527

507528
func testHeadingFinder() async throws {

0 commit comments

Comments
 (0)