How do I add a class to all links? #1031
-
What I wantI'd like to add a class to all my links. For instance, I'd like to convert this: MarkdownWhat has [myspace](https://myspace.com) become?! To this: HTMLWhat has <a href="https://myspace.com" class="link">myspace</a> become?! Why I want itI use tailwindcss and have some nice components setup—one being a Tailwindcss component.link {
@apply underline decoration-2 text-blue-600 hover:text-blue-800 dark:text-blue-400 font-semibold;
} What I triedIt looks like there's something perhaps built-in to do this at the end of the Abstract Syntax Tree section: HTML Attributesuse League\CommonMark\Extension\CommonMark\Node\Inline\Link;
$link = new Link('https://twitter.com/colinodell', '@colinodell');
$link->data->append('attributes/class', 'social-link');
$link->data->append('attributes/class', 'twitter');
$link->data->set('attributes/target', '_blank');
$link->data->set('attributes/rel', 'noopener'); But I'm unsure how to…
My existing, working conversion code# Use markdown converter library
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
use League\CommonMark\MarkdownConverter;
# Define markdown converter environment
$environment = new Environment();
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new SmartPunctExtension());
$environment->addExtension(new StrikethroughExtension());
$environment->addExtension(new FrontMatterExtension());
# Define converter
$converter = new MarkdownConverter($environment); Can you tell me how to solve this? PS: Thanks so much for the incredible library. It still blows me away that I can use it (and others' libraries) for free. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The Default Attributes extension does exactly this - no custom code required! If you really did want to implement this with custom code, here's an example you could easily adapt - just change this part: public function onDocumentParsed(DocumentParsedEvent $event): void
{
$document = $event->getDocument();
$walker = $document->walker();
while ($event = $walker->next()) {
$node = $event->getNode();
// Only stop at Link nodes when we first encounter them
if (!($node instanceof Link) || !$event->isEntering()) {
continue;
}
- $url = $node->getUrl();
- if ($this->isUrlExternal($url)) {
- $node->data->append('attributes/class', 'external-link');
- }
+ $link->data->append('attributes/class', 'link');
}
} But since that extension should do everything you need I'd recommend just using that :)
Thank you so much for those kind words! |
Beta Was this translation helpful? Give feedback.
-
Perfect! Thank you so much for letting me know. Exactly what I was looking for. (And I thank you for going the extra mile and showing me how to do it in a totally custom way!) And btw I tried it, and it almost works like a charm—but I may have discovered a bug (one that I can easily work around). But just so you know: Bug overviewThe library throws a fatal error and exits if the value of the class is How to duplicate
$config = [
'default_attributes' => [
Link::class => [
'class' => 'link',
'target' => '_self',
],
],
];
MarkdownWhat has [myspace](https://myspace.com) become?!
Expected resultHTML<p>What has <a href="https://myspace.com" class="link">myspace</a> become?!</p> Actual result
|
Beta Was this translation helpful? Give feedback.
The Default Attributes extension does exactly this - no custom code required!
If you really did want to implement this with custom code, here's an example you could easily adapt - just change this part: