Jump to content

MediaWiki:ShortDescription.js: Difference between revisions

Show the short description under the article title
 
A little more space under the short description
 
Line 22: Line 22:
var css = document.createElement( 'style' );
var css = document.createElement( 'style' );
css.textContent = '.cp-shortdesc{display:block;font-family:sans-serif;font-size:14px;line-height:1.4;' +
css.textContent = '.cp-shortdesc{display:block;font-family:sans-serif;font-size:14px;line-height:1.4;' +
'font-weight:normal;color:#54595d;margin:4px 0 0;}' +
'font-weight:normal;color:#54595d;margin:4px 0 0;padding-bottom:6px;}' +
'.cp-shortdesc-mobile{margin:-4px 0 8px;}';
'.cp-shortdesc-mobile{margin:-4px 0 8px;}';
document.head.appendChild( css );
document.head.appendChild( css );

Latest revision as of 18:07, 21 November 2026

/* Short description under the article title (like Wikipedia's mobile site).
 * Shows the text of {{Short description|...}} when the article has one. Loaded from Common.js and Mobile.js. */
( function () {
	if ( mw.config.get( 'wgNamespaceNumber' ) !== 0 || mw.config.get( 'wgAction' ) !== 'view' ||
		mw.config.get( 'wgIsMainPage' ) || !mw.config.get( 'wgIsArticle' ) ) {
		return;
	}
	$.getJSON( mw.config.get( 'wgScriptPath' ) + '/api.php', {
		action: 'query', prop: 'revisions', rvprop: 'content', rvslots: 'main', rvsection: 0,
		pageids: mw.config.get( 'wgArticleId' ), format: 'json', formatversion: 2
	} ).then( function ( d ) {
		var page = d.query && d.query.pages && d.query.pages[ 0 ];
		var text = page && page.revisions && page.revisions[ 0 ].slots.main.content;
		var m = text && text.match( /\{\{\s*[Ss]hort description\s*\|\s*([^|}]+)/ );
		if ( !m ) {
			return;
		}
		var desc = m[ 1 ].trim();
		if ( !desc || desc.toLowerCase() === 'none' ) {
			return;
		}
		var css = document.createElement( 'style' );
		css.textContent = '.cp-shortdesc{display:block;font-family:sans-serif;font-size:14px;line-height:1.4;' +
			'font-weight:normal;color:#54595d;margin:4px 0 0;padding-bottom:6px;}' +
			'.cp-shortdesc-mobile{margin:-4px 0 8px;}';
		document.head.appendChild( css );
		var el = document.createElement( 'div' );
		el.className = 'cp-shortdesc';
		el.textContent = desc;
		var h = document.getElementById( 'firstHeading' );
		if ( document.querySelector( '.vector-page-titlebar' ) && h ) {
			h.appendChild( el );
			return;
		}
		var heading = document.querySelector( '.page-heading' ) || h;
		if ( heading ) {
			el.className += ' cp-shortdesc-mobile';
			heading.parentNode.insertBefore( el, heading.nextSibling );
		}
	} );
}() );