Last active
May 12, 2018 16:26
-
-
Save RobRuana/04b7900dd640965b92b76496e5d70aa9 to your computer and use it in GitHub Desktop.
Automatically link Jira Issues From Confluence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript"> | |
// Converts text like OFFICE-123 on a Confluence page into links to the | |
// referenced Jira issue. If the issue key is _already_ a link, it will | |
// be ignored. | |
// | |
// Add this whole snippet "At the end of HEAD" in the "Custom HTML" form here: | |
// https://docs.mycompany.com/admin/viewcustomhtml.action | |
AJS.toInit(function() { | |
var autolinkify = function(regex, replacement) { | |
// Select nodes that are neither links nor children of links. | |
AJS.$(':not(a *)').not('a').contents().each(function() { | |
// Skip non-text nodes, and nodes that do not match our regex. | |
if(this.nodeType != 3 || !regex.test(this.nodeValue)) { | |
return; | |
} | |
AJS.$(this).before(this.nodeValue.replace(regex, replacement)).remove(); | |
}); | |
}; | |
// List of Jira project slugs. You could also use a regex like [A-Z], but | |
// that may capture text that is not actually a Jira link, like "HAL-9000". | |
var jiraProjects = ['OFFICE', 'DEV', 'SYSADMIN', 'WEB']; | |
var jiraIssueRegex = new RegExp('(https:\/\/jira\.mycompany\.com\/browse\/)?(' + jiraProjects.join('|') + ')(-\\d+)', 'gi'); | |
var jiraLinkReplacement = '<a href="https://jira.mycompany.com/browse/$2$3">$&</a>'; | |
autolinkify(jiraIssueRegex, jiraLinkReplacement); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment