Kirbytext: don't clean up link tag

The link KirbyTag (link: https://www.google.com/ text: https://www.google.com/) cleans up the URL and outputs <a href="https://www.google.com/">google.com</a>.

Is there a way to disable this behavior to output the full text (https://www.google.com/)?

Not without overriding the link tag, I’m afraid. The shortening happens in the Html::link() method.

So, in the code for the link tag, Html::a() is used to build the link. Inside the a() method, Html::link() is called. So when overriding, don’t use Html::a() but build the link manually.

1 Like

Great, thanks for the hint!

<?php

$originalTag = Kirby\Text\KirbyTag::$types['link'];

Kirby::plugin('my/plugin', [
    'tags' => [
        'link' => [

            'attr' => [
                'class',
                'lang',
                'rel',
                'role',
                'target',
                'title',
                'text',
            ],

            'html' => function($tag) use ($originalTag)  {

                $result = '<a href="' . $tag->value . '">' . $tag->text . '</a>';

                return $result;
            }
        ]
    ]
]);

Although I’m not actually “reusing” the tag since I can’t wrap my head around how to reuse all the other attributes in my new tag without using too many if statements…

guess I could maybe use Html::link inside a preg_replace to replace the “clean” link with the wanted one…? hmmm

hmm, apparently there’s something I don’t quite understand about how kirbytags are processed. If I do

'html' => function($tag) use ($originalTag)  {

    $wanted = 'asdasd';
    $result = $originalTag['html']($tag);
    $pattern = '~>\K[^<>]*(?=<)~';

    $result = preg_replace($pattern, $wanted, $result);

    return $result;
}

inside the above function, it correctly outputs
<a class="testclass" href="https://www.google.com/">asdasd</a>

but if I do
$result = preg_replace($pattern, $tag->text, $result);

it becomes
<a class="testclass" href="https://www.google.com/"></a> <a href="https://www.google.com/">https://www.google.com/</a>

:exploding_head:

giving up for now…

You could simply do it like this

$originalTag = Kirby\Text\KirbyTag::$types['link'];
Kirby::plugin('your/plugin', [
    'tags' => [
        'link' => [
            'attr' => $originalTag['attr'],

            'html' => function($tag) {
                if (empty($tag->lang) === false) {
                    $tag->value = Url::to($tag->value, $tag->lang);
                }
    
                return '<a href="' . $tag->value . '"' . ' rel="' . $tag->rel . '"' . ' class="' . $tag->class . '">' . $tag->text . '</a>'; 
            }
        ]
    ]
]);

(I left out some of the properties, because to lazy to type it all out :wink: , and may syntax may not be 100% correct (I hate concatening HTML strings and variables).

1 Like

On the same boat… But this is what I’ll do for now! Thanks a lot :~)

…wait, but this solution also returns weird Matrjoschka’d anchor tags :face_with_raised_eyebrow:
<a href="https://www.google.com/" rel="" class="testclass"><a href="https://www.google.com/">https://www.google.com/</a></a>

Hm, I wonder if that’s a bug in 3.4.4, because it seems to work in 3.4.2.

…i’m on 3.3.5, just realized – let’s upgrade…

aaaand it’s working. Wonder if I’ll ever remember upgrading Kirby before resuming work on unfinished projects :upside_down_face: