User:V111P/js/Smart Linking

Smart Linking is a tool for linking articles and previewing the linked pages while source-editing Wikipedia/MediaWiki articles. (The visual editor and the gadget WikEd are not supported.)

To use it (after installing it), position the text cursor anywhere within or just after the second closing bracket of a link and press the Smart Linking button, or select an existing link (with or without its square brackets) or select unlinked text you want to make a link from, and press the Smart Linking button. Smart Linking's display with the linked article's contents (or just the title of the page in red, if there is no such page yet) will appear above the textarea (tables, images, and templates are removed from the articles).


You can learn how to use most of the features by hovering with the mouse pointer over the various icons and links and reading the tooltips. Some less obvious things you need to know are:
- The links within Smart Linking's display can lead you to other articles, but keep in mind that visiting other articles will change the address of the link in the textarea where you last left the cursor before pressing the Smart Linking button. You can use the back link (<<) to get back to the article you want to link to.
- You can resize the display to make it display more than a single line at a time in two ways:
- temporarily - by double clicking anywhere within the display or by dragging the bottom edge of the display (The second way doesn't always work in Internet Explorer). This way of resizing hides part of the textarea under the display. Double clicking the display while it is temporarily expanded collapses it back and returns focus to the textarea.
- semi-permanently (until you leave the page) - by dragging the resize handle on the right of the display. This method moves the textarea down.
- You can set the size of the display by editing your common.js file. How to do this will be explained later.
- Sometimes after you click the Smart Linking button the keyboard focus is left within the display (For example after a redirect the focus in on the Back link to allow for an easy cancellation of the redirecting). You can press Esc or ` (grave accent) to return the focus to the textarea (Escape won't work in Google Chrome).
- Keyboard navigation within the Smart Linking display is possible - use Tab, Shift+Tab, Enter. With Esc or ` you can return to the textarea. With ^ or Shift+Esc you can remove the Smart Linking's display, ! acts like double click (described above).
- Internet Explorer - There are several problems that I noticed with (version 10 of) this browser:
- Usually when you click a link you will notice the entire page flashing (scrolling to a different position and back again).
- Sometimes you can't resize the display by dragging the bottom edge - the handle is not there (sometimes you can find it only at the right side of the display).
- The menu at the bottom right (
) disappears
More documentation may appear here soon.
Menus/Icons


- (<<) - Back to the previous article
- Open the article in a new window/tab
- Edit the article/section in a new window/tab
- Scroll to the table of contents at the bottom. The same icon at the ToC means: Show/hide the section links.
- Scroll to the table of contents at the bottom, hide the section links from it
- Scroll back to the top
- Return the focus to the textarea
- Open a menu with several other icons
- Open this help page in a new window/tab
- Close the display
- Attention! (This image is not a button, it signals a non-existing page, a disambiguation page, or an error.)
Selecting and focusing wiki links - Examples
In the following cases a valid already-existing wiki link is focused (the caret is after the first opening square bracket and at the latest right after the second closing square bracket, the caret's position here is marked with a vertical bar):
[|[Wikipedia]]
,
[[|Wikipedia]]
,
[[Wiki|pedia]]
,
[[Wikipedia|]]
,
[[Wikipedia]|]
,
[[Wikipedia]]|
.
In the following cases no valid link is selected or focused (the caret's position here is marked with a vertical bar):
Wikipedia|
(the caret is not within a wiki link or right after a wiki link)|[[Wikipedia]]
(the caret is before the wiki link)[[Wikipedia]] |
(the caret is not right after the wiki link, there is an interval between them)[[2 > 1]]|
(invalid article title)
In the following cases a valid link is selected:
[[Wikipedia]]
,
[[Wikipedia]]
,
[[Wikipedia]]
,
[[Wikipedia]]
,
[[Wikipedia]]
,
[[Wikipedia]]
,
[[Wikipedia]]
,
[[Wikipedia]]
.
If you are using the gadget Navigation Popups, selecting wiki links with all four of the square brackets ([[Wikipedia]]) will cause Navigation Popups to load the article. You can disable this behavior by adding var popupOnEditSelection = false;
to your common.js or equivalent file.
In the following cases the selected text will be turned into a wiki link:
English Wikipedia
,
English Wikipedia
,
English Wikipedia
,
English Wikipedia
.
The spaces around the text won't be included in the link, so it's not a problem if they are also selected.
In the following case, the selected text won't be turned into a wiki link: 2 > 1
(invalid article title).
Installation
For the English Wikipedia, add this code to your common.js (or vecor.js, or monobook.js, whichever you are using) file:
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) > -1 ) {
importScript('User:V111P/js/smartLinkingLoader.js'); // [[User:V111P/js/Smart Linking]]
}
The wiki link is there so that your common.js file appears in the "What links here" list of this page. This way we can see how many people are using the script.
You should move all your JavaScript code that you need only when editing wiki pages before the last closing } in the code above. That way it won't get loaded and executed on every page.
To try Smart Linking in other Wikipedias, you can use the code below. However, Smart Linking won't be able to detect your other-meaning and main-article templates, so you're losing some features. I'll explain how to fix that later.
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) > -1 ) {
$.ajax({
url: '//en.wikipedia.org/w/index.php?title=User:V111P/js/smartLinkingLoader.js'
+ '&action=raw&ctype=text/javascript',
dataType: 'script', cache: true
});
}
For developers
If you would like to experiment and improve Smart Linking, I am ready to explain any and all parts of it - just ask.
Smart Linking is using several main components:
- smartLinking.js is the main file
- wikiParserV.js is a collection of useful functions
- msgDisplay.js is the script that deals with Smart Linking's display
- valSel.js is the script which provides an easy cross-browser methods for manipulating the textarea content
- addToolbarButtons.js is a short script which adds the toolbar button
- smartLinkingLoader.js is a short script which loads addToolbarButton.js and adds the button. By loading this file, instead of directly smartLinking.js, the user doesn't have to download the entire program before actually pressing the Smart Linking button for the first time. Also, I control the caching of smartLinking.js within smartLinkingLoader.js. Without control, scripts are cached for 30 days I think, which is too long for a beta version of an application. Alternatively though, you could load smartLinking.js directly and then call
window.smartLinking()
any way you like (no button will be added to the toolbar).
If you are experimenting and make a change in one or more of the files msgDisplay.js, wikiParserV.js, valSel.js, or addToolbarButtons.js, you can see the result by (1) executing the code from that file in the JavaScript console of your browser, then (2) executing the code from smartLinking.js, and finally (3) pressing the Smart Linking toolbar button. You don't need step (2) if you haven't already clicked Smart Linking's toolbar button since opening the page for editing. If you make a change only in smartLinking.js, than just execute that script.
valSel.js and addToolbarButtons.js are completely documented and ready for use in other applications.
An acknowledgment: valSel.js includes code from Rangy Inputs, a jQuery plug-in for selection and caret manipulation within textareas and text inputs, Copyright 2010-2013, Tim Down, Licensed under the MIT license.