s9e\TextFormatter can optionally generate a JavaScript parser that can be used in browsers to parse text and preview the result in a live environment. The JavaScript parser requires Internet Explorer 8 or later, or any contemporary browser that supports XSLTProcessor.

$configurator = new s9e\TextFormatter\Configurator;

// Enable the JavaScript parser

// Now finalize() will return an entry for "js"

With JavaScript enabled, finalize() will return an element named js that contains the JavaScript source for the s9e.TextFormatter JavaScript object.


// Parse $text and return the XML as a string

// Parse $text and preview it in HTMLElement $target. It will return the last node modified
s9e.TextFormatter.preview($text, $target);

// Toggle a plugin by name

// Toggle a tag by name

// Runtime configuration
s9e.TextFormatter.setNestingLimit($tagName, $limit);
s9e.TextFormatter.setTagLimit($tagName, $limit);

Minify the JavaScript parser with the Google Closure Compiler service

The JavaScript parser can be automatically be minified using the Google Closure Compiler service via HTTP. The minification level and other configuration are automatically set.

$configurator = new s9e\TextFormatter\Configurator;

Other minifiers are available.

Speed up minification with a cache

The result of minification can be cached locally and reused. It's only useful if the JavaScript parser is regenerated more often than the configuration changes, since any modification to the configuration produces a different source.

$configurator = new s9e\TextFormatter\Configurator;

    ->cacheDir = '/path/to/cache';

Improve minification

It is possible to improve the minification ratio by disabling features that are not used. For instance, if you only use the parse and preview methods you can reduce the API to only those two methods.

$configurator = new s9e\TextFormatter\Configurator;

$configurator->javascript->exports = [