From 81b16772d741fc3c8665d96c9fc699f52c11476e Mon Sep 17 00:00:00 2001 From: kolaente Date: Sun, 13 Dec 2020 22:44:13 +0100 Subject: [PATCH] Fix handling bard text --- src/Modifiers/TextSnippet.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Modifiers/TextSnippet.php b/src/Modifiers/TextSnippet.php index 4c38ac1..d4c8ae8 100644 --- a/src/Modifiers/TextSnippet.php +++ b/src/Modifiers/TextSnippet.php @@ -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('

', ' ', $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);