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 09:01, 18 November 2021 (disable on section editing for the time being). 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' &&
$(function autoSectionLink() {
	let section = $('[name="wpSection"]').val();
	if (section && section !== '0') return;
	let $widget = $('#wpSummaryWidget');
	if (!$widget.length) return;
	mw.loader.using([
		'mediawiki.Title', 'jquery.textSelection', 'oojs-ui-core',
		'oojs-ui.styles.icons-editing-core'
	], () => {
		let input = OO.ui.infuse($widget);
		if (!input) return;
		let button = new OO.ui.ButtonWidget({
			framed: false,
			icon: 'undo',
			title: 'Restore previous section link'
		}).toggle().on('click', () => {
			let cache = button.getData();
			input.setValue(input.getValue().replace(
				/^(\/\*.*?\*\/)?\s*/,
				cache[0] ? '/* ' + cache[0] + ' */ ' : ''
			));
			cache.reverse();
		}).on('toggle', () => {
			input.$element.css('width', `calc(100% - ${button.$element.width()}px)`);
		});
		button.$element.css({ position: 'absolute', top: 0, right: 0, margin: 0 })
			.insertAfter($widget).parent().css('position', 'relative');
		mw.hook('wikipage.diff').add($diff => {
			let i, lineNums = [];
			$diff.find('td:last-child').each(function () {
				if (this.classList.contains('diff-lineno')) {
					i = this.textContent.replace(/\D+/g, '') - 1;
				} else if (this.classList.contains('diff-context')) {
					i++;
				} else if (this.classList.contains('diff-addedline')) {
					i++;
					if (!lineNums[0]) lineNums[0] = i;
					lineNums[1] = i;
				} else if (this.classList.contains('diff-empty')) {
					if (!lineNums[0]) lineNums[0] = i === 0 ? 1 : 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.getValue().match(/^(?:\/\*\s*(.*?)\s*\*\/)?\s*(.*?)$/);
			if (!match[1]) match[1] = '';
			if (match[1] === head) return;
			input.setValue((head ? '/* ' + head + ' */ ' : '') + match[2]);
			button.setData([match[1], head]).toggle(true);
		});
	});
});