GH-1805: Replace unfurl link with card title (#1869)

* <Jump To Card>

* GOALNG Ci lint error

* ToDo Comment for internationalize Jump To Card
This commit is contained in:
Hossein 2021-11-25 09:05:32 -05:00 committed by GitHub
parent 083d89953c
commit 79ce416896
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -291,7 +291,8 @@ func postWithBoardsEmbed(post *mmModel.Post, showBoardsUnfurl bool) *mmModel.Pos
return post return post
} }
firstLink := getFirstLink(post.Message) firstLink, newPostMessage := getFirstLinkAndShortenAllBoardsLink(post.Message)
post.Message = newPostMessage
if firstLink == "" { if firstLink == "" {
return post return post
@ -342,26 +343,33 @@ func postWithBoardsEmbed(post *mmModel.Post, showBoardsUnfurl bool) *mmModel.Pos
return post return post
} }
func getFirstLink(str string) string { func getFirstLinkAndShortenAllBoardsLink(postMessage string) (firstLink, newPostMessage string) {
firstLink := "" newPostMessage = postMessage
seenLinks := make(map[string]bool)
markdown.Inspect(postMessage, func(blockOrInline interface{}) bool {
if autoLink, ok := blockOrInline.(*markdown.Autolink); ok {
link := autoLink.Destination()
markdown.Inspect(str, func(blockOrInline interface{}) bool { if firstLink == "" {
if _, ok := blockOrInline.(*markdown.Autolink); ok {
if link := blockOrInline.(*markdown.Autolink).Destination(); firstLink == "" {
firstLink = link firstLink = link
return false }
if seen := seenLinks[link]; !seen && isBoardsLink(link) {
// TODO: Make sure that <Jump To Card> is Internationalized and translated to the Users Language preference
markdownFormattedLink := fmt.Sprintf("[%s](%s)", "<Jump To Card>", link)
newPostMessage = strings.ReplaceAll(newPostMessage, link, markdownFormattedLink)
seenLinks[link] = true
} }
} }
if inlineLink, ok := blockOrInline.(*markdown.InlineLink); ok { if inlineLink, ok := blockOrInline.(*markdown.InlineLink); ok {
if link := inlineLink.Destination(); firstLink == "" { if link := inlineLink.Destination(); firstLink == "" {
firstLink = link firstLink = link
return false
} }
} }
return true return true
}) })
return firstLink return firstLink, newPostMessage
} }
func returnBoardsParams(pathArray []string) (workspaceID, boardID, viewID, cardID string) { func returnBoardsParams(pathArray []string) (workspaceID, boardID, viewID, cardID string) {
@ -406,3 +414,23 @@ func returnBoardsParams(pathArray []string) (workspaceID, boardID, viewID, cardI
} }
return workspaceID, boardID, viewID, cardID return workspaceID, boardID, viewID, cardID
} }
func isBoardsLink(link string) bool {
u, err := url.Parse(link)
if err != nil {
return false
}
urlPath := u.Path
urlPath = strings.TrimPrefix(urlPath, "/")
urlPath = strings.TrimSuffix(urlPath, "/")
pathSplit := strings.Split(strings.ToLower(urlPath), "/")
if len(pathSplit) == 0 {
return false
}
workspaceID, boardID, viewID, cardID := returnBoardsParams(pathSplit)
return workspaceID != "" && boardID != "" && viewID != "" && cardID != ""
}