// <nowiki>
$(function () {
const pageName = mw.config.get('wgPageName');
const isEdit = mw.config.get('wgAction') === 'edit';
const useCleaner = new URLSearchParams(window.location.search).get('useAICleaner') === '1';
if (!isEdit) {
mw.util.addPortletLink(
'p-cactions',
mw.util.getUrl(pageName, { action: 'edit', useAICleaner: '1' }),
'AI cleaner',
'ca-aicleaner',
'Open editor and clean AI-style formatting'
);
}
if (!isEdit || !useCleaner) return;
const editSummaryText = "Removed inappropriate formatting - [[User:Sophisticatedevening/AI Cleaner.js|AI-Cleaner]]";
function toSentenceCaseHeading(headingText) {
return headingText.replace(/^([^\[]*)(.*)$/, (_, plain, rest) => {
const trimmed = plain.trim();
if (trimmed.length === 0) return headingText;
const firstChar = trimmed[0].toUpperCase();
const restText = trimmed.slice(1).toLowerCase();
return firstChar + restText + rest;
});
}
function cleanText(text) {
let wikiBoldCount = 0;
text = text.replace(/'''(.*?)'''/g, (match, content) => {
if (wikiBoldCount === 0) {
wikiBoldCount++;
return match;
}
return content;
});
text = text.replace(/\*\*(\S(?:.*?\S)?)\*\*/g, '$1');
text = text.replace(/(^|[^\*])\*(\S(?:.*?\S)?)\*([^\*]|$)/g, '$1$2$3');
// Sentence-case headings
text = text.replace(/^(={1,6})\s*(.+?)\s*\1/gm, (match, equals, heading) => {
const processed = heading.replace(/(\[\[.*?\]\]|{{.*?}}|<.*?>|''.*?''|[A-Z][a-z]+\s[A-Z][a-z]+)/g, (preserve) => `%%%${preserve}%%%`);
let lower = processed.toLowerCase();
lower = lower.charAt(0).toUpperCase() + lower.slice(1);
const restored = lower.replace(/%%%([^%]+)%%%/g, (_, p1) => p1);
return `${equals} ${restored.trim()} ${equals}`;
});
return text;
}
function handleClassic() {
const attemptClean = () => {
const $ta = $('#wpTextbox1');
if ($ta.length && $ta.val().length > 0) {
const orig = $ta.val();
const cleaned = cleanText(orig);
if (cleaned !== orig) {
$ta.val(cleaned);
}
$('#wpSummary').val(editSummaryText);
return true;
}
return false;
};
const rafLoop = () => {
if (!attemptClean()) {
requestAnimationFrame(rafLoop);
}
};
rafLoop();
}
function handleVE() {
mw.hook('ve.activationComplete').add(() => {
const surface = ve.init.target.getSurface();
const model = surface.getModel();
const doc = model.getDocument();
const orig = doc.getText();
const cleaned = cleanText(orig);
if (cleaned !== orig) {
model.setLinearContentFromText(cleaned);
}
const $summary = $('#wpSummary');
if ($summary.length) {
$summary.val(editSummaryText);
}
});
}
if ($('#wpTextbox1').length) {
handleClassic();
} else {
handleVE();
}
});
// </nowiki>