-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Question: How to add classes on specific node ? #605
Comments
You can create an event listener the handles the In your case, that code would look something like this: use League\CommonMark\EnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
// TODO: add other missing "use" statements here
class AddCustomCssClassesProcessor
{
private $environment;
public function __construct(EnvironmentInterface $environment)
{
$this->environment = $environment;
}
public function onDocumentParsed(DocumentParsedEvent $event)
{
$document = $event->getDocument();
$walker = $document->walker();
while ($event = $walker->next()) {
$node = $event->getNode();
// Only stop when we first encounter a node
if (!$event->isEntering()) {
continue;
}
if ($node instanceof Heading && $node->getLevel() === 1) {
$node->data['attributes']['class'] = 'title-main';
} elseif ($node instanceof Table) {
$node->data['attributes']['class'] = 'table';
}
// etc.
}
}
} The code above is untested but it should get you on the right track. I do like the idea of creating a more-generic, core piece of functionality that works similar to your example, so I'll keep this open as a feature request for the upcoming 2.0 release :) |
awesome! I walked throughout the documentation but the event-dispatcher. I didn't expect to find the solution in this section ;p Thanks ! |
Note, I'm using
I'm getting an |
@armetiz Would something like this for you? $config = [
'default_attributes' => [
Heading::class => [
'class' => function (Heading $node) {
if ($node->getLevel() === 1) {
return 'title-main';
} else {
return null;
}
},
],
Table::class => [
'class' => 'table',
],
Paragraph::class => [
'class' => ['text-center', 'font-comic-sans'],
],
Link::class => [
'class' => 'btn btn-link',
'target' => '_blank',
],
],
];
That's probably because |
It's really cool ;) |
New extension implemented in #616 for the upcoming 2.0.0 release! 🎉 |
Pardon my ignorance but I'm way over my head in this :) How would I go about adding CSS classes to task lists? |
Follow the example on https://commonmark.thephpleague.com/2.1/extensions/default-attributes/, but use |
Thanks! 😃 |
Well it works, but it works too good 😅 |
Ah, in that case, you have two options:
|
Thanks for the pointers! |
It took me a while but this is what I've come up with:
It works, but I have a sneaking suspicion there's a more elegant way. |
Is it possible to assign multiple attributes in one go, or will I have to repeat a block with a new key and a new value? |
You're definitely on the right track! Navigating the AST isn't the easiest. You could potentially do something like this instead: 'class' => static function (Node $node) {
// Check every first child down the tree
while (($node = $node->firstChild()) !== null) {
if ($node instanceof TaskListItemMarker) {
return 'task-list-item';
}
// If we've found a nested list we're probably too deep and don't want that nested list affecting the current one
if ($node instanceof ListBlock) {
break;
}
}
return null;
}
That's not possible with this extension, but it's very possible to do with a custom event listener like this one: https://commonmark.thephpleague.com/2.1/customization/event-dispatcher/#example If you have large documents or multiple attributes you want to add, using a custom event listener is almost certainly more efficient. |
I've been looking at that page :) Where would such an event listener be saved? Or does it have to be written in the same page? |
I'd recommend defining that listener as a separate class. (You could define an anonymous class inline with your other code, but that might get messy) |
Is there a recommended location to save class files, or is that arbitrary? |
I'd recommend that you follow PSR-4 |
Something like this?
with |
Ideally, it would be more like this:
It's a great question but unfortunately beyond the scope of this library. I'd recommend doing some research on PSR-0, PSR-4, autoloading, namespaces, and common naming conventions - that should get you moving in the right direction. |
Well that was my initial plan 😄 Would that make the namespace |
Question
I have a common markdown content, without any specific attributes.
When rendering markdown to HTML, I want to see some CSS Classes on HTML nodes.
Let's say :
.title-main
.table
.table .table-row
Is this possible without creating custom renderer ?
It could be awesome to be able to configure using the environment :
The use case is : I'm using a bought theme that request some css class. I don't want to change markdown original content, and I don't want to change theme.
Thank a lot for your awesome job on this package !
Regards,
The text was updated successfully, but these errors were encountered: