Skip to content
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

Added markdown extensions support #14419

Open
wants to merge 9 commits into
base: 4.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div
class="fi-fo-markdown-editor fi-disabled prose block w-full max-w-none rounded-lg bg-gray-50 px-3 py-3 text-gray-500 shadow-sm ring-1 ring-gray-950/10 dark:prose-invert dark:bg-transparent dark:text-gray-400 dark:ring-white/10 sm:text-sm"
>
{!! str($getState())->markdown()->sanitizeHtml() !!}
{!! str($getState())->sanitizeHtml()->markdown($getCommonMarkOptions(), $getCommonMarkExtensions()) !!}
</div>
@else
<x-filament::input.wrapper
Expand Down
2 changes: 2 additions & 0 deletions packages/forms/src/Components/MarkdownEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Filament\Forms\Components;

use Closure;
use Filament\Support\Concerns\CanConfigureCommonMark;
use Filament\Support\Concerns\HasExtraAlpineAttributes;

class MarkdownEditor extends Field implements Contracts\CanBeLengthConstrained, Contracts\HasFileAttachments
{
use CanConfigureCommonMark;
use Concerns\CanBeLengthConstrained;
use Concerns\HasFileAttachments;
use Concerns\HasMaxHeight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Schema;
use Filament\Support\Concerns\CanConfigureCommonMark;
use Filament\Support\Contracts\HasLabel as LabelInterface;
use Filament\Support\Enums\ArgumentValue;
use Illuminate\Contracts\Support\Htmlable;
Expand All @@ -15,6 +16,8 @@

trait CanFormatState
{
use CanConfigureCommonMark;

protected ?Closure $formatStateUsing = null;

protected int | Closure | null $characterLimit = null;
Expand Down Expand Up @@ -304,7 +307,7 @@ public function formatState(mixed $state): mixed
}

if ($isHtml && $this->isMarkdown()) {
$state = Str::markdown($state);
$state = Str::markdown($state, $this->getCommonMarkOptions(), $this->getCommonMarkExtensions());
}

$prefix = $this->getPrefix();
Expand Down
55 changes: 55 additions & 0 deletions packages/support/src/Concerns/CanConfigureCommonMark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Filament\Support\Concerns;

use Closure;
use League\CommonMark\Extension\ExtensionInterface;

trait CanConfigureCommonMark
{
/**
* @var array<string, mixed> | Closure | null
*/
protected array | Closure | null $commonMarkOptions = null;

/**
* @var array<array-key, ExtensionInterface> | Closure | null
*/
protected array | Closure | null $commonMarkExtensions = null;

/**
* @param array<string, mixed> | Closure | null $commonMarkOptions
*/
public function commonMarkOptions(array | Closure | null $commonMarkOptions): static
{
$this->commonMarkOptions = $commonMarkOptions;

return $this;
}

/**
* @return array<string, mixed>
*/
public function getCommonMarkOptions(): array
{
return $this->evaluate($this->commonMarkOptions) ?? [];
}

/**
* @param array<array-key, ExtensionInterface> | Closure | null $commonMarkExtensions
*/
public function commonMarkExtensions(array | Closure | null $commonMarkExtensions): static
{
$this->commonMarkExtensions = $commonMarkExtensions;

return $this;
}

/**
* @return array<array-key, ExtensionInterface>
*/
public function getCommonMarkExtensions(): array
{
return $this->evaluate($this->commonMarkExtensions) ?? [];
}
}
5 changes: 4 additions & 1 deletion packages/tables/src/Columns/Concerns/CanFormatState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Columns\Concerns;

use Closure;
use Filament\Support\Concerns\CanConfigureCommonMark;
use Filament\Support\Contracts\HasLabel as LabelInterface;
use Filament\Support\Enums\ArgumentValue;
use Filament\Tables\Columns\TextColumn;
Expand All @@ -15,6 +16,8 @@

trait CanFormatState
{
use CanConfigureCommonMark;

protected ?Closure $formatStateUsing = null;

protected int | Closure | null $characterLimit = null;
Expand Down Expand Up @@ -308,7 +311,7 @@ public function formatState(mixed $state): mixed
}

if ($isHtml && $this->isMarkdown()) {
$state = Str::markdown($state);
$state = Str::markdown($state, $this->getCommonMarkOptions(), $this->getCommonMarkExtensions());
}

$prefix = $this->getPrefix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Filament\Tables\Columns\Summarizers\Concerns;

use Closure;
use Filament\Support\Concerns\CanConfigureCommonMark;
use Filament\Support\Enums\ArgumentValue;
use Filament\Tables\Columns\Summarizers\Summarizer;
use Filament\Tables\Table;
Expand All @@ -13,6 +14,8 @@

trait CanFormatState
{
use CanConfigureCommonMark;

protected ?Closure $formatStateUsing = null;

protected string | Closure | null $placeholder = null;
Expand Down Expand Up @@ -157,7 +160,7 @@ public function formatState(mixed $state): mixed
}

if ($isHtml && $this->isMarkdown()) {
$state = Str::markdown($state);
$state = Str::markdown($state, $this->getCommonMarkOptions(), $this->getCommonMarkExtensions());
}

$prefix = $this->getPrefix();
Expand Down
Loading