HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

VanillaHtmlFormatter: how to customize, add attributes and class

2»

Comments

  • Since I have no addon but a plugin, I assume I should either use "gdn_pluginManager_afterStart" or "container_init" and I have chosen "container_init". The file in /settings/bootstrap.php is not included

      // Bootstrapping.
       foreach ($addonManager->getEnabled() as $addon) {
           /* @var Addon $addon */
           if ($bootstrapPath = $addon->getSpecial('bootstrap')) {
               $bootstrapPath = $addon->path($bootstrapPath);
               include_once $bootstrapPath;
           }
       }
    
       // Plugins startup
       $addonManager->bindAllEvents($eventManager);
    
       if ($eventManager->hasHandler('gdn_pluginManager_afterStart')) {
           $eventManager->fire('gdn_pluginManager_afterStart', $dic->get(Gdn_PluginManager::class));
       }
    
       // Now that all of the events have been bound, fire an event that allows plugins to modify the container.
       $eventManager->fire('container_init', $dic);
    });
    

    With that code in my plugin (I've also tried RJPlugins\CKEditorHtmlFormat and \RJPlugins\CKEditorHtmlFormat)

       public function container_init(Container $dic) {
           $dic->rule(FormatService::class)->addCall(
               'registerFormat',
               [
                   'Html',
                   new \Garden\Container\Reference(CKEditorHtmlFormat::class)
               ]
           );
       }
    

    I receive the following error, which makes absolutely no sense to me:

    Fatal Error in Gdn_Session.start();
    Call to a member function getSession() on null
    The error occurred on or near: /usr/share/nginx/html/vanilla/installations/release_current/library/core/class.session.php
    
    439: 
    
    440:        // Now retrieve user information.
    
    441:        if ($this->UserID > 0) {
    
    442:            // Instantiate a UserModel to get session info
    
    443:            $this->User = $userModel->getSession($this->UserID);
    
    444: 
    
    445:            $userSignedIn = false;
    
    446:            if ($this->User) {
    
    447:                $this->permissions->setPermissions($this->User->Permissions);
    
    Backtrace:
    
    [/usr/share/nginx/html/vanilla/installations/release_current/library/core/class.auth.php:64] Gdn_Session->start();
    
    [/usr/share/nginx/html/vanilla/installations/release_current/bootstrap.php:517] Gdn_Auth->startAuthenticator();
    
    [/usr/share/nginx/html/vanilla/installations/release_current/index.php:22] PHP::require_once();
    
    

    Looks like what I'm doing in container_init is totally wrong.

  • Okay, this is where I am:

    1. In my plugin I have that method:

       public function container_init(Container $dic) {
           $dic->rule(Vanilla\Formatting\FormatService::class)->addCall(
               'registerFormat',
               [
                   'Html',
                   new \Garden\Container\Reference(RJPlugins\CKEditorHtmlFormat::class)
               ]
           );
       }
    

    2. I have that CKEditorHtmlFormat:

    <?php
    
    namespace RJPlugins;
    
    use Vanilla\Formatting\Formats\HtmlFormat;
    use Vanilla\Formatting\Html\HtmlSanitizer;
    use VanillaHtmlFormatter;
    use Vanilla\EmbeddedContent\LegacyEmbedReplacer;
    use Gdn;
    use Vanilla\Formatting\Formats\StaticCacheTranslationTrait;
    use Vanilla\Formatting\Htm;
    
    /**
     * Handle special markup generated by CKEditor5.
     *
     * CKEditor5 inserts additional CSS classes which are stripped out by Vanillas
     * default Html renderer.
     *
     */
    class CKEditorHtmlFormat extends HtmlFormat {
    
       // use StaticCacheTranslationTrait;
    
       const FORMAT_KEY = "html";
    
       /** @var HtmlSanitizer */
       private $htmlSanitizer;
    
       /** @var HtmlEnhancer */
       private $htmlEnhancer;
    
       /** @var bool */
       private $shouldCleanupLineBreaks;
    
       /** @var HtmlPlainTextConverter */
       private $plainTextConverter;
    
       /**
        * Constructor for dependency injection.
        *
        * @param HtmlSanitizer $htmlSanitizer
        * @param HtmlEnhancer $htmlEnhancer
        * @param HtmlPlainTextConverter $plainTextConverter
        * @param bool $shouldCleanupLineBreaks
        */
       public function __construct(
           HtmlSanitizer $htmlSanitizer,
           HtmlEnhancer $htmlEnhancer,
           HtmlPlainTextConverter $plainTextConverter,
           bool $shouldCleanupLineBreaks = true
       ) {
           $this->htmlSanitizer = $htmlSanitizer;
           $this->htmlEnhancer = $htmlEnhancer;
           $this->plainTextConverter = $plainTextConverter;
           $this->shouldCleanupLineBreaks = $shouldCleanupLineBreaks;
       }
    
       /**
        * @inheritdoc
        */
       public function renderHtml(string $content, bool $enhance = true): string {
    return 'test';
    ...
    

    I would expect that this simply returns a "test" for every Html post, but it does not, it simply renders them as html like before...

Sign In or Register to comment.