Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpret issue/pr links like GitHub does in markdown #165

Merged
merged 10 commits into from
Jan 30, 2024
17 changes: 16 additions & 1 deletion Lambdas/GHHooks/Extensions/String+Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ extension String {
assert(hardLimit > 0, "Hard length limit must be greater than zero (got \(hardLimit)).")
assert(hardLimit >= maxVisualLength, "maxVisualLength '\(maxVisualLength)' can't be more than hardLimit '\(hardLimit)'.")

/// Interpret urls like GitHub does.
/// For example GitHub changes `https://github.com/vapor/penny-bot/issues/99` to
/// `[vapor/penny-bot#99](https://github.com/vapor/penny-bot/issues/99)` which
/// ends up looking like a blue `vapor/penny-bot#99` text linked to the url.
let regex = #/
https://(?:www\.)?github\.com
/(?<org>[A-Za-z0-9](?:[A-Za-z0-9\-]*[A-Za-z0-9])?)
/(?<repo>[A-Za-z0-9.\-_]+)
/(?:pull|issues)
/(?<number>\d+)
/#
let withModifiedLinks = self.replacing(regex) { match in
"[\(match.output.org)/\(match.output.repo)#\(match.output.number)](\(self[match.range]))"
}

/// Remove all HTML elements and all links lacking a destination; they don't look good in Discord.
let document1 = Document(parsing: self)
let document1 = Document(parsing: withModifiedLinks)
var htmlRemover = HTMLAndImageRemover()
guard let markup1 = htmlRemover.visit(document1) else { return "" }
var emptyLinksRemover = EmptyLinksRemover()
Expand Down
4 changes: 2 additions & 2 deletions Tests/PennyTests/Tests/+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ extension XCTestCase {
XCTFail(
"""
Not equal at line \(idx + 1):
Expected: \(line1.debugDescription)
Got: \(line2.debugDescription)
Got: \(line1.debugDescription)
Expected: \(line2.debugDescription)
""",
file: file,
line: line
Expand Down
21 changes: 21 additions & 0 deletions Tests/PennyTests/Tests/GHHooksTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,27 @@ class GHHooksTests: XCTestCase {
There should be no more `Sendable` warnings in Vapor, even with complete concurrency checking turned on.
""")
}

/// Test modifying GitHub links
do {
let text = """
Final stage of Vapor’s `Sendable` journey as `Request` is now `Sendable` at https://github.com/swift-server/swiftly/pull/9.

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.
"""

let formatted = text.formatMarkdown(
maxVisualLength: 512,
hardLimit: 2_048,
trailingTextMinLength: 128
)

XCTAssertMultilineStringsEqual(formatted, """
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).

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.
""")
}
}

func testHeadingFinder() async throws {
Expand Down