User:V111P/js/Smart Linking
File:Interpage icon.png | |
File:SmartLinking - disambig page.png | |
Description | Helps linking to the correct articles by letting you preview them while editing |
---|---|
Author | V111P |
Status | Beta |
Updated | May 2, 2014 |
Browser support | All browsers |
Skin support | All skins |
Source | See below |
Smart Linking is a tool for linking articles and previewing the linked pages while source-editing Wikipedia/MediaWiki articles. (The visual editor is not supported.) Using it helps prevent linking to the wrong articles, including to redirecting or disambiguation pages.
The WikEd gadget is supported only partially at this time - you have to select the link text (instead of just placing the cursor within it) and if an article with that name exists Smart Linking will display it, but won't fix redirects for you in the wiki code.
You can edit this page. If I don't like or understand something you wrote, I will correct it. ;)
How to use
To use Smart Linking (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. (See the section Selecting and focusing wiki links - Examples below for a clearer explanation). 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. See Customizing below.
- 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 (File:WMF-Agora-Settings BCBCBC.svg) disappears
Menus/Icons
- (<<) - Back to the previous article
- File:Inkscape icons window new.svg - Open the article in a new window/tab
- File:WG.Icon.edit.png - Edit the article/section in a new window/tab
- File:Plan.png - Scroll to the table of contents at the bottom. The same icon at the ToC means: Show/hide the section links.
- File:OSM relation.png - Scroll to the table of contents at the bottom, hide the section links from it
- File:Icon Arrow Up 26x26.png - Scroll back to the top
- File:Approve icon.svg - Return the focus to the textarea
- File:WMF-Agora-Settings BCBCBC.svg - Open a menu with several other icons
- File:Blue Question.svg - Open this help page in a new window/tab
- File:Button hover.svg - Close the display
- File:Symbol opinion vote.svg - 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/smartLinkingLoader.js]]
}
The wiki link is there so that your common.js file appears in the "What links here" list of that page. This way we can see how many people have installed 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.
In other Wikipedias
To try Smart Linking in other Wikipedias, you can use the code below.
if ( $.inArray( mw.config.get( 'wgAction' ), ['edit', 'submit'] ) > -1 ) {
mw.loader.load('//en.wikipedia.org/w/index.php?title=User:V111P/js/smartLinkingLoader.js'
+ '&action=raw&ctype=text/javascript');
}
However, Smart Linking won't be able to detect your other-meaning and main-article templates, so you're losing some important features. That's why you need to set up a configuration file and load it instead of directly loading smartLinkingLoader.js as above. See the template configuration file for more information, read all the comments there and don't hesitate to ask if you need help. Let me know if you set Smart Linking up for another Wikipedia, so I can mention it here.
Customizing
To configure Smart Linking's display, in your common.js or equivalent file add these lines first:
$.extend(window.smartLinkingConfig = window.smartLinkingConfig || {}, {
});
Then you can set properties by inserting them between the first and the last line of that code. To set a property, use this as a template: propertyName: value,
where propertyName
is one of the property names listed below, and value
is an appropriate value for that property. Values which are not numbers or the keywords true and false need to be enclosed in either single or double quotes, as you can see in the example. All these lines must end with a comma, except the last one, right before the closing });
line (where a comma will actually cause an error in very old versions of Internet Explorer).
Here is an example setting several properties:
$.extend(window.smartLinkingConfig = window.smartLinkingConfig || {}, {
blurChar: '!',
height: '3em',
expandHeight: '20em',
background: 'cyan',
insertRelation: 'after'
});
Here is a list of the possible properties:
closeChar
- a character which, when typed while the keyboard focus is on the display, causes the display to disappear and the keyboard focus to be returned to the textarea. The default is ` (grave accent).expandCollapseChar
- a character which, when typed while the keyboard focus is on the display, causes the display to expand if it is not already expanded, otherwise it collapses and return the keyboard focus to the textarea. The default is !blurChar
- a character which, when typed while the keyboard focus is on the display, causes the display to collapse and returns the keyboard focus to the textarea. The default is ^height
- the initial height of the display. Can be just a number (which means it's in pixels) or a string with a number and one of the CSS units. The default value is'1.4em'
. If you set this, set alsoexpandHeight
to the same or a larger value.minHeight
- height in number of pixels (no CSS units allowed). You can't resize the display with the mouse to be shorter in height than that. The default is whatever value you give toheight
. If you set this, try to set it to less than or equal to the value you give toheight
. If you don't set this, you can't make the display shorter than its initial height.expandHeight
- the height to which the display will be expanded in certain cases, such as on double click or when opening a disambiguation page. Set this to greater than or equal toheight
. If you don't want the display to expand automatically on double click, or when clicking certain links in it, or on disambiguation pages, setexpandHeight
to the same value asheight
.background
- set the background with a CSS value (which can be a color or an image url). The default is'#fed'
. An example value with an image:'url(//upload.wikimedia.org/wikipedia/commons/0/0f/Wiki_background.jpg)'
. The image needs to be very light in color, or the text won't be readable.insertRelToSelector
(advanced) - a jQuery selector for the element before, after or within which the display is to be inserted. The default is'#wpTextbox1'
(the textarea).insertRelation
- one of'before', 'after', 'prepend', 'append'
with meanings as in jQuery (for example'prepend'
means insert as a first element of the element selected withinsertDisplayRelToSelector
). The default is'before'
. Try using'after'
without settinginsertDisplayRelToSelector
('prepend'
or'append'
don't make sense for a textarea and won't work though).addButton
- set this tofalse
(without quotes) to disable adding the Smart Linking button to the toolbar. Then, to use Smart Linking, you'll need to have some other way to callmw.libs.smartLinkingLoader.smartLinking()
allPossibleOtherMeaningTemplates
- set this totrue
(without quotes) to cause all templates which appear in the wiki code on a line by themselves near the top of the article or section to be considered other-meaning or main-article templates. This is only useful while testing to find templates which need to be included in the configuration file for that wiki.
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
- 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, or addToolbarButtons.js, you can test it by (1) setting the main object to undefined or null (e.g. for msgDisplay.js: mediaWiki.libs.msgDisplay = null;
), (2) executing the code from that file in the JavaScript console of your browser, then (3) executing the code from smartLinking.js, and finally (4) pressing the Smart Linking toolbar button. You don't need step (3) 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.