The DreamerThe Beastie Blog

GitHub Pages Plugin to linkify PR: references?

Sunday, April 27 2014

After I had written many posts with “PR:” lines everywhere with the internal identification string assigned to my problem report submission. It occurred to me that it might be nice if it turned into the link to PR itself.

I looked to see if there might be a way to extend one of the markdown formats to do this automagically, except I immediately realized that I’m kind of limited to what ‘github-pages’ allows.

So, I decided the solution was to create some javascript code to do this.

This is what I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<script type="text/javascript">
function addEvent(evType, f) {
  if (window.addEventListener){
    window.addEventListener(evType, f, false);
    return true;
  } else if (window.attachEvent){
    var r = window.attachEvent("on"+evType, f);
    return r;
  } else {
    return false;
  }
}
addEvent('load', function() {
  var pElements = document.getElementsByClassName('body');

  var pElement = pElements.item(0);
  var pHText = pElement.innerHTML;
  var myRegExp = /PR: ([^\/]+\/\d+)/;
  var myMatch = myRegExp.exec(pHText);

  if (myMatch) {
    var myReplace = "PR: <a href=\"http://www.freebsd.org/cgi/query-pr.cgi?pr=" + myMatch[1] + "\" target=\"_blank\">" + myMatch[1] + "</a>";

    var pReplaced = pHText.replace(myRegExp, myReplace);
    pElement.innerHTML = pReplaced;
  }
});
</script>

Enjoy! :monkey_face: