Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for system mentions #5

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions parser/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down Expand Up @@ -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:] {
Expand All @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions testdata/mentions.test
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]}]