April 20, 2021

Confluence: Share good links

There is one pitfall to know when sharing Confluence page links to have links robust to page renaming. Secondly how cool would it be to be able to paste a Confluence page url and have it automatically converted into a nice-looking rich-text link including the page title and related space information? I present here a solution based on AutoHotkey implemented in the NWS PowerTool IntelliPaste feature.

How to share a Confluence page?

You have 3 types of links in Confluence:
  • Page Title aka Pretty Format: the space and page title is contained in the url
    • https://confluence.example.com/display/SPACEKEY/Page+Title
  • Link by PageId
    • https://confluence.example.com/pages/viewpage.action?pageId=123456
  • Tiny link: you can get it from the Share button (see screenshot below)
    • https://confluence.example.com/x/a4OcE

If you use the Page Title aka Pretty Format link, the link won't work immediately in case you rename the page. Confluence will be intelligent enough to propose to you the most likely new page location but it isn't a direct redirection (like in HCL Connections Wikis for example.)


For this reason, I always recommend not to use the pretty link format when sharing Confluence page links.

It seems not possible to configure how URLs are displayed - see Change Confluence URL Default to use Page ID

Code / AutoHotkey implementation

I have implemented my AutoHotkey PowerTool such a feature that will convert a pretty link (not robust to renaming) to a link by page Id.
It uses a call to the Confluence API and parses the response with some RegExMatch to extract the pageId, spaceName and pageTitle information.

Code is available in ahk/Lib/Confluence.ahk -> Confluence_CleanLink() function

Confluence_CleanLink(sUrl){
; link := Confluence_CleanLink(sUrl)
; link[1]: Link url
; link[2]: Link display text
; Paste link with Clip_PasteLink(link[1],link[2])
; Works for page link by title or pageId or tinyUrl

sResponse := Confluence_Get(sUrl)

sPat = s)<meta name="ajs-page-title" content="([^"]*)">.*<meta name="ajs-space-name" content="([^"]*)">.*<meta name="ajs-page-id" content="([^"]*)">
RegExMatch(sResponse, sPat, sMatch)
sLinkText := sMatch1 " | " sMatch2 " - Confluence"
sLinkText := StrReplace(sLinkText,"&amp;","&")
RegExMatch(sUrl, "https://[^/]*", sRootUrl)
sUrl := sRootUrl "/pages/viewpage.action?pageId=" sMatch3
;MsgBox %sLinkText% %sUrl%
return [sUrl, sLinkText]
}

The connection to Confluence via API (Confluence_Get) requires that you have set your ConfluenceUserName (or JiraUserName) in the PowerTools Settings.

NWS PowerTool integration - IntelliPaste

The IntelliPaste feature of the NWS PowerTool makes use of this feature.
It will automatically convert a pretty Confluence URL into a Link by page Id.

Moreover the display text is also intelligent and based like this <PageName> | <SpaceName> - Confluence.
You can still edit it before inserting the nice link.

See also

IntelliPaste - PowerTools

Change Confluence URL Default to use Page ID

Why don't the normal URL links keep working when p...

[CONFSERVER-11285] Page names with special characters to generate regular URL

[CONFSERVER-45898] URLs should be based on pageID instead of pageTitle

No comments:

Post a Comment