aboutsummaryrefslogtreecommitdiffstats
path: root/resources/src/mediawiki.api/parse.js
blob: 617e499b59a36e7875a19b9818c781f1283925f5 (plain) (blame)
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
( function () {

	Object.assign( mw.Api.prototype, /** @lends mw.Api.prototype */ {
		/**
		 * Convenience method for 'action=parse'.
		 *
		 * @param {string|mw.Title} content Content to parse, either as a wikitext string or
		 *   a mw.Title.
		 * @param {Object} additionalParams Parameters object to set custom settings, e.g.
		 *   `redirects`, `sectionpreview`. `prop` should not be overridden.
		 * @return {jQuery.Promise<string>} Promise that resolves with the parsed HTML of `wikitext`
		 */
		parse: function ( content, additionalParams ) {
			let apiPromise,
				config = Object.assign( {
					formatversion: 2,
					action: 'parse',
					// Minimize the JSON we get back, there is no way to access anything else anyway
					prop: 'text',
					contentmodel: 'wikitext'
				}, additionalParams );

			if ( mw.Title && content instanceof mw.Title ) {
				// Parse existing page
				config.page = content.getPrefixedDb();
				apiPromise = this.get( config );
			} else {
				// Parse wikitext from input
				config.text = String( content );
				apiPromise = this.post( config );
			}

			return apiPromise
				.then( ( data ) => data.parse.text )
				.promise( { abort: apiPromise.abort } );
		}
	} );

}() );