Manual:$wgUseTidy

Details
Use "HTML tidy" to make sure HTML output is sane.

HTML tidy is a free tool that fixes broken HTML. See HTML tidy and http://www.w3.org/People/Raggett/tidy/

You may wish to setup this tool, and set $wgUseTidy=true, to ensure that the wiki outputs reasonably clean and compliant HTML, even when malicous or foolish users add corrupt/badly formatted HTML to wiki pages.

Note that MediaWiki already does some built-in checks and corrections to user's HTML, and limits the range of html tags and attributes which can be used (unless you set $wgRawHtml=true Dangerous!) Limitations are described at meta:Help:HTML in wikitext. The logic for this is found in includes/Sanitizer.php. As such, you may decide that running HTML tidy over the output is not necessary.

Configuration
It is recommended to have configuration file present (Manual:$wgTidyConf) in /extensions/tidy/tidy.conf.

An example is provided in the discussion page here: Manual talk:$wgUseTidy TODO: Move the example onto this page if it works OK