Fix handling bard text

This commit is contained in:
kolaente 2020-12-13 22:44:13 +01:00
parent 7ed3687d12
commit 81b16772d7
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 19 additions and 0 deletions

View File

@ -2,6 +2,7 @@
namespace Kolaente\Snippet\Modifiers;
use Statamic\Fields\Value;
use Statamic\Modifiers\Modifier;
class TextSnippet extends Modifier
@ -24,6 +25,24 @@ class TextSnippet extends Modifier
$lengthMin = $params[0] ?? 200;
$lengthMax = $params[1] ?? 250;
if (is_array($value)) { // Dealing with bard text
$text = '';
foreach ($value as $v) {
if (isset($v['text'])) {
/** @var Value $t */
$t = $v['text'];
$spaced = str_replace('<p>', ' ', $t->value()); // Adds extra space between cut words
$text .= strip_tags($spaced) . ' ';
}
// No need to create a bunch of text if we're scraping most of it anyways
if (strlen($text) > $lengthMax) {
break;
}
}
$value = $text;
}
$firstSentenceEnd = strpos($value, '.', $lengthMin);
if ($firstSentenceEnd <= $lengthMax) {
return substr($value, 0, $firstSentenceEnd + 1);