Skip to content

Highlight text containing diacritics on search results #4188

@athoik

Description

@athoik

Describe the Bug

Searching text with or without diacritics works great! 👍

Although the highlighted text works only if an exact match is found on getMatchPositions

$pos = mb_strpos($text, $term, $offset);

So search a text like δοκιμή will only get highlighted only if enter as written on a page. Entering text δοκιμη works, but no highlighted text shown on search results.

The following patch fixes the issue, using transliterator_transliterate to convert text to lower case without diacritics.
It requires package php-intl installed (eg apt-get install php8.2-intl).

diff --git a/app/Search/SearchResultsFormatter.php b/app/Search/SearchResultsFormatter.php index 9cbc5ee6..6bbab29a 100644 --- a/app/Search/SearchResultsFormatter.php +++ b/app/Search/SearchResultsFormatter.php @@ -84,11 +84,11 @@ class SearchResultsFormatter protected function getMatchPositions(string $text, array $terms): array { $matchRefs = []; - $text = mb_strtolower($text); + $text = transliterator_transliterate('NFD; [:Nonspacing Mark:] Remove; Lower; NFC;', $text); foreach ($terms as $term) { $offset = 0; - $term = mb_strtolower($term); + $term = transliterator_transliterate('NFD; [:Nonspacing Mark:] Remove; Lower; NFC;', $term); $pos = mb_strpos($text, $term, $offset); while ($pos !== false) { $end = $pos + mb_strlen($term); 

I believe above above change will work universally for all languages with diacritics.

Please consider accepting that change, if you believe it will improve BookStack.

Thanks!

Steps to Reproduce

  1. Create a page that contains text δοκιμή (Greek word for test, with ή -> GREEK SMALL LETTER ETA WITH TONOS)
  2. Got to 'search'
  3. Type word δοκιμη (small letters without diacritics)
  4. Search results appear but text δοκιμή is not highlighted

Expected Behaviour

The text δοκιμή should be highlighted, since it was possible to search that text.

Screenshots or Additional Context

No response

Browser Details

No response

Exact BookStack Version

v23.02.3

PHP Version

8.2.5

Hosting Environment

Debian 11 with PHP 8.2 by @armando-femat

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions