User:Jon Harald Søby/linkItemsInDescriptions.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Tool for linking entities that are mentioned in descriptions
 *
 * Some despcriptions, especially for properties, mention other Wikidata entities in the description.
 * This tool links those in the same way as if they used a {{Q|...}} or {{P|...}} template.
 * It also does this for P2559 (Wikidata usage instructions).
 *
 * Add it to your common.js with:

mw.loader.load( "//www.wikidata.org/w/index.php?title=User:Jon_Harald_Søby/linkItemsInDescriptions.js&action=raw&ctype=text/javascript" );

 */
mw.hook("wikibase.entityPage.entityView.rendered").add(function () {
	$(".wikibase-descriptionview-text, #P2559 .wb-monolingualtext-value").each( function() {
		var $this = $( this ),
	    	contenttext = $this.html(),
			newcontenttext = contenttext.replace(/([QPL])(\d+)/g, "{{$1|$1$2}}");
		if ( contenttext !== newcontenttext ) {
			new mw.Api().get({
				action: 'parse',
				text: newcontenttext,
				contentmodel: 'wikitext',
				prop: 'text'
			}).done( function(data) {
				$this.html( data.parse.text["*"] );
			}).fail( function(err) {
				alert(err);
			});
		}
	});
});