I’ve been converting a K2 project over to K3 and have noticed a little glitch in the way that links are stripped out of blocks of text when using excerpt()
in K3.
I’m using markdown to add links to the text field in my content files, and have been using the following code to trim down that text when it’s displayed in a search result:
$result->text()->excerpt(140)
In K2, the search results look like this:
In K3, if a markdown link is followed by a punctuation mark (such as a period or comma) and that link gets stripped from the text, an extra space gets inserted between the originla linked text and the punctuation.
In the example below, the words “cursor”, “input”. and “tapped” were all linked in the the original text field.
Can I assume that this is a bug in the excerpt()
method?
It happens because of this line:
$string = strip_tags(str_replace('<', ' <', $string));
in the Str::excerpt()
method.
Ok, but should it be happening in the first place?
K2 doesn’t do this, and it’s rather annoying.
No, that shouldn’t happen. Although, I guess there was a reason this string replacement was added. Maybe create an issue on GitHub?
In the meantime, you can do a string replacement:
<?php echo str_replace(' .', '.', $page->textarea()->kt()->excerpt(140)) ?>
Or with an array of characters as first argument to include question marks, exclamation marks etc.
Thanks for the workaround example.
I was going to try a string replacement similar to this, regardless.
And I will create an issue on GitHub as well.
splorp
September 24, 2022, 4:51am
7
I’m pleased to report that this issue has been fixed in the Kirby 3.8 release candidate.
getkirby:develop
← getkirby:fix/4301-excerpt-space
opened 07:51PM - 15 Sep 22 UTC
## This PR …
<!--
A clear and concise description of the PR.
Use this section… for review hints, explanations or discussion points/todos.
Add relevant release notes: Features, Enhancements, Fixes, Deprecated.
Reference issues from the `kirby` repo or ideas from `feedback.getkirby.com`.
Always mention whether your PR introduces breaking changes.
How to contribute: https://contribute.getkirby.com
-->
### Fixes
- `Str::except()` does not add superfluous space after stripped tag and before interpunctuation
#4301
## Ready?
<!--
If you can help to check off the following tasks, that'd be great.
If not, don't worry - we will take care of it.
More details: https://contribute.getkirby.com
-->
- [x] Unit tests for fixed bug/feature
- [x] In-code documentation (wherever needed)
- [x] Tests and checks all pass
### For review team
<!--
We will take care of the following before merging the PR.
-->
- [x] Add changes to release notes draft in Notion
- [x] ~~Add to [website docs release checklist](https://github.com/getkirby/getkirby.com/pulls) (if needed)~~