User:Nardog/SmartDiff

From Wikipedia, the free encyclopedia
SmartDiff
DescriptionMakes links and template calls in diffs clickable (supports red links, avoids WP:SEAOFBLUE)
UpdatedFebruary 26, 2024
    (58 days ago)
Browser supportChrome, Firefox, etc. (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[edit]

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[edit]

Template parameter linking[edit]

You can instruct the script to link certain unnamed parameters in templates, so e.g. {{tl|reflist}} appears as {{tl|reflist}}. This is useful for linking templates like {{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[edit]

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[edit]

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. {{tl|Wikipedia:Cleanup/Header}} 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[edit]

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[edit]

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

  • Shows links to nonexistent pages in red.
  • Links only [[this part]] and [[this part|in piped links]], avoiding WP:SEAOFBLUE.
  • Links section headings (e.g. == Customization ==).
  • Recognizes template names without }} in the same line (e.g. {{Infobox person).
  • Recognizes relative links to and transclusions of subpages (e.g. [[/Archive 1]]).
  • Does not mistake common magic words and template parameters ({{PAGENAME}}, {{#if:...}}, {{{1}}} etc.) for template calls.
  • Supports customizable linking of unnamed parameters in templates (e.g. {{tl|Cite web}}, {{distinguish|Foo}})—see above.
  • Links content inside tvar in multilingual wikis, as in [[<tvar name=1>Foo</tvar>|Foo]].
  • Shows external and interwiki links in the same color as in the page body.
  • Adds to links to redirects and disambiguation pages the respective mw-redirect and mw-disambig classes, so if you style them in your CSS (or use the gadget "Display links to disambiguation pages in orange"), they appear the same as in the page body.
  • Verifies the result after processing each line, so it will never alter the substance of a diff even if it makes a mistake (and notifies you in such an event so you can report it).
  • Does not support image previews.

Limitations[edit]

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