User:Nardog/SmartDiff

Source: Wikipedia, the free encyclopedia.
SmartDiff
DescriptionMakes links and template calls in diffs clickable (supports red links, avoids
ES2018+ needed)
SourceUser:Nardog/SmartDiff.js

This script turns wikilinks, external links, and template/module calls in a diff into clickable links. It shows links to nonexistent pages in red, and keeps the linked part to a minimum to avoid

WP:SEAOFBLUE, unlike fancy-diffs (further comparison
).

Installation

Add the following to your common.js or global.js:

mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Nardog/SmartDiff.js&action=raw&ctype=text/javascript');

Or you may enable the script installer in Preferences → Gadgets and click "Install" in the infobox on this page.

Customization

Template parameter linking

You can instruct the script to link certain unnamed parameters in templates, so e.g. {{tl|reflist}} appears as {{

tl}} and hatnotes like {{About
}}. Linking of named parameters is not supported, and the template call and parameters must be in the same line for the latter to be linked.

To do it, add code like below to your common.js (templates vary by wiki, so even if you have installed the script globally, it is recommended to have different code on each wiki).

window.smartdiffTemplates = [
	// Objects go here
];

Example

Here is an example based on templates on English Wikipedia.

Example code
window.smartdiffTemplates = [

	// {{t}}, {{tl}}
	{
		names: ['T', 'Tl'],
		end: 1,
		namespace: 10,
		forceNs: true
	},

	// {{tlx}}
	{
		names: ['Tlx'],
		end: 1,
		namespace: 10
	},

	// {{u}}
	{
		names: ['U'],
		end: 1,
		namespace: 1
	},

	// {{Reply to}}, {{Re}}, {{Ping}}
	{
		names: ['Re', 'Reply to', 'Ping'],
		namespace: 1
	},

	// {{About}}
	{
		names: ['About'],
		start: 3,
		skipEven: true
	},

	// {{For}}
	{
		names: ['For'],
		start: 2
	},

	// {{Other uses}}, {{Otheruses}}
	{
		names: ['Other uses', 'Otheruses'],
		end: 1
	},

	// {{Distinguish}}, {{Further}}, {{Main}}, {{See also}}
	{
		names: ['Distinguish', 'Further', 'Main', 'See also', 'Seealso']
	},

	// {{Redirect}}
	{
		names: ['Redirect'],
		skipEven: true,
		noRedirectEnd: 1
	},

	// {{Tracked}}
	{
		names: ['Tracked'],
		end: 1,
		prefix: 'phab:'
	},

];

Schema

The objects can have the following properties (only names is required):

names (array of strings)
Template names to apply this object to. Must be well-formed names (no Template:, no underscores, start with a capital letter except on Wiktionary). Redirects are not resolved, so include all you expect to find.
start, end (number); skipOdd, skipEven (boolean)
Specify the range of parameters to link. If omitted, all unnamed parameters will be linked.
namespace (number)
Namespace number
for the link.
forceNs (boolean)
If true, the namespace prefix will be added even if the input begins with a valid namespace prefix (so e.g. {{}} is linked correctly).
prefix, suffix (string)
String to prepend or append to the input. prefix is useful for linking to a special page or sister site.
noRedirectStart, noRedirectEnd (number)
Specify the range of parameters to suppress redirects (by adding redirect=no to the URL) in.

Magic words

If you want the script to recognize certain magic words it does not by default and stop linking them as templates, you can do so by adding code like below to your common.js or global.js. This example adds localized variables for PAGENAME and DEFAULTSORT in Polish (a trailing colon is required if the magic word accepts a variable).

window.smartdiffMagicWords = ['NAZWASTRONY', 'NAZWASTRONY:', 'SORTUJ:'];

Comparison with fancy-diffs

This script owes a great debt to Enterprisey's fancy-diffs in its concept, and makes improvements on it. Differences include:

Limitations

  • The detection/retrieval of red links and link classes is capped at 500 unique page names (10 API calls) per diff.