feat: add support for system mentions

This change parses `0xXXXXX` as mentions such that they
can be translated to system mentions (like `@everyone`).
This commit is contained in:
Pascal Precht 2022-12-19 15:38:38 +01:00 committed by r4bbit
parent 7ee4aa8bbc
commit 8f1babe09d
3 changed files with 78 additions and 4 deletions

View File

@ -68,6 +68,7 @@ func (p *Parser) Inline(currBlock ast.Node, data []byte) {
const pkLength = 132
const compressedPkPrefixLen = 3
const systemMentionLength = 7
func mention(p *Parser, data []byte, offset int) (int, ast.Node) {
data = data[offset:]
@ -96,10 +97,7 @@ func mention(p *Parser, data []byte, offset int) (int, ast.Node) {
mention.Literal = data[1 : pkLength+1]
return i, mention
} else if n >= compressedPkPrefixLen+1 {
if data[1] != 'z' || data[2] != 'Q' || data[3] != '3' {
return 0, nil
}
} else if n >= compressedPkPrefixLen+1 && (data[1] == 'z' || data[2] == 'Q' || data[3] == '3') {
i := 1
for _, c := range data[1:] {
@ -117,6 +115,29 @@ func mention(p *Parser, data []byte, offset int) (int, ast.Node) {
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
}

View File

@ -712,6 +712,11 @@ func isValidCompressedPublicKeyChar(c byte) bool {
c == 'R' || c == 'S' || c == 'T' || c == 'U' || c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z'
}
func isValidSystemTagChar(c byte) bool {
return c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'
}
// TODO: this is not used
// Replace tab characters with spaces, aligning to the next TAB_SIZE column.
// always ends output with a newline

View File

@ -85,3 +85,51 @@ Invalid char @zQ3shh7B7RWHqbcFxZXwG9H9qnproDSvcCwqmSMzdwrvzN2Of
Not starting with zq3 @zq3shh7B7RWHqbcFxZXwG9H9qnproDSvcCwqmSMzdwrvzN2jf
+++
[{"type":"paragraph","children":[{"literal":"Not starting with zq3 @zq3shh7B7RWHqbcFxZXwG9H9qnproDSvcCwqmSMzdwrvzN2jf"}]}]
+++
Valid systemMention at the end @0x00001
+++
[{"type":"paragraph","children":[{"literal":"Valid systemMention at the end "},{"type":"mention","literal":"0x00001"}]}]
+++
Valid systemMention comma @0x00001,
+++
[{"type":"paragraph","children":[{"literal":"Valid systemMention comma "},{"type":"mention","literal":"0x00001"},{"literal":","}]}]
+++
Valid systemMention dot @0x00001.
+++
[{"type":"paragraph","children":[{"literal":"Valid systemMention dot "},{"type":"mention","literal":"0x00001"},{"literal":"."}]}]
+++
Valid systemMention colon @0x00001:
+++
[{"type":"paragraph","children":[{"literal":"Valid systemMention colon "},{"type":"mention","literal":"0x00001"},{"literal":":"}]}]
+++
Valid systemMention semi-colon @0x00001;
+++
[{"type":"paragraph","children":[{"literal":"Valid systemMention semi-colon "},{"type":"mention","literal":"0x00001"},{"literal":";"}]}]
+++
@0x00001 at the beginning
+++
[{"type":"paragraph","children":[{"literal":""},{"type":"mention","literal":"0x00001"},{"literal":" at the beginning"}]}]
+++
in the middle @0x00001 middle
+++
[{"type":"paragraph","children":[{"literal":"in the middle "},{"type":"mention","literal":"0x00001"},{"literal":" middle"}]}]
+++
too short @0x001
+++
[{"type":"paragraph","children":[{"literal":"too short @0x001"}]}]
+++
too long @0x000001
+++
[{"type":"paragraph","children":[{"literal":"too long @0x000001"}]}]
+++
Invalid char @0x000a1
+++
[{"type":"paragraph","children":[{"literal":"Invalid char @0x000a1"}]}]
+++
Not starting with 0x @x00001
+++
[{"type":"paragraph","children":[{"literal":"Not starting with 0x @x00001"}]}]
+++
Ends with 0 @0x00000
+++
[{"type":"paragraph","children":[{"literal":"Ends with 0 @0x00000"}]}]