Skip to content

Commit

Permalink
fix: better cut off (#209)
Browse files Browse the repository at this point in the history
Co-authored-by: Thessa Kockelkorn <thessa@notfound.nl>
  • Loading branch information
thessakockelkorn and Thessa Kockelkorn committed Feb 21, 2024
1 parent 49288b5 commit ca8f824
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Models/Indexes/SolrItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ public function resultList()
$resultArray['summary'] = '';
if (isset($this->highlights->{$result->url}->{$this->header->params->{'hl.fl'}}[0])) {
$summary = Str::words(preg_replace("/^\p{P}\s+/", '', $this->highlights->{$result->url}->{$this->header->params->{'hl.fl'}}[0]), $this->highlightLength, ' ...');
$summary = Str::limit($summary, 500);
$resultArray['summary'] = $summary;
$truncated = Str::limit($summary.' ', 500); // truncate excessively long strings resulting from parsing full pdf texts
$position = strrpos($truncated, ' ');
$truncated = substr($summary, 0, $position); // cut off last word if its is incomplete (i.e. not followed by a space)
if (substr($truncated, -4) != ' ...') { // put ellipsis back if it was cut off
$truncated .= ' ...';
}
$resultArray['summary'] = $truncated;
}
$resultList[] = (object) $resultArray;
}
Expand Down

0 comments on commit ca8f824

Please sign in to comment.