Possible to use both of these together? Looks like nbsp is only added on the last paragraph tag rather then all of them.
<?=$page->description()->kirbytext()->widont();?>
Expected:
<p>Lorem ipsum dolor sit amet.</p>
<p>Donec quam felis, ultricies nec.</p>
Actual:
<p>Lorem ipsum dolor sit amet.</p>
<p>Donec quam felis, ultricies nec.</p>
The widont
method only works for single paragraphs, it is not applied to each one.
You could however create your own copy of widont
(the code it uses is in the Str
class) and put your new code inside a new field method (for example in a plugin):
field::$methods['multiWidont'] = function($field) {
$field->value = 'Your modification code here';
return $field;
};
I think widont
could be multiline if you add the multiline pattern modifier to the regex pattern.
Do you think it would work if we change the regex from:
|([^\s])\s+([^\s]+)\s*$|
to
\|([^\s])\s+([^\s]+)\s*$|\m
1 Like
This is what I had come up with, maybe could be made to be more efficient, but it allows for configuration on which tags you want to widon’t inside.
// Which tags should we make sure never end with a widow?
$tags = 'p';
$return = array();
if (!empty($tags) && preg_match_all('#<('.$tags.')>(.+)</\1>#', $string, $m)) {
foreach ($m[0] as $match) {
// Remove any trailing whitespace, it's useless and messes things up.
$match = preg_replace('/\s\s+/', "", $match);
// Pass through toolkit Widont to get version of chunk without widow.
$return[] = Str::widont($match);
}
}
return implode($return);
Thanks, that’s a great idea. I have created an issue about this.