Solution for specific czech typography rules

Hi to Kirby community.

I would like to kindly ask you for help with trying to code a solution for specific typographic rules for czech language. We have a specific treatment for letters like a,v,i,s,o,k,z and similar that they could not be alone at the end of the line of text. There should always be no-breaking-space between them and the next word.

There’s plenty solutions for php to do it automatically when the editors forget to put no-break space in, but I can’t make them work in Kirby. I think I just overlook something or putting in together in the wrong way.

There is one of the solution:

<?php
function fixPrepositions($text) {
    // Seznam jednoznakových předložek a spojek
    $prepositions = ['k', 's', 'v', 'z', 'o', 'u', 'a', 'i', 'u'];
    
    // Vytvoření regulárního výrazu pro nahrazení
    $pattern = '/\b(' . implode('|', $prepositions) . ')\s+/ui';
    
    // Nahrazení mezery za pevnou mezeru (&nbsp;)
    $replacement = '$1&nbsp;';
    
    return preg_replace($pattern, $replacement, $text);
}

// Testovací text
$text = "Jdu k domu a v lese najdu cestu. Z hory s kamarádem o dovolené.";
$fixedText = fixPrepositions($text);

echo $fixedText;
?>

Any help would be welcome.
Thanks a lot.

Would be good to know what you tried and how to see why it didn’t work.

I would think a custom field method or even a general KirbyText hook could be starting points.