Skip to content

Commit acde7e7

Browse files
Add support and document custom aliases (#119)
1 parent 461f6dd commit acde7e7

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

docs/custom-grammars.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,16 @@ $html = (new Phiki)
4949
->codeToHtml('Your code here...', 'my-language', Theme::GithubLight)
5050
->toString();
5151
```
52+
53+
## Aliasing
54+
55+
If you wish to register custom aliases for a grammar that has already been registered, you can do so by calling the `Phiki::alias()` method.
56+
57+
```php
58+
(new Phiki)
59+
->alias('my-php', Grammar::Php)
60+
->codeToHtml('<?php echo "Hello, World!"; ?>', 'my-php', Theme::GithubLight)
61+
->toString();
62+
```
63+
64+
This will allow you to use `my-php` as an alias for the built-in PHP grammar.

src/Adapters/Laravel/Facades/Phiki.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @method static \Phiki\Environment environment()
1313
* @method static \Phiki\Phiki extend(\Phiki\Contracts\ExtensionInterface $extension)
1414
* @method static \Phiki\Phiki grammar(string $name, string|\Phiki\Grammar\ParsedGrammar $pathOrGrammar)
15+
* @method static \Phiki\Phiki alias(string $alias, string | \Phiki\Grammar\Grammar $for)
1516
* @method static \Phiki\Phiki theme(string $name, string|\Phiki\Theme\ParsedTheme $pathOrTheme)
1617
* @method static \Phiki\Phiki cache(\Psr\SimpleCache\CacheInterface $cache)
1718
*

src/Phiki.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public function grammar(string $name, string|ParsedGrammar $pathOrGrammar): stat
8383
return $this;
8484
}
8585

86+
public function alias(string $alias, string | Grammar $for): static
87+
{
88+
$this->environment->grammars->alias($alias, $for instanceof Grammar ? $for->value : $for);
89+
90+
return $this;
91+
}
92+
8693
public function theme(string $name, string|ParsedTheme $pathOrTheme): static
8794
{
8895
$this->environment->themes->register($name, $pathOrTheme);

tests/Unit/PhikiTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ function add(int|float $a, int|float $b): int|float {
5555
it('accepts a theme enum member', function () {
5656
expect((new Phiki)->codeToHtml('echo $a;', Grammar::Php, Theme::GithubDark))->__toString()->toBeString();
5757
});
58+
59+
it('can alias a grammar', function () {
60+
$phiki = (new Phiki)->alias('my-php', Grammar::Php);
61+
62+
expect($phiki->codeToHtml('echo $a;', 'my-php', Theme::GithubDark))->__toString()->toBeString();
63+
});

0 commit comments

Comments
 (0)