mirror of
git://jb55.com/damus
synced 2024-09-20 20:16:58 +00:00
Fix nostr: mention prefix bugs
The zero-width space was causing parsing issues. Not sure why we need this so I just removed it. Changelog-Fixed: Fix nostr:nostr:... bugs
This commit is contained in:
parent
72a060c7b3
commit
139be9eef2
@ -570,16 +570,12 @@ func build_post(post: NSMutableAttributedString, action: PostAction, uploadedMed
|
|||||||
|
|
||||||
// Add zero-width space in case text preceding the mention is not a whitespace.
|
// Add zero-width space in case text preceding the mention is not a whitespace.
|
||||||
// In the case where the character preceding the mention is a whitespace, the added zero-width space will be stripped out.
|
// In the case where the character preceding the mention is a whitespace, the added zero-width space will be stripped out.
|
||||||
post.replaceCharacters(in: range, with: "\u{200B}\(normalized_link)\u{200B}")
|
post.replaceCharacters(in: range, with: "\(normalized_link)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var content = post.string
|
var content = post.string
|
||||||
// If two zero-width spaces are next to each other, normalize it to just one zero-width space.
|
|
||||||
.replacingOccurrences(of: "\u{200B}\u{200B}", with: "\u{200B}")
|
|
||||||
// If zero-width space is next to an actual whitespace, remove the zero-width space.
|
|
||||||
.replacingOccurrences(of: " \u{200B}", with: " ")
|
|
||||||
.replacingOccurrences(of: "\u{200B} ", with: " ")
|
|
||||||
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
|
||||||
|
|
||||||
let imagesString = uploadedMedias.map { $0.uploadedURL.absoluteString }.joined(separator: " ")
|
let imagesString = uploadedMedias.map { $0.uploadedURL.absoluteString }.joined(separator: " ")
|
||||||
|
@ -34,8 +34,11 @@ struct UserSearch: View {
|
|||||||
guard let pk = bech32_pubkey(user.pubkey) else {
|
guard let pk = bech32_pubkey(user.pubkey) else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let tagAttributedString = user_tag_attr_string(profile: user.profile, pubkey: pk)
|
|
||||||
appendUserTag(withTag: tagAttributedString)
|
let user_tag = user_tag_attr_string(profile: user.profile, pubkey: pk)
|
||||||
|
user_tag.append(.init(string: " "))
|
||||||
|
|
||||||
|
appendUserTag(withTag: user_tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func appendUserTag(withTag tagAttributedString: NSMutableAttributedString) {
|
private func appendUserTag(withTag tagAttributedString: NSMutableAttributedString) {
|
||||||
@ -97,14 +100,12 @@ struct UserSearch_Previews: PreviewProvider {
|
|||||||
func user_tag_attr_string(profile: Profile?, pubkey: String) -> NSMutableAttributedString {
|
func user_tag_attr_string(profile: Profile?, pubkey: String) -> NSMutableAttributedString {
|
||||||
let display_name = Profile.displayName(profile: profile, pubkey: pubkey)
|
let display_name = Profile.displayName(profile: profile, pubkey: pubkey)
|
||||||
let name = display_name.username.truncate(maxLength: 50)
|
let name = display_name.username.truncate(maxLength: 50)
|
||||||
let tagString = "@\(name)\u{200B} "
|
let tagString = "@\(name)"
|
||||||
|
|
||||||
let tagAttributedString = NSMutableAttributedString(string: tagString,
|
return NSMutableAttributedString(string: tagString, attributes: [
|
||||||
attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0),
|
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18.0),
|
||||||
NSAttributedString.Key.link: "nostr:\(pubkey)"])
|
NSAttributedString.Key.foregroundColor: UIColor.label,
|
||||||
tagAttributedString.removeAttribute(.link, range: NSRange(location: tagAttributedString.length - 2, length: 2))
|
NSAttributedString.Key.link: "damus:nostr:\(pubkey)"
|
||||||
tagAttributedString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.label], range: NSRange(location: tagAttributedString.length - 2, length: 2))
|
])
|
||||||
|
|
||||||
return tagAttributedString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,15 +136,21 @@ class ReplyTests: XCTestCase {
|
|||||||
guard let hex_pk = bech32_pubkey_decode(pk) else {
|
guard let hex_pk = bech32_pubkey_decode(pk) else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let content = """
|
|
||||||
@\(pk)
|
|
||||||
@\(pk)
|
|
||||||
"""
|
|
||||||
|
|
||||||
let blocks = parse_mentions(content: content, tags: []).blocks
|
let profile = Profile(name: "jb55", display_name: "Will", about: nil, picture: nil, banner: nil, website: nil, lud06: nil, lud16: nil, nip05: nil, damus_donation: nil)
|
||||||
|
|
||||||
|
let post = user_tag_attr_string(profile: profile, pubkey: pk)
|
||||||
|
post.append(.init(string: "\n"))
|
||||||
|
post.append(user_tag_attr_string(profile: profile, pubkey: pk))
|
||||||
|
post.append(.init(string: "\n"))
|
||||||
|
|
||||||
|
let post_note = build_post(post: post, action: .posting(.none), uploadedMedias: [], references: [.p(hex_pk)])
|
||||||
|
|
||||||
let rendered = render_blocks(blocks: blocks)
|
|
||||||
let expected_render = "nostr:\(pk)\nnostr:\(pk)"
|
let expected_render = "nostr:\(pk)\nnostr:\(pk)"
|
||||||
|
XCTAssertEqual(post_note.content, expected_render)
|
||||||
|
|
||||||
|
let blocks = parse_mentions(content: post_note.content, tags: []).blocks
|
||||||
|
let rendered = render_blocks(blocks: blocks)
|
||||||
|
|
||||||
XCTAssertEqual(rendered, expected_render)
|
XCTAssertEqual(rendered, expected_render)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user