/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

'use strict';

const cheerio = require( 'cheerio' );

/**
 * Enforce priority 30 to run it after apply design doc classes filter.
 */
hexo.extend.filter.register( 'after_post_render', page => {
	if ( page.projectTheme !== 'gloria' ) {
		return page;
	}

	const $ = cheerio.load( page.content, null, false );

	$( 'table' ).each( ( _, table ) => {
		if ( $( table ).parent().prop( 'tagName' ) !== 'FIGURE' ) {
			// If the table is not wrapped in a figure, wrap it.
			$( table ).wrap( '<figure class="doc b-table-wrapper"></figure>' );
		}
	} );

	page.content = $.html();

	return page;
}, 30 );
