Stand with Ukraine 🇺🇦

Top Comments by Likes — Confluence Cloud Macro

confluence-content

The macro fetches comments (inline and footer) from a selected Confluence page, counts likes, sorts by popularity and date, and displays the top N comments. Defaults to the current page if no specific page is set. It supports nested comments and provides clickable links to the original comment.

Try for free

User Parameters

Page ID

Select a page for which to show top comments

123

Top Count

Enter a number of comments in the top list

Template

#set($allComments = [])

#set($topCount = $parameters.topCount)
#if(!$topCount || $topCount < 1)
    #set($topCount = 3)
    #end

    ## Default to the current page ID
    #set($selectedPageId = $page.id)
    
    ## Check if a user parameter for page ID is set
    #if($parameters.pageId)
    ## Parse parameters for spaceId and pageTitle
    
    #if($parameters.pageId.contains(":"))
      #set($paramParts = $parameters.pageIdentifier.split(":"))
      #set($spaceId = $paramParts[0])
      #set($pageTitle = $paramParts[1])
    #else
      #set($spaceId = $space.key)
      #set($pageTitle = $parameters.pageIdentifier)
    #end
    ## Fetch page ID using the Confluence REST API
    #set($pageSearchResponse = $ConfluenceManager.get("/wiki/rest/api/content?title=$pageTitle&spaceKey=$spaceId"))
    #if($pageSearchResponse)
    #set($selectedPageId = $pageSearchResponse.results[0].id) ## Override the selected page ID
    #end
    #end

    ## Fetch inline comments for the selected page
    #set($inlineCommentsResponse = $ConfluenceManager.get("/wiki/api/v2/pages/$selectedPageId/inline-comments"))
    #if($inlineCommentsResponse)
    #foreach($comment in $inlineCommentsResponse.results)
    #if($comment.status == "current")
    ## Fetch like count
    #set($likeCountResponse = $ConfluenceManager.get("/wiki/api/v2/inline-comments/$comment.id/likes/count"))
    #set($likeCount = $likeCountResponse.count)

    ## Fetch comment text
    #set($commentTextResponse = $ConfluenceManager.get("/wiki/api/v2/inline-comments/$comment.id?body-format=storage"))
    #set($commentText = $commentTextResponse.body.storage.value)

    ## Add comment details to the list
    #set($discard = $allComments.add({
    "id": $comment.id,
    "likeCount": $likeCount,
    "commentText": $commentText,
    "createdAt": $comment.version.createdAt,
    "webuiLink": "$baseUrl$comment._links.webui"
    }))

    ## Fetch and add child inline comments
    #set($childrenResponse = $ConfluenceManager.get("/wiki/api/v2/inline-comments/$comment.id/children"))
    #if($childrenResponse && $childrenResponse.results)
    #foreach($childComment in $childrenResponse.results)
    #if($childComment.status == "current")
    ## Fetch like count for the child comment
    #set($childLikeCountResponse = $ConfluenceManager.get("/wiki/api/v2/inline-comments/$childComment.id/likes/count"))
    #set($childLikeCount = $childLikeCountResponse.count)

    ## Fetch child comment text
    #set($childTextResponse = $ConfluenceManager.get("/wiki/api/v2/inline-comments/$childComment.id?body-format=storage"))
    #set($childCommentText = $childTextResponse.body.storage.value)

    ## Add child comment details to the list
    #set($discard = $allComments.add({
    "id": $childComment.id,
    "likeCount": $childLikeCount,
    "commentText": $childCommentText,
    "createdAt": $childComment.version.createdAt,
    "webuiLink": "$baseUrl$childComment._links.webui"
    }))
    #end
    #end
    #end
    #end
    #end
    #end

    ## Fetch footer comments for the selected page
    #set($footerCommentsResponse = $ConfluenceManager.get("/wiki/api/v2/pages/$selectedPageId/footer-comments"))
    #if($footerCommentsResponse)
    #foreach($comment in $footerCommentsResponse.results)
    #if($comment.status == "current")
    ## Fetch like count
    #set($likeCountResponse = $ConfluenceManager.get("/wiki/api/v2/footer-comments/$comment.id/likes/count"))
    #set($likeCount = $likeCountResponse.count)

    ## Fetch comment text
    #set($commentTextResponse = $ConfluenceManager.get("/wiki/api/v2/footer-comments/$comment.id?body-format=storage"))
    #set($commentText = $commentTextResponse.body.storage.value)

    ## Add comment details to the list
    #set($discard = $allComments.add({
    "id": $comment.id,
    "likeCount": $likeCount,
    "commentText": $commentText,
    "createdAt": $comment.version.createdAt,
    "webuiLink": "$baseUrl$comment._links.webui"
    }))

    ## Fetch and add child footer comments
    #set($childrenResponse = $ConfluenceManager.get("/wiki/api/v2/footer-comments/$comment.id/children"))
    #if($childrenResponse)
    #foreach($childComment in $childrenResponse.results)
    #if($childComment.status == "current")
    ## Fetch like count for the child comment
    #set($childLikeCountResponse = $ConfluenceManager.get("/wiki/api/v2/footer-comments/$childComment.id/likes/count"))
    #set($childLikeCount = $childLikeCountResponse.count)

    ## Fetch child comment text
    #set($childTextResponse = $ConfluenceManager.get("/wiki/api/v2/footer-comments/$childComment.id?body-format=storage"))
    #set($childCommentText = $childTextResponse.body.storage.value)

    ## Add child comment details to the list
    #set($discard = $allComments.add({
    "id": $childComment.id,
    "likeCount": $childLikeCount,
    "commentText": $childCommentText,
    "createdAt": $childComment.version.createdAt,
    "webuiLink": "$baseUrl$childComment._links.webui"
    }))
    #end
    #end
    #end
    #end
    #end
    #end

    ## Sort comments by like count (descending) and then by creation date (descending)
    #set($sortedComments = $SortTool.sort($allComments, ["likeCount:desc", "createdAt:desc"]))

    #if(!$sortedComments || $sortedComments.size() == 0)
    <p>
        No comments available to display
    </p>
    #else
    
    #if($sortedComments.size() < $topCount)
      #set ($topCount = $sortedComments.size())
    #end    
    <ol>
        #foreach($comment in $sortedComments.subList(0, $topCount))
        <li>
            <a href="$comment.webuiLink" target="_blank" style="display: inline-block;">
                <span>$comment.commentText</span>
            </a>
            <span class="aui-badge aui-badge-primary">$comment.likeCount</span>
        </li>
        #end
    </ol>
    #end