Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Text Formatter Laravel Package

s9e/text-formatter

PHP text formatting library with plugin support for BBCode, Markdown, HTML, and more. Includes predefined bundles, extensive documentation, and a JavaScript port for client-side preview and demos. Install via Composer and integrate customizable parsing/rendering.

View on GitHub
Deep Wiki
Context7

Your own bundle

To create your own bundle, first configure all of the plugins and settings, then call $configurator->saveBundle() with the name of the class and where to save it as a file.

$configurator = new s9e\TextFormatter\Configurator;
$configurator->BBCodes->addFromRepository('B');
$configurator->BBCodes->addFromRepository('I');
$configurator->BBCodes->addFromRepository('URL');
$configurator->Emoticons->add(':)', '/path/to/img.png');

$success = $configurator->saveBundle('MyBundle', '/tmp/MyBundle.php');
var_dump($success);
bool(true)

Then you can use your own bundle as you would any other:

// Include the bundle's file manually if your autoloader isn't configured for it
include_once '/tmp/MyBundle.php';

$text = '[b]Hello World[/b]';
$xml  = MyBundle::parse($text);
$html = MyBundle::render($xml);

echo $html;
<b>Hello World</b>

Customize a bundle

For convenience, you can use an existing bundle as a starting point to create your own. To do so, instead of creating a new configurator, you can obtain an instance of the bundle's configurator. Then you can save it back as your own bundle.

// Use the Forum bundle's configurator
$configurator = s9e\TextFormatter\Configurator\Bundles\Forum::getConfigurator();

// Customize it to your need
$configurator->HTMLElements->allowElement('b');
$configurator->HTMLElements->allowElement('i');
$configurator->HTMLElements->allowElement('u');

// Save it back as your own
$configurator->saveBundle('MyBundle', '/tmp/MyBundle.php');

Prepare your bundle for redistribution

If you intend to redistribute your bundle, you may want to use a native PHP renderer rather than the default XSLT renderer. A native PHP renderer compiles the XSL templates to native PHP and does not require the ext/xsl extension.

To make maintenance easier, you may create your own bundle configurator by extending s9e\TextFormatter\Configurator\Bundle. For convenience, your configurator and the generated files (the bundle file and the PHP renderer) should all live in the same namespace and rely on your normal autoloader.

The following code is what a minimal bundle configurator can look like. You can see a more complete example in the My\Project repository.

namespace My\Project\TextFormatter;

use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Configurator\Bundle as AbstractBundleConfigurator;

class BundleConfigurator extends AbstractBundleConfigurator
{
	public function configure(Configurator $configurator): void
	{
		// Configure plugins
		$configurator->BBCodes->addFromRepository('B');

		// Configure the PHP renderer to exist in the current namespace
		$configurator->rendering->engine            = 'PHP';
		$configurator->rendering->engine->className = __NAMESPACE__ . '\\Renderer';
		$configurator->rendering->engine->filepath  = __DIR__ . '/Renderer.php';
	}

	public static function saveBundle(): bool
	{
		$configurator = (new static)->getConfigurator();

		return $configurator->saveBundle(
			__NAMESPACE__ . '\\Bundle',
			__DIR__ . '/Bundle.php',
			['autoInclude' => false]
		);
	}
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours