/**
* Convenience method for 'action=parse'.
*
- * @param {string} wikitext
+ * @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}
* @return {Function} return.done
* @return {string} return.done.data Parsed HTML of `wikitext`.
*/
- parse: function ( wikitext ) {
- var apiPromise = this.get( {
+ parse: function ( content, additionalParams ) {
+ var apiPromise, config = $.extend( {
formatversion: 2,
action: 'parse',
- contentmodel: 'wikitext',
- text: wikitext
- } );
+ contentmodel: 'wikitext'
+ }, additionalParams );
+
+ if ( typeof content === 'string' ) {
+ // Wikitext
+ config.text = content;
+ } else {
+ // mw.Title
+ config.page = content.getPrefixedDb();
+ }
+
+ apiPromise = this.get( config );
return apiPromise
.then( function ( data ) {
} ) );
QUnit.test( 'Hello world', function ( assert ) {
- QUnit.expect( 1 );
+ QUnit.expect( 2 );
var api = new mw.Api();
);
} );
+ api.parse( new mw.Title( 'Earth' ) ).done( function ( html ) {
+ assert.equal( html, '<p><b>Earth</b> is a planet.</p>' );
+ } );
+
+ this.server.respondWith( /action=parse.*&page=Earth/, function ( request ) {
+ request.respond( 200, { 'Content-Type': 'application/json' },
+ '{ "parse": { "text": "<p><b>Earth</b> is a planet.</p>" } }'
+ );
+ } );
+
this.server.respond();
} );
}( mediaWiki ) );