返回介绍

Formatting

发布于 2024-06-22 20:04:58 字数 2438 浏览 0 评论 0 收藏 0

Flarum uses the powerful s9e TextFormatter library to format posts from plain markup into HTML. You should become familiar with how TextFormatter works before you attempt to extend it.

In Flarum, post content is formatted with a minimal TextFormatter configuration by default. The bundled Markdown and BBCode extensions simply enable the respective plugins on this TextFormatter configuration.

Configuration

You can configure the TextFormatter Configurator instance, as well as run custom logic during parsing and rendering, using the Formatter extender:

use Flarum\Extend;
use Psr\Http\Message\ServerRequestInterface as Request;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Parser;
use s9e\TextFormatter\Renderer;

return [
    (new Extend\Formatter)
        // Add custom text formatter configuration
        ->configure(function (Configurator $config) {
            $config->BBCodes->addFromRepository('B');
        })
        // Modify raw text before it is parsed.
        // This callback should return the modified text.
        ->parse(function (Parser $parser, $context, $text) {
            // custom logic here
            return $newText;
        })
        // Modify the XML to be rendered before rendering.
        // This callback should return the new XML.
        // For example, in the mentions extension, this is used to
        // provide the username and display name of the user being mentioned.
        // Make sure that the last $request argument is nullable (or omitted entirely).
        ->render(function (Renderer $renderer, $context, $xml, Request $request = null) {
            // custom logic here
            return $newXml;
        })
];

With a good understanding of TextFormatter, this will allow you to achieve anything from simple BBCode tag additions to more complex formatting tasks like Flarum's Mentions extension.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文