Skip to content

[5.x] Render markdown after antlers when smartypants is enabled #11814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
Merged
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
5 changes: 5 additions & 0 deletions src/Fields/Fieldtype.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,9 @@ public function extraRenderableFieldData(): array
{
return [];
}

public function shouldParseAntlersFromRawString(): bool
{
return false;
}
}
21 changes: 17 additions & 4 deletions src/Fields/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,14 @@ public function value()
$raw = $this->fieldtype->field()?->defaultValue() ?? null;
}

$value = $this->shallow
return $this->getAugmentedValue($raw);
}

private function getAugmentedValue($raw)
{
return $this->shallow
? $this->fieldtype->shallowAugment($raw)
: $this->fieldtype->augment($raw);

return $value;
}

private function iteratorValue()
Expand Down Expand Up @@ -150,9 +153,19 @@ public function antlersValue(Parser $parser, $variables)
}

if ($shouldParseAntlers) {
if ($parseFromRawString = $this->fieldtype?->shouldParseAntlersFromRawString()) {
$value = $this->raw();
}

$value = (new DocumentTransformer())->correct($value);

return $parser->parse($value, $variables);
$parsed = $parser->parse($value, $variables);

if (! $parseFromRawString) {
return $parsed;
}

return $this->getAugmentedValue($parsed);
}

if (Str::contains($value, '{')) {
Expand Down
5 changes: 5 additions & 0 deletions src/Fieldtypes/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,9 @@ public function preload()
'previewUrl' => cp_route('markdown.preview'),
];
}

public function shouldParseAntlersFromRawString(): bool
{
return $this->config('smartypants', false);
}
}
38 changes: 38 additions & 0 deletions tests/Fields/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Statamic\Fields\Value;
use Statamic\Fields\Values;
use Statamic\Query\Builder;
use Statamic\View\Antlers\AntlersString;
use Tests\TestCase;

class ValueTest extends TestCase
Expand Down Expand Up @@ -387,6 +388,43 @@ public function it_can_proxy_property_access_to_value()
$this->assertEquals('foo', $value->bar);
$this->assertEquals('nope', $value->baz ?? 'nope');
}

#[Test]
public function it_parses_from_raw_string()
{
$fieldtype = new class extends Fieldtype
{
public function augment($data)
{
// if we are being asked to augment an already parsed antlers string
// then we return the correct value
if ($data instanceof AntlersString) {
return 'augmented_value';
}

return 'not_augmented_value';
}

public function config(?string $key = null, $fallback = null)
{
if ($key == 'antlers') {
return true;
}

return parent::config($key, $fallback);
}

public function shouldParseAntlersFromRawString(): bool
{
return true;
}
};

$value = new Value('raw_value', null, $fieldtype);
$value = $value->antlersValue(app(\Statamic\Contracts\View\Antlers\Parser::class), []);

$this->assertEquals('augmented_value', (string) $value);
}
}

class DummyAugmentable implements \Statamic\Contracts\Data\Augmentable
Expand Down
19 changes: 19 additions & 0 deletions tests/Fieldtypes/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades;
use Statamic\Fields\Field;
use Statamic\Fields\Value;
use Statamic\Fieldtypes\Markdown;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
Expand Down Expand Up @@ -170,6 +171,24 @@ public function it_converts_statamic_asset_urls()
$this->assertEquals($expected, $this->fieldtype()->augment($markdown));
}

#[Test]
public function it_converts_to_smartypants_after_antlers_is_parsed()
{
$md = $this->fieldtype(['smartypants' => true, 'antlers' => true]);

$value = <<<'EOT'
{{ "this is a string" | replace(" is ", " isnt ") | reverse }}
EOT;

$value = new Value($value, 'markdown', $md);

$expected = <<<'EOT'
<p>gnirts a tnsi siht</p>
EOT;

$this->assertEqualsTrimmed($expected, $value->antlersValue(app(\Statamic\Contracts\View\Antlers\Parser::class), []));
}

private function fieldtype($config = [])
{
return (new Markdown)->setField(new Field('test', array_merge(['type' => 'markdown'], $config)));
Expand Down