Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-01-06 03:07:18

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

[SOLVED] trim bug in shortcode?

I have the following shortcode for external links

<a<txp:if_yield name="nf"> rel="nofollow noopener"
<txp:else /> 
rel="external noopener"</txp:if_yield>
<txp:if_yield name="lang"> lang="<txp:yield name="lang" />"</txp:if_yield> 
href="<txp:yield name="url" />"<txp:if_yield name="email"> style="color:#ba0000;text-decoration:none;"</txp:if_yield>
<txp:if_yield name="class"> class="<txp:yield name="class" />"</txp:if_yield>>
<txp:if_yield name="txt"><txp:yield name="txt" /><txp:else />
<txp:yield name="url" trim="https?://www.?" /></txp:if_yield></a>

It all works just fine except, there appears to be a strange bug with the trim function.

When I use the shortcode like <txp::ext url="https://domain.tld" /> it all works fine and parses as it should: <a rel="external noopener" href="https://domain.tld">domain.tld</a>

If I use it the same way but the site domain starts with a t, the following happens

shortcode: <txp::ext url="https://tomain.tld" />

Parses to <a rel="external noopener" href="https://tomain.tld">omain.tld</a>, trimming the first t.

Any ideas why this is happening?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#2 2024-01-06 10:56:29

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: [SOLVED] trim bug in shortcode?

It’s not really a bug, I’m surprised it works at all. To trigger regex mode, trim must be a valid delimited regex pattern, like #^https?://(www\.)?#i. Otherwise, it will just progressively strip all provided characters (including t) at url extremities.

Offline

#3 2024-01-06 16:14:48

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

Re: [SOLVED] trim bug in shortcode?

etc wrote #336297:

It’s not really a bug, I’m surprised it works at all. To trigger regex mode, trim must be a valid delimited regex pattern, like #^https?://(www\.)?#i. Otherwise, it will just progressively strip all provided characters (including t) at url extremities.

#^https?://(www\.)?#i did indeed fix it! I had the trim function for some time but I had no urls that started or finished the a t so I never noticed my error.

Thanks so much Oleg!


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#4 2024-11-12 05:08:46

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

Re: [SOLVED] trim bug in shortcode?

I’m returning to this as some of the urls create long horizontal scrolls on mobile devices, so more trimming is needed.

The current simplified tag structure is:

<a rel="external noopener" href="<txp:yield name="url" />">
<txp:if_yield name="txt">
<txp:yield name="txt" />
<txp:else />
<txp:yield name="url" trim="#^https?://(www\.)?#i" />
</txp:if_yield>
</a>

Is there a way to limit the characters of the urls to 30 characters when they appear as linked text and replace those trimmed characters with replace="&hellip;?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#5 2024-11-12 09:14:43

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: [SOLVED] trim bug in shortcode?

Something like this?

<txp:yield name="url" trim="#^https?://(?:www\.)?(.{5,25})\b.*$#is" replace="$1&hellip;" />

Offline

#6 2024-11-12 13:52:36

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

Re: [SOLVED] trim bug in shortcode?

etc wrote #338195:

Something like this?

<txp:yield name="url" trim="#^https?://(?:www\.)?(.{5,25})\b.*$#is" replace="$1&hellip;" />...

Indeed!!! Thanks!! I can understand the 25 in the structure, but what is the first 5 for?


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#7 2024-11-12 17:21:11

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: [SOLVED] trim bug in shortcode?

colak wrote #338198:

I can understand the 25 in the structure, but what is the first 5 for?

I guess for avoiding short urls truncation, but it does not seem very reliable. Maybe this?

/^https?:\/\/(?:www\.)?(.{25}(?:(?<=\w)\w)*).{5,}$/s

It is expected to skip 25 characters and stop at the first non-word character after it, if the remaining chunk is at least 5 characters long. But Google ChatGPT might be able to provide a better pattern :-)

Offline

#8 2024-11-12 18:31:28

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

Re: [SOLVED] trim bug in shortcode?

Could it be that you’re missing a { at .{25}, e.g.

/^https?:\/\/(?:www\.)?(.{25}(?:(?<=\w)\w*|\b)).{5,}$/s

It’s an interesting idea, but for long domain names, 25 characters is quite limiting making the links essentially indistinguishable.

You can play around with it here on regex101.


TXP Builders – finely-crafted code, design and txp

Offline

#9 2024-11-12 18:38:22

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: [SOLVED] trim bug in shortcode?

Oops, thank you, corrected.

You can add a ‘tail’:

<txp:yield name="url"
    trim="/^https?:\/\/(?:www\.)?(.{25}(?:(?<=\w)\w)*).{5,}(.\b.{5,})$/s"
    replace="$1&hellip;$2"
/>

But make sure first that url is long enough to match the pattern. It makes me think that <txp:if_yield /> needs match attribute, with pattern among other options.

Offline

#10 2024-11-12 23:27:38

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

Re: [SOLVED] trim bug in shortcode?

Excellent. That’s a good idea for differentiating links that begin similarly and would otherwise look identical when truncated. I also learned some more regex from that, too.


TXP Builders – finely-crafted code, design and txp

Offline

#11 2024-11-13 04:15:35

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

Re: [SOLVED] trim bug in shortcode?

etc wrote #338202:

Oops, thank you, corrected.

You can add a ‘tail’:

<txp:yield name="url"...

But make sure first that url is long enough to match the pattern. It makes me think that <txp:if_yield /> needs match attribute, with pattern among other options.

This is indeed excellent! I particularly like how the end of the url is preserved, which is very important for links that direct to pdfs. The only minor issue that I have detected is that short urls do not get their first part trimmed. ie. https://www.sitesitesite.tld remains the same.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#12 2024-11-13 11:08:32

etc
Developer
Registered: 2010-11-11
Posts: 5,186
Website GitHub

Re: [SOLVED] trim bug in shortcode?

colak wrote #338206:

The only minor issue that I have detected is that short urls do not get their first part trimmed. ie. https://www.sitesitesite.tld remains the same.

Yep, that’s what I’m alluding to by saying ‘long enough’. You can separate two cases:

<txp:if_request name='<txp:yield name="url" />' type="name" value="^.{50}" match="pattern">
    <txp:yield name="url" trim="/^https?:\/\/(?:www\.)?(.{25}(?:(?<=\w)\w)*).{5,}(.\b.{5,})$/s" replace="$1&hellip;$2" />
<txp:else />
    <txp:yield name="url" trim="/^https?:\/\/(?:www\.)?/i" />
</txp:if_request>

Or trim twice:

<txp:evaluate trim="/^\s*(.{25}(?:(?<=\w)\w)*).{5,}(.\b\S{5,})\s*$/s" replace="$1&hellip;$2">
    <txp:yield name="url" trim="/^https?:\/\/(?:www\.)?/i" />
</txp:evaluate>

And if one day conditional replacements are finally implemented in php, a universal pattern will be possible.

Offline

Board footer

Powered by FluxBB