Skip to content

Commit

Permalink
Merge pull request #38 from jespino/handling-autolink-nodes
Browse files Browse the repository at this point in the history
Handling autolink nodes too, not only text nodes
  • Loading branch information
jespino committed Jan 31, 2019
2 parents 5a46ac9 + fc33ac1 commit b0ad829
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
20 changes: 17 additions & 3 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,27 @@ func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*mode
return false
}

if textNode, ok := node.(*markdown.Text); ok {
startPos, endPos := textNode.Range.Position+offset, textNode.Range.End+offset
origText := postText[startPos:endPos]
origText := ""
startPos := 0
endPos := 0

if autolinkNode, ok := node.(*markdown.Autolink); ok {
startPos, endPos = autolinkNode.RawDestination.Position+offset, autolinkNode.RawDestination.End+offset
origText = postText[startPos:endPos]
if autolinkNode.Destination() != origText {
mlog.Error(fmt.Sprintf("Markdown autolink did not match range text, '%s' != '%s'", autolinkNode.Destination(), origText))
return true
}
} else if textNode, ok := node.(*markdown.Text); ok {
startPos, endPos = textNode.Range.Position+offset, textNode.Range.End+offset
origText = postText[startPos:endPos]
if textNode.Text != origText {
mlog.Error(fmt.Sprintf("Markdown text did not match range text, '%s' != '%s'", textNode.Text, origText))
return true
}
}

if origText != "" {
newText := origText
for _, l := range links {
newText = l.Replace(newText)
Expand All @@ -78,6 +91,7 @@ func (p *Plugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*mode
offset += len(newText) - len(origText)
}
}

return true
})
post.Message = postText
Expand Down
24 changes: 19 additions & 5 deletions server/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestSpecialCases(t *testing.T) {
}, &Link{
Pattern: "(Example)",
Template: "[Example](https://example.com)",
}, &Link{
Pattern: "(https://mattermost.atlassian.net/browse/)(MM)(-)(?P<jira_id>\\d+)",
Template: "[MM-$jira_id](https://mattermost.atlassian.net/browse/MM-$jira_id)",
}, &Link{
Pattern: "(foo!bar)",
Template: "fb",
Expand Down Expand Up @@ -160,16 +163,27 @@ func TestSpecialCases(t *testing.T) {
}, {
"foo!bar & foo!bar\nfoo!bar & foo!bar\nfoo!bar & foo!bar",
"fb & fb\nfb & fb\nfb & fb",
}, {
"https://mattermost.atlassian.net/browse/MM-12345",
"[MM-12345](https://mattermost.atlassian.net/browse/MM-12345)",
}, {
"Welcome https://mattermost.atlassian.net/browse/MM-12345",
"Welcome [MM-12345](https://mattermost.atlassian.net/browse/MM-12345)",
}, {
"text https://mattermost.atlassian.net/browse/MM-12345 other text",
"text [MM-12345](https://mattermost.atlassian.net/browse/MM-12345) other text",
},
}

for _, tt := range tests {
post := &model.Post{
Message: tt.inputMessage,
}
t.Run(tt.inputMessage, func(t *testing.T) {
post := &model.Post{
Message: tt.inputMessage,
}

rpost, _ := p.MessageWillBePosted(&plugin.Context{}, post)
rpost, _ := p.MessageWillBePosted(&plugin.Context{}, post)

assert.Equal(t, tt.expectedMessage, rpost.Message)
assert.Equal(t, tt.expectedMessage, rpost.Message)
})
}
}

0 comments on commit b0ad829

Please sign in to comment.