Skip to content

Instantly share code, notes, and snippets.

@bradyvercher
Created October 25, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradyvercher/7157369 to your computer and use it in GitHub Desktop.
Save bradyvercher/7157369 to your computer and use it in GitHub Desktop.
A bookmarklet to convert [#####] text nodes on a page to links pointing to the corresponding changeset on WordPress Trac.

Bookmarklet

(function e(t){var n=/\[([0-9]{5})\]/m;if(t.childNodes.length>0){for(var r=0;r<t.childNodes.length;r++){e(t.childNodes[r])}}if(3===t.nodeType&&n.test(t.nodeValue)){var i=t.nodeValue.match(n).pop(),s=document.createElement("a");s.appendChild(document.createTextNode(i));s.setAttribute("href","http://core.trac.wordpress.org/changeset/"+i+"/trunk");t.parentNode.insertBefore(s,t.nextSibling);t.parentNode.insertBefore(document.createTextNode(" ["),t.nextSibling);t.parentNode.insertBefore(document.createTextNode("]"),s.nextSibling);t.nodeValue=t.nodeValue.replace(n,"")}})(document.body)

Source

( function traverse( node ) {
	var regexPattern = /\[([0-9]{5})\]/m;

    if ( node.childNodes.length > 0 ) {
        for ( var i = 0; i < node.childNodes.length; i ++ ) {
            traverse( node.childNodes[ i ] );
		}
	}

    if ( 3 === node.nodeType && regexPattern.test( node.nodeValue ) ) {
        var revision = node.nodeValue.match( regexPattern ).pop(),
        	link = document.createElement( 'a' );

		link.appendChild( document.createTextNode( revision ) );
		link.setAttribute( 'href', 'http://core.trac.wordpress.org/changeset/' + revision + '/trunk' );
		
		node.parentNode.insertBefore( link, node.nextSibling );
		node.parentNode.insertBefore( document.createTextNode( ' [' ), node.nextSibling );
		node.parentNode.insertBefore( document.createTextNode( ']' ), link.nextSibling );
		node.nodeValue = node.nodeValue.replace( regexPattern, '' );
	}
} )( document.body );

Test

Run the bookmarklet on this page to convert [#####] strings to links pointing to the corresponding changeset on Trac.

http://codex.wordpress.org/Version_3.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment