Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Search/Comb/Comb.php
Original file line number Diff line number Diff line change
Expand Up @@ -1077,13 +1077,13 @@ private function extractSnippets($value, $chunks)
[, $before, $chunk, $after] = $match;
$before = $surplus.$before;
$surplus = '';
$half = floor(($length - Str::length($chunk)) / 2);
$half = max(0, floor(($length - Str::length($chunk)) / 2));
if (Str::length($after) < $half) {
$snippet = $chunk.$after;
$snippet = Str::safeTruncateReverse($before, $length - Str::length($snippet)).$snippet;
$snippet = Str::safeTruncateReverse($before, max(0, $length - Str::length($snippet))).$snippet;
} else {
$snippet = Str::safeTruncateReverse($before, $half).$chunk;
$trimmed = Str::safeTruncate($after, $length - Str::length($snippet));
$trimmed = Str::safeTruncate($after, max(0, $length - Str::length($snippet)));
$surplus = Str::substr($after, Str::length($trimmed));
$snippet = $snippet.$trimmed;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Search/CombTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,36 @@ public function it_extracts_snippets_from_a_bard_field()
$this->assertEquals($expected, collect($results['data'] ?? [])->pluck('snippets.content')->all());
}

#[Test]
public function it_extracts_snippets_when_the_term_is_longer_than_the_snippet_length()
{
// https://github.com/statamic/cms/issues/12951
$content = <<<'EOT'
We know, it was a long wait, but now we finally have it, support for OpenID
Connect front and back-channel logout. The backchannel_logout_session_required
flag can be set on a client. See backchannel_logout_uri too. The
frontchannel_logout_session_required flag is the front-channel equivalent, and
backchannel_logout_session_required appears once more right here.
EOT;

$comb = new Comb([
['content' => $content],
], ['snippet_length' => 30]);

try {
$results = $comb->lookUp('backchannel_logout_session_required');
} catch (NoResultsFound $e) {
$results = [];
}

$expected = [[
'backchannel_logout_session_required',
'backchannel_logout_session_required',
]];

$this->assertEquals($expected, collect($results['data'] ?? [])->pluck('snippets.content')->all());
}

#[Test]
public function it_can_search_for_plus_signs()
{
Expand Down
Loading