418fd2306f7d2390d6aefbe9343dd6031c90c043
[lhc/web/wiklou.git] / resources / src / mediawiki / api / parse.js
1 /**
2 * @class mw.Api.plugin.parse
3 */
4 ( function ( mw, $ ) {
5
6 $.extend( mw.Api.prototype, {
7 /**
8 * Convenience method for 'action=parse'.
9 *
10 * @param {string|mw.Title} content Content to parse, either as a wikitext string or
11 * a mw.Title.
12 * @param {Object} additionalParams Parameters object to set custom settings, e.g.
13 * redirects, sectionpreview. prop should not be overridden.
14 * @return {jQuery.Promise}
15 * @return {Function} return.done
16 * @return {string} return.done.data Parsed HTML of `wikitext`.
17 */
18 parse: function ( content, additionalParams ) {
19 var apiPromise, config = $.extend( {
20 formatversion: 2,
21 action: 'parse',
22 contentmodel: 'wikitext'
23 }, additionalParams );
24
25 if ( mw.Title && content instanceof mw.Title ) {
26 // Parse existing page
27 config.page = content.getPrefixedDb();
28 } else {
29 // Parse wikitext from input
30 config.text = String( content );
31 }
32
33 apiPromise = this.get( config );
34
35 return apiPromise
36 .then( function ( data ) {
37 return data.parse.text;
38 } )
39 .promise( { abort: apiPromise.abort } );
40 }
41 } );
42
43 /**
44 * @class mw.Api
45 * @mixins mw.Api.plugin.parse
46 */
47
48 }( mediaWiki, jQuery ) );