User:Waldyrious/whitespacefixer.js
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Fork of [[User:Omegatron/monobook.js/headingformattingfixer.js]],
// without the code to normalize headings
// (i.e. add spaces within the equal signs, and newlines after the headings)
// since it produced dirty diffs too often.
// Diff of the original fork: https://en.wikipedia.org?diff=625426859&oldid=625426743
function whitespacefixer() {
var txt = document.editform.wpTextbox1;
// 1 blank line before the headings
// (this is super-kludgy because of the newlines conflicting when one heading is right after another)
// (seems to work, though, for now. some other day...)
txt.value = txt.value.replace(/(\n+={2,}) (.*?) (={2,})/g, '\n\n$1 $2 $3');
// Remove all extra newlines?
txt.value = txt.value.replace(/(\n[ \t\v\r\f]*){2,}/g, '\n\n');
// One space after * and # lists, and after : and ;
txt.value = txt.value.replace(/(^|\n)((\*|\#|\;|\:)+)\s*(\w|\[|\"|\'|\{\{)/g, '$1$2 $4');
// Add a tag to the summary box
var editSummary = document.editform.wpSummary;
var appendToSummary = "cleanup whitespace (using [[w:en:User:Waldir/whitespacefixer.js|Regex whitespace fixer]])";
if (editSummary.value.indexOf(appendToSummary) == -1) {
if (editSummary.value.match(/[^\*\/\s][^\/\s]?\s*$/)) {
editSummary.value += "; ";
}
editSummary.value += appendToSummary;
}
}
$(function () {
if(document.forms.editform) {
mw.util.addPortletLink('p-cactions', 'javascript:whitespacefixer()', '_', 'ca-whitespacefixer', 'Fix whitespace', '-', '');
}
});