Kirbytext email field renders to crazy formatting

Hi there,
I am simply trying to insert an email within a kirbytext field and for some reason, it renders like this:

Here’s the formatting that the Elements inspector shows me:

Does anybody know what’s going on?

Thanks!

This: https://getkirby.com/docs/reference/tools/str/encode => https://github.com/getkirby/kirby/blob/3.3.4/src/Toolkit/Str.php#L285

Sorry, I don’t quite understand. How am I supposed to use the Str::encode() method here? I am trying to create the email link via a Markdown tag within a Kirbytext field.

My markdown tag is simply (email: something@gmx.de).

Yes, if you render the tag with kirbytext() the email is encoded (if you inspect the source), but should show up alright as a mailto link in the browser.

Well, it doesn’t, it just shows up as gibberish, see the screenshot above. And I am using kirbytext(). But now that you mentioned that it is automatically encoded, I have the suspicion I realize that a kirbytext hook of mine (that wraps all numbers in spans) is responsible for messing this up. Didn’t realize that the encoding process inserts numbers into this field.

Now, this would result into a follow-up question: Is there a way to selectively turn off kirbytext:after hooks for email fields only? (I guess not?)

What exactly does your hook do? And which hook exactly do you use?

I almost don’t want to post it, because it makes my original question so stupid. :see_no_evil: It searches all numbers and wraps them in a span:

'kirbytext:after' => function ($text) {

  return preg_replace('/(\d+|td)/', '<span class="n">$1</span>', $text);
}

Edit: Well, it searches all number and the letter combination td. :wink:

I don’t know why you are doing this, but if you use a hook that does the number wrapping before Kirbytext is parsed, you shouldn’t get into trouble, see this lovely recipe:

I need to assign a different font-family-settings to numbers.

Upon first inspection, that seems to work and does not seem to mess anything up. Lovely! Thank you!

Sorry, I have to re-activate this thread, because now I have in fact run into trouble with the proposed solution. Moving the kirbyhook from after to before worked for the email, but instead it messes up links containing numbers that appear in Kirbytext.

This is the kirbytext hook I am currently using:

'kirbytext:before' => function ($text) {

  return preg_replace('/(\d+|td)/', '<span class="n">$1</span>', $text);
}

Of course this messes up any url containing numbers as it wraps the numbers into span tags and renders the url useless.

Is there any way to have this hook generally working, but disable it on urls?

You could either add some code to your regex which will cause it to not match if a number is prefixed by a scheme followed by some characters without spaces OR add a leading and trailing space (or \s+) if the numbers you are interested in are always enclosed by spaces (or whitespace characters).

No, they are not unfortunately. Hm this will turn into some really complicated regex, which I really would like to avoid.

Is there maybe some way to apply some sort of extra-hook that processes all rendered text after all the kirbytext rendering is done? That way I could insert the span tags as the last step.

The kirbytext:after hook is the last one that runs: https://getkirby.com/docs/cookbook/extensions/kirbytext-hooks

Wouldn’t it be easier to create a custom Kirbytag for the numbers that have to be wrapped in spans?

Unfortunately not. Every single number needs to be wrapped into these spans (well, except those numbers that are part of urls and encoded email adresses), I cannot expect the editors to wrap every number into an extra kirbytag.

What do you think about Markdown Email links and using markdown instead of kirbytext for that field?

Not tested…

That would not be encoded and protected then, right?

At the moment I cannot test, it is a guess…

Good luck!

Reconsider that if you will find no other solution. Yes, a regex might become complicated but thats because they are powerful. There are quite a few online resources like https://regexr.com/ which might help you, I would not think it is impossible.

I think there is no other option than regex then. Keep in mind that depending on the length of your texts parsing might be costly. What exactly are you doing with these numbers and is it really worth it?