mention: fix broken mentions when there is text is directly after

If a user adds text right after a mention without a space, a space gets
added so the mention can be parsed correctly after posting.

Closes: https://github.com/damus-io/damus/issues/1872
Changelog-Fixed: Fix broken mentions when there is text is directly after
Lightning-url: LNURL1DP68GURN8GHJ7EM9W3SKCCNE9E3K7MF0D3H82UNVWQHKWUN9V4HXGCTHDC6RZVGR8SW3G
Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-01-18 17:56:32 -05:00
committed by William Casarin
parent aa1f75ad58
commit af75eed83a
2 changed files with 29 additions and 0 deletions

View File

@@ -617,6 +617,13 @@ func load_draft_for_post(drafts: Drafts, action: PostAction) -> DraftArtifacts?
func build_post(state: DamusState, post: NSMutableAttributedString, action: PostAction, uploadedMedias: [UploadedMedia], references: [RefId]) -> NostrPost {
post.enumerateAttributes(in: NSRange(location: 0, length: post.length), options: []) { attributes, range, stop in
if let link = attributes[.link] as? String {
let nextCharIndex = range.upperBound
if nextCharIndex < post.length,
let nextChar = post.attributedSubstring(from: NSRange(location: nextCharIndex, length: 1)).string.first,
!nextChar.isWhitespace {
post.insert(NSAttributedString(string: " "), at: nextCharIndex)
}
let normalized_link: String
if link.hasPrefix("damus:nostr:") {
// Replace damus:nostr: URI prefix with nostr: since the former is for internal navigation and not meant to be posted.

View File

@@ -142,6 +142,28 @@ final class PostViewTests: XCTestCase {
checkMentionLinkEditorHandling(content: content, replacementText: "", replacementRange: NSRange(location: 5, length: 28), shouldBeAbleToChangeAutomatically: true)
}
func testMentionLinkEditorHandling_noWhitespaceAfterLink1_addsWhitespace() {
var content: NSMutableAttributedString
content = NSMutableAttributedString(string: "Hello @user ")
content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5))
checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in
XCTAssertEqual(newManuallyEditedContent.string, "Hello @user up")
XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil))
})
}
func testMentionLinkEditorHandling_noWhitespaceAfterLink2_addsWhitespace() {
var content: NSMutableAttributedString
content = NSMutableAttributedString(string: "Hello @user test")
content.addAttribute(.link, value: "damus:1234", range: NSRange(location: 6, length: 5))
checkMentionLinkEditorHandling(content: content, replacementText: "up", replacementRange: NSRange(location: 11, length: 1), shouldBeAbleToChangeAutomatically: true, expectedNewCursorIndex: 13, handleNewContent: { newManuallyEditedContent in
XCTAssertEqual(newManuallyEditedContent.string, "Hello @user uptest")
XCTAssertNil(newManuallyEditedContent.attribute(.link, at: 11, effectiveRange: nil))
})
}
}
func checkMentionLinkEditorHandling(