Участник:Stjn/wikifyDisambig.js

Материал из Википедии — свободной энциклопедии
Страница персонального оформления. У этого JS-кода есть документация: Участник:Stjn/wikifyDisambig.
После сохранения очистите кэш браузера.
// <nowiki>
//
// Remove wikilinks from disambig text
// Works only as a plugin to wikificator
//
( () => {
	if ( [ 'edit', 'submit' ].includes( mw.config.get( 'wgAction' ) ) === false ) return;
	if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 ) return;
	
	var text = document.querySelector( '#wpTextbox1' ).value.toLowerCase();
	var templateNames = [
		'неоднозначность',
		'disambig',
		'нисба',
		'одноимённые воинские части',
		'омонимы систематиков',
		'список однофамильцев',
		'список однофамильцев-тёзок',
		'список полных тёзок',
		'список тёзок',
	]
	
	if ( !templateNames.some( t => text.includes( `{{${ t }` ) ) ) return;
	
	// Set window.wikifyDisambigSummary = null in your common.js to disable
	window.wikifyDisambigSummary = window.wikifyDisambigSummary || '[[user:stjn/wikifyDisambig|оформление страницы значений]]';
	var userSummary = window.wikifyDisambigSummary;
	
	// Set window.wikifyDisambigSubst = true to enable substitution
	window.wikifyDisambigSubst = window.wikifyDisambigSubst || false;
	
	function wikifyDisambig( txt, r ) {
		// Delink all links after —
		r( /\[\[.*?\]\].*?—\s*(.*)/gm, ( str, m1 ) => {
			var result = m1.replace( /\[\[([^\|\]]*?)\]\]/g, '$1' )
				.replace( /\[\[([^\|\]]*?)\|(.*?)\]\]/g, '$2' );
			return str.replace( m1, result );
		} );
		
		// Clean up template
		r( /\{\{однофамильцы\|\s*(.*?) \(значения\)\s*{{!}}\s*\1\}\}/gi, '{{однофамильцы|$1 (значения)}}' );
		
		// Substitute template names
		if ( window.wikifyDisambigSubst ) {
			var substRegExp = new RegExp( `{{(${ templateNames.slice( 1, templateNames.length ).join( '|' ) })`, 'gi' );
			r( substRegExp, '{{подст:$1' );
		}
		
		// Switch D- to D-l
		r( /\{\{([Dd]\-)\s*\|\s*\[\[([^\]]*?)\]\]}}/g, '{{$1l|$2}}' );
		
		// Fix up bold text
		r( /^'''(.*?):'''/gm, `'''$1''':` );
		
		// Remove noinclude
		var changedNoinclude = false;
		r( /([^\n])<noinclude>{{/gi, '$1\n<noinclude>{{' );
		r( /<noinclude>(.*?)<\/noinclude>/gis, ( str, m1 ) => {
			changedNoinclude = true;
			return m1;
		} );
		
		// Replace noinclude with a guess about onlyinclude
		if ( changedNoinclude ) {
			r( /(\*\s*)(.*?)(\n+\{\{)/gs, ( str, m1, m2, m3 ) => {
				return `<onlyinclude>\n${ m1 }${ m2 }\n</onlyinclude>${ m3 }`;
			} );
		} else {
			// Fix <onlyinclude> to be on separate lines in obvious cases
			r( /:<onlyinclude>/i, ':\n<onlyinclude>' );
			r( /<onlyinclude>\* /i, '<onlyinclude>\n* ' );
			r( /\.<\/onlyinclude>/i, '.\n</onlyinclude>' );
			r( /<\/onlyinclude>{{/i, '<\/onlyinclude>\n{{' );
			r( /(==+)<onlyinclude>/gi, '$1\n<onlyinclude>' );
		}
		
		r( /<\/onlyinclude>\n+{{/, '<\/onlyinclude>\n{{' );

		// Fix list breaks
		r( /\*\s*(.*?)\n\n\s*\*\s*/g, '* $1\n* ' );
		
		var summary = document.querySelector( '#wpSummary' );
		if ( userSummary && !summary.value.includes( userSummary ) ) {
			summary.value = summary.value.length > 0 ? `${ summary.value }, ${ userSummary }` : userSummary;
		}
	}
	
	window.wfPluginsT = window.wfPluginsT || [];
	window.wfPluginsT.push( wikifyDisambig );
} )();
// </nowiki>