Skip to content
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
42 changes: 42 additions & 0 deletions docs/laravel.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
<x-phiki::code grammar="php" theme="github-light">
echo "Hello, world!";
</x-phiki::code>
```

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
<x-phiki::code grammar="php" theme="github-light" gutter>
echo "Hello, world!";
</x-phiki::code>
```

### Changing the starting line number

To change the starting line number, add the `starting-line` attribute to the component.

```blade
<x-phiki::code grammar="php" theme="github-light" gutter :starting-line="5">
echo "Hello, world!";
</x-phiki::code>
```

### 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
<x-phiki::code grammar="php" theme="github-light" :code="$myCodeVariable" />
```

This is especially useful if you want to highlight code that is stored in a variable or preserve trailing and leading whitespace.
3 changes: 2 additions & 1 deletion src/Adapters/Laravel/Components/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre class="phiki language-php github-light" data-language="php" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">&lt;</span><span class="token" style="color: #d73a49;">?</span><span class="token" style="color: #005cc5;">php</span><span class="token"> </span><span class="token" style="color: #005cc5;">echo</span><span class="token"> </span><span class="token" style="color: #032f62;">&#039;</span><span class="token" style="color: #032f62;">Hello, world!</span><span class="token" style="color: #032f62;">&#039;</span><span class="token">;</span><span class="token">
</span></span></code></pre>
10 changes: 10 additions & 0 deletions tests/Adapters/Laravel/Components/CodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@

expect($output)->toContain('10</span>');
});

it('can accept code through an attribute', function () {
$output = Blade::render(<<<'BLADE'
<x-phiki::code grammar="php" theme="github-light" :$code />
BLADE, [
'code' => "<?php echo 'Hello, world!';",
]);

expect($output)->toMatchSnapshot();
});