From ecbacb0a7fd5e4e9ee15dc8a6aa4209a582c4162 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 14 Nov 2023 16:56:40 -0500 Subject: [PATCH] fix(markdown): fix system mentions when text is too long (#4319) Fixes https://github.com/status-im/status-desktop/issues/11520 --- go.mod | 2 +- go.sum | 4 +- .../status-im/markdown/parser/inline.go | 144 ++++++++++-------- .../status-im/markdown/parser/parser.go | 2 +- vendor/modules.txt | 2 +- 5 files changed, 88 insertions(+), 66 deletions(-) diff --git a/go.mod b/go.mod index fa6446c77..6f98aa724 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,7 @@ require ( github.com/prometheus/client_golang v1.16.0 github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a github.com/status-im/doubleratchet v3.0.0+incompatible - github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5 + github.com/status-im/markdown v0.0.0-20231114210825-6c2d15b5dc57 github.com/status-im/migrate/v4 v4.6.2-status.3 github.com/status-im/rendezvous v1.3.7 github.com/status-im/status-go/extkeys v1.1.2 diff --git a/go.sum b/go.sum index 87bdf0d9f..a2310e16d 100644 --- a/go.sum +++ b/go.sum @@ -1993,8 +1993,8 @@ github.com/status-im/gomoji v1.1.3-0.20220213022530-e5ac4a8732d4/go.mod h1:hmpnZ github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 h1:Oo2KZNP70KE0+IUJSidPj/BFS/RXNHmKIJOdckzml2E= github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= -github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5 h1:Mie/nQtRrI/+qqyV4j5ydesjoPh6v6KF583NOPLKFQY= -github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5/go.mod h1:5rjPyv3KffPNVbFjnsVy0NGj9+JeW40WvXLdxH1VKuE= +github.com/status-im/markdown v0.0.0-20231114210825-6c2d15b5dc57 h1:AuJFXERIFVzUjf9rrTb8vamFubB6Ks/e8aUasDr4pOM= +github.com/status-im/markdown v0.0.0-20231114210825-6c2d15b5dc57/go.mod h1:5rjPyv3KffPNVbFjnsVy0NGj9+JeW40WvXLdxH1VKuE= github.com/status-im/migrate/v4 v4.6.2-status.2/go.mod h1:c/kc90n47GZu/58nnz1OMLTf7uE4Da4gZP5qmU+A/v8= github.com/status-im/migrate/v4 v4.6.2-status.3 h1:Khwjb59NzniloUr5i9s9AtkEyqBbQFt1lkoAu66sAu0= github.com/status-im/migrate/v4 v4.6.2-status.3/go.mod h1:c/kc90n47GZu/58nnz1OMLTf7uE4Da4gZP5qmU+A/v8= diff --git a/vendor/github.com/status-im/markdown/parser/inline.go b/vendor/github.com/status-im/markdown/parser/inline.go index f5357dbd5..428b7f7ce 100644 --- a/vendor/github.com/status-im/markdown/parser/inline.go +++ b/vendor/github.com/status-im/markdown/parser/inline.go @@ -70,75 +70,97 @@ const pkLength = 132 const compressedPkPrefixLen = 3 const systemMentionLength = 7 +func getPublicKeyMention(data []byte, n int) (int, ast.Node) { + // need to start with 0x + if data[1] != '0' || data[2] != 'x' { + return 0, nil + } + + i := 3 + for i < pkLength+1 { + if !isValidPublicKeyChar(data[i]) { + return 0, nil + } + i++ + } + + // Check there's a space + if n != pkLength+1 && !isValidTerminatingMentionChar(data[pkLength+1]) { + return 0, nil + } + + mention := &ast.Mention{} + mention.Literal = data[1 : pkLength+1] + + return i, mention +} + +func getCompressedPublicKeyMention(data []byte, n int) (int, ast.Node) { + i := 1 + for _, c := range data[1:] { + if !isValidCompressedPublicKeyChar(c) { + break + } + i++ + } + + decodedPK := base58.Decode(string(data[2:i])) + decodedPKStr := hex.EncodeToString(decodedPK) + if !strings.HasPrefix(decodedPKStr, "e701") || len(decodedPKStr) != 70 { + return 0, nil + } + + mention := &ast.Mention{} + mention.Literal = data[1:i] + return i, mention +} + +func getSystemTagMention(data []byte, n int) (int, ast.Node) { + // need to start with 0x and can't end with 0 + if data[1] != '0' || data[2] != 'x' || data[systemMentionLength] == '0' { + return 0, nil + } + + i := 3 + for i < systemMentionLength+1 { + if !isValidSystemTagChar(data[i]) { + return 0, nil + } + i++ + } + + // Check there's a space + if n != systemMentionLength+1 && !isValidTerminatingMentionChar(data[systemMentionLength+1]) { + return 0, nil + } + + mention := &ast.Mention{} + mention.Literal = data[1 : systemMentionLength+1] + + return i, mention +} + func mention(p *Parser, data []byte, offset int) (int, ast.Node) { data = data[offset:] n := len(data) if n >= pkLength+1 { - // need to start with 0x - if data[1] != '0' || data[2] != 'x' { - return 0, nil + pos, node := getPublicKeyMention(data, n) + if node != nil { + return pos, node } - - i := 3 - for i < pkLength+1 { - if !isValidPublicKeyChar(data[i]) { - return 0, nil - } - i++ + } + if n >= compressedPkPrefixLen+1 && (data[1] == 'z' || data[2] == 'Q' || data[3] == '3') { + pos, node := getCompressedPublicKeyMention(data, n) + if node != nil { + return pos, node } - - // Check there's a space - if n != pkLength+1 && !isValidTerminatingMentionChar(data[pkLength+1]) { - return 0, nil + } + if n >= systemMentionLength+1 { + pos, node := getSystemTagMention(data, n) + if node != nil { + return pos, node } - - mention := &ast.Mention{} - mention.Literal = data[1 : pkLength+1] - - return i, mention - } else if n >= compressedPkPrefixLen+1 && (data[1] == 'z' || data[2] == 'Q' || data[3] == '3') { - - i := 1 - for _, c := range data[1:] { - if !isValidCompressedPublicKeyChar(c) { - break - } - i++ - } - - decodedPK := base58.Decode(string(data[2:i])) - decodedPKStr := hex.EncodeToString(decodedPK) - if !strings.HasPrefix(decodedPKStr, "e701") || len(decodedPKStr) != 70 { - return 0, nil - } - - mention := &ast.Mention{} - mention.Literal = data[1:i] - return i, mention - } else if n >= systemMentionLength+1 { - // need to start with 0x and can't end with 0 - if data[1] != '0' || data[2] != 'x' || data[systemMentionLength] == '0' { - return 0, nil - } - - i := 3 - for i < systemMentionLength+1 { - if !isValidSystemTagChar(data[i]) { - return 0, nil - } - i++ - } - - // Check there's a space - if n != systemMentionLength+1 && !isValidTerminatingMentionChar(data[systemMentionLength+1]) { - return 0, nil - } - - mention := &ast.Mention{} - mention.Literal = data[1 : systemMentionLength+1] - - return i, mention } return 0, nil diff --git a/vendor/github.com/status-im/markdown/parser/parser.go b/vendor/github.com/status-im/markdown/parser/parser.go index bbac725fe..6285ca727 100644 --- a/vendor/github.com/status-im/markdown/parser/parser.go +++ b/vendor/github.com/status-im/markdown/parser/parser.go @@ -696,7 +696,7 @@ func isValidStatusTagChar(c byte) bool { } func isValidTerminatingMentionChar(c byte) bool { - return isSpace(c) || c == '.' || c == ',' || c == ':' || c == ';' + return isSpace(c) || c == '.' || c == ',' || c == ':' || c == ';' || c == '!' || c == '?' } func isValidPublicKeyChar(c byte) bool { diff --git a/vendor/modules.txt b/vendor/modules.txt index b0b846044..71e0b9e46 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -915,7 +915,7 @@ github.com/status-im/go-multiaddr-ethv4 # github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 ## explicit github.com/status-im/keycard-go/derivationpath -# github.com/status-im/markdown v0.0.0-20230314100416-26c6f74522d5 +# github.com/status-im/markdown v0.0.0-20231114210825-6c2d15b5dc57 ## explicit; go 1.12 github.com/status-im/markdown github.com/status-im/markdown/ast