fix(markdown): fix system mentions when text is too long (#4319)

Fixes https://github.com/status-im/status-desktop/issues/11520
This commit is contained in:
Jonathan Rainville 2023-11-14 16:56:40 -05:00 committed by GitHub
parent 0345612a31
commit ecbacb0a7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 66 deletions

2
go.mod
View File

@ -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

4
go.sum
View File

@ -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=

View File

@ -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

View File

@ -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 {

2
vendor/modules.txt vendored
View File

@ -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