1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 05:36:54 +02:00

feat(shortcodes): nested shortcodes implementation for strings

This commit is contained in:
Awilum
2022-05-25 15:44:57 +03:00
parent a37c99ca36
commit 6face7f82b
2 changed files with 12 additions and 4 deletions

View File

@@ -20,19 +20,23 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use function parsers;
use function registry;
// Shortcode: [strings] strings to modify [/strings]
// Shortcode: strings
// Usage: (strings) strings to modify (/strings)
parsers()->shortcodes()->addHandler('strings', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.strings.enabled')) {
return '';
}
$content = $s->getContent();
$varsDelimeter = $s->getParameter('varsDelimeter') ?: ',';
$content = ($s->getContent() != null) ? parsers()->shortcodes()->parse($s->getContent()) : '';
$varsDelimeter = ($s->getParameter('varsDelimeter') != null) ? parsers()->shortcodes()->parse($s->getParameter('varsDelimeter')) : ',';
foreach($s->getParameters() as $key => $value) {
$vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : [];
// Parse shortcodes for each var.
$vars = array_map(fn($v) => parsers()->shortcodes()->parse(is_string($v) ? $v : ''), $vars);
if ($key == 'append') {
if (is_iterable($vars)) {
$content = strings($content)->{'append'}(...$vars)->toString();

View File

@@ -2,7 +2,7 @@
declare(strict_types=1);
test('(strings) shortcode', function () {
test('strings shortcode', function () {
// lower
$this->assertEquals("zed foo bar", parsers()->shortcodes()->parse("(strings lower)zed foo bar(/strings)"));
@@ -340,4 +340,8 @@ test('(strings) shortcode', function () {
// wordsFrequency
$this->assertEquals('{"foo":"33.33","bar":"33.33","baz":"33.33"}', parsers()->shortcodes()->parse("(strings wordsFrequency)foo bar baz(/strings)"));
});
test('strings nested shortcode', function () {
expect(parsers()->shortcodes()->parse("(strings append:'(strings hash)(strings random:10 /)(/strings)')Hash: (/strings)"))->toBe('Hash: 66e51cf9114bf1eef8f1793ad1c0e8a1');
});