Normalize Whitespace

Supports multiple operations to normalize whitespace in code blocks.

How to use

Obviously, this is supposed to work only for code blocks (<pre><code>) and not for inline code.

By default the plugin trims all leading and trailing whitespace of every code block. It also removes extra indents and trailing whitespace on every line.

The plugin can be disabled for a particular code block by adding the class no-whitespace-normalization to either the <pre> or <code> tag.

The default settings can be overridden with the setDefaults() method like so:

	'remove-trailing': true,
	'remove-indent': true,
	'left-trim': true,
	'right-trim': true,
	/*'break-lines': 80,
	'indent': 2,
	'remove-initial-line-feed': false,
	'tabs-to-spaces': 4,
	'spaces-to-tabs': 4*/

The following settings are available:

Removes trailing whitespace on all lines.
If the whole code block is indented too much it removes the extra indent.
Removes all whitespace from the top of the code block.
Removes all whitespace from the bottom of the code block.
Simple way of breaking long lines at a certain length (default is 80 characters).
Adds a certain number of tabs to every line.
Less aggressive version of left-trim. It only removes a single line feed from the top of the code block.
Converts all tabs to a certain number of spaces (default is 4 spaces).
Converts a certain number of spaces to a tab (default is 4 spaces).


The following example demonstrates the use of this plugin:


The result looks like this:


		var example = {
			foo: true,

			bar: false


It is also compatible with the keep-markup plugin:


	@media screen {
		div {
			text-decoration: underline;
			background: url('foo.png');

This plugin can also be used on the server or on the command line with Node.js:

var Prism = require('prismjs');
var Normalizer = require('prismjs/plugins/normalize-whitespace/prism-normalize-whitespace');
// Create a new Normalizer object
var nw = new Normalizer({
	'remove-trailing': true,
	'remove-indent': true,
	'left-trim': true,
	'right-trim': true,
	/*'break-lines': 80,
	'indent': 2,
	'remove-initial-line-feed': false,
	'tabs-to-spaces': 4,
	'spaces-to-tabs': 4*/

// ..or use the default object from Prism
nw = Prism.plugins.NormalizeWhitespace;

// The code snippet you want to highlight, as a string
var code = "\t\t\tvar data = 1;    ";

// Removes leading and trailing whitespace
// and then indents by 1 tab
code = nw.normalize(code, {
	// Extra settings
	indent: 1

// Returns a highlighted HTML string
var html = Prism.highlight(code, Prism.languages.javascript);