Jump to content

User:Nardog/AutoSectionLink.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nardog (talk | contribs) at 10:29, 22 April 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
['edit', 'submit'].includes(mw.config.get('wgAction')) &&
mw.config.get('wgPageContentModel') === 'wikitext' &&
$.when($.ready, mw.loader.using([
	'jquery.textSelection', 'oojs-ui-core', 'oojs-ui.styles.icons-editing-core'
])).then(function AutoSectionLink() {
	let input = document.getElementById('wpSummary');
	if (!input) return;
	let prev;
	let button = new OO.ui.ButtonWidget({
		framed: false,
		icon: 'undo',
		title: 'Restore previous section link'
	}).toggle().on('click', () => {
		input.value = input.value.replace(
			/^(\/\*.*?\*\/)?\s*/,
			prev ? '/* ' + prev + ' */ ' : ''
		);
		button.toggle(false);
	}).on('toggle', visible => {
		input.parentElement.style.width = visible
			? `calc(100% - ${button.$element.width()}px)`
			: '';
	});
	button.$element.css({ position: 'absolute', top: 0, right: 0, margin: 0 })
		.insertAfter('#wpSummaryWidget')
		.parent().css('position', 'relative');
	mw.hook('wikipage.diff').add($diff => {
		let i, lineNums = [];
		$diff[0].querySelectorAll('td:last-child').forEach(td => {
			switch (td.className) {
				case 'diff-lineno':
					i = td.innerText.replace(/\D+/g, '') - 1;
					break;
				case 'diff-context':
					i++;
					break;
				case 'diff-addedline':
					i++;
					if (!lineNums[0]) lineNums[0] = i;
					lineNums[1] = i;
					break;
				case 'diff-empty':
					if (!lineNums[0]) {
						if (i === 0) {
							lineNums[0] = 1;
						} else {
							lineNums[0] = i;
						}
					}
					lineNums[1] = i;
			}
		});
		if (!lineNums[0]) return;
		let text = $('#wpTextbox1').textSelection('getContents');
		let chunks = [];
		chunks[0] = text.match(new RegExp(`(?:^.*\n?){${lineNums[0]}}`, 'm'))[0];
		chunks[1] = lineNums[1] > lineNums[0]
			? text
				.slice(chunks[0].length)
				.match(new RegExp(`(?:^.*\n?){${lineNums[1] - lineNums[0]}}`, 'm'))[0]
			: '';
		let pat = /^(={1,6})\s*(.+?)\s*\1\s*(?:<!--.+-->\s*)?$/gm;
		let head = '', match, curLevel, lowest = 6;
		while ((match = pat.exec(chunks[1]))) {
			curLevel = match[1].length;
			if (curLevel < lowest) lowest = curLevel;
		}
		while ((match = pat.exec(chunks[0]))) {
			if (match[1].length < lowest) head = match[2];
		}
		// https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/master/includes/parser/Sanitizer.php
		head = head
			.replace(/'''(.+)'''|\[\[:?(?:[^\|\]]+\|)?([^\]]+)\]\]|<\/?(?:abbr|b|bdi|bdo|big|cite|code|data|del|dfn|em|font|i|ins|kbd|mark|nowiki|q|rb|ref|rp|rt|rtc|ruby|s|samp|small|span|strike|strong|sub|sup|templatestyles|time|tt|u|var)(?:\s[^[^>]*)?>|<!--.*?-->|\[(?:https?:)?\/\/[^\s\[\]]+\s([^\]]+)\]/gi, '$1$2$3')
			.replace(/''(.+)''/g, '$1')
			.trim();
		match = input.value.match(/^(?:\/\*\s*(.*?)\s*\*\/)?\s*(.*?)$/);
		if (!match[1]) match[1] = '';
		if (match[1] === head) return;
		input.value = (head ? '/* ' + head + ' */ ' : '') + match[2];
		prev = match[1];
		button.toggle(true);
	});
});