Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add classes? For example to an list? #155

Closed
ArneAnka opened this issue Apr 20, 2021 · 5 comments
Closed

How do I add classes? For example to an list? #155

ArneAnka opened this issue Apr 20, 2021 · 5 comments

Comments

@ArneAnka
Copy link

Hi!

Pretty straightforward but how do I add class-names so my css framework kan render properly? I want to add list-disc to the <ul>

@colinodell
Copy link
Contributor

thephpleague/commonmark#605 might answer your question

@GrahamCampbell
Copy link
Owner

Thanks so much @colinodell. Massively appreciate your continual jumping into these issues and helping out. :)

@ArneAnka
Copy link
Author

Thanks @colinodell

If anyone is looking to do the same, this is what I did:

  1. Create an Extensions folder under App
  2. Create file called AddListStyleClassesExtension.php under the newly created folder App/Extensions
  3. Populate AddListStyleClassesExtension.php with:
<?php

namespace App\Extensions;

use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ExtensionInterface;

class AddListStyleClassesExtension implements ExtensionInterface
{
    public function register(ConfigurableEnvironmentInterface $environment): void
    {
            $environment
                ->addEventListener(DocumentParsedEvent::class, new AddListStyleClassesRenderer());
        ;
    }
}
  1. Create file called AddListStyleClassesRenderer.php under the newly created folder App/Extensions
  2. Populate AddListStyleClassesRenderer.php with:
<?php
namespace App\Extensions;

use League\CommonMark\EnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Block\Element\ListBlock;

class AddListStyleClassesRenderer {

    /**
     * @param DocumentParsedEvent $e
     *
     * @return void
     */
    public function __invoke(DocumentParsedEvent $e)
    {
        $walker = $e->getDocument()->walker();

        while ($event = $walker->next()) {
            $node = $event->getNode();
            if ($node instanceof ListBlock) {
                $node->data['attributes'] = array('class' => 'list-disc list-inside');
            }
        }
    }

}
  1. Add the AddListStyleClassesExtension class to the markdown.php-config file
    'extensions' => [\App\Extensions\AddListStyleClassesExtension::class],

Done!

@colinodell
Copy link
Contributor

Thanks for sharing your solution with others! o/

@ptmkenny
Copy link

ptmkenny commented Feb 8, 2023

For anybody coming to this from a search engine like I just did, there is a much easier solution now using Default Attributes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants