From a7e81648b1e261737cc9371ea39a9a1c1910e48c Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Mon, 25 Aug 2025 19:39:12 +0100 Subject: [PATCH 1/2] Support passing code through as argument in Blade component --- src/Adapters/Laravel/Components/Code.php | 3 ++- .../it_can_accept_code_through_an_attribute.snap | 2 ++ tests/Adapters/Laravel/Components/CodeTest.php | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/.pest/snapshots/Adapters/Laravel/Components/CodeTest/it_can_accept_code_through_an_attribute.snap diff --git a/src/Adapters/Laravel/Components/Code.php b/src/Adapters/Laravel/Components/Code.php index c92cc21..6a6b763 100644 --- a/src/Adapters/Laravel/Components/Code.php +++ b/src/Adapters/Laravel/Components/Code.php @@ -14,6 +14,7 @@ class Code extends Component public function __construct( public string|Grammar $grammar, public string|array|Theme $theme, + public ?string $code = null, public bool $gutter = false, public int $startingLine = 1, ) {} @@ -24,7 +25,7 @@ public function __construct( public function render(): string { return <<<'BLADE' - {!! \Phiki\Adapters\Laravel\Facades\Phiki::codeToHtml($slot->__toString(), $grammar, $theme)->withGutter($gutter)->startingLine($startingLine) !!} + {!! \Phiki\Adapters\Laravel\Facades\Phiki::codeToHtml($code ?? $slot->__toString(), $grammar, $theme)->withGutter($gutter)->startingLine($startingLine) !!} BLADE; } } diff --git a/tests/.pest/snapshots/Adapters/Laravel/Components/CodeTest/it_can_accept_code_through_an_attribute.snap b/tests/.pest/snapshots/Adapters/Laravel/Components/CodeTest/it_can_accept_code_through_an_attribute.snap new file mode 100644 index 0000000..c381cb4 --- /dev/null +++ b/tests/.pest/snapshots/Adapters/Laravel/Components/CodeTest/it_can_accept_code_through_an_attribute.snap @@ -0,0 +1,2 @@ +
<?php echo 'Hello, world!';
+
\ No newline at end of file diff --git a/tests/Adapters/Laravel/Components/CodeTest.php b/tests/Adapters/Laravel/Components/CodeTest.php index 79b0d3b..bf3bad7 100644 --- a/tests/Adapters/Laravel/Components/CodeTest.php +++ b/tests/Adapters/Laravel/Components/CodeTest.php @@ -31,3 +31,13 @@ expect($output)->toContain('10'); }); + +it('can accept code through an attribute', function () { + $output = Blade::render(<<<'BLADE' + + BLADE, [ + 'code' => "toMatchSnapshot(); +}); From 6fe924a6cbd7983d2d19461f74f20b13ab68f8d4 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Mon, 25 Aug 2025 19:42:18 +0100 Subject: [PATCH 2/2] Add docs for new attribute --- docs/laravel.mdx | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/docs/laravel.mdx b/docs/laravel.mdx index 606c52d..721e27f 100644 --- a/docs/laravel.mdx +++ b/docs/laravel.mdx @@ -143,3 +143,45 @@ Str::markdown($markdown, extensions: [ new PhikiExtension(Theme::GithubLight, resolve(Phiki::class)), ]); ``` + +## Blade component + +If you want to highlight code inside of a Blade view, Phiki provides a Blade component that you can use. + +```blade + +echo "Hello, world!"; + +``` + +This will highlight the code block using the `Grammar::Php` grammar and the `Theme::GithubLight` theme. + +### Enabling the gutter + +To enable the gutter, add the `gutter` attribute to the component. + +```blade + +echo "Hello, world!"; + +``` + +### Changing the starting line number + +To change the starting line number, add the `starting-line` attribute to the component. + +```blade + +echo "Hello, world!"; + +``` + +### Passing code as an attribute + +Since Laravel trims whitespace from the slot of a Blade component, you can also pass the code to the component using the `code` attribute. + +```blade + +``` + +This is especially useful if you want to highlight code that is stored in a variable or preserve trailing and leading whitespace.