summaryrefslogtreecommitdiff
path: root/resources/mediawiki.api/mediawiki.api.parse.js
blob: e8d1b3e6cb03b1c87be2e495fe7fbc0cae9c50cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
 * mw.Api methods for parsing wikitext.
 */
( function ( mw, $ ) {

	$.extend( mw.Api.prototype, {
		/**
		 * Convinience method for 'action=parse'. Parses wikitext into HTML.
		 *
		 * @param wikiText {String}
		 * @param ok {Function} [optional] deprecated (success callback)
		 * @param err {Function} [optional] deprecated (error callback)
		 * @return {jQuery.Promise}
		 */
		parse: function ( wikiText, ok, err ) {
			var apiDeferred = $.Deferred();

			// Backwards compatibility (< MW 1.20)
			if ( ok ) {
				apiDeferred.done( ok );
			}
			if ( err ) {
				apiDeferred.fail( err );
			}

			this.get( {
					action: 'parse',
					text: wikiText
				} )
				.done( function ( data ) {
					if ( data.parse && data.parse.text && data.parse.text['*'] ) {
						apiDeferred.resolve( data.parse.text['*'] );
					}
				} )
				.fail( apiDeferred.reject );

			// Return the promise
			return apiDeferred.promise();
		}
	} );

}( mediaWiki, jQuery ) );