Skip to content

Commit c13a03d

Browse files
committed
feat: add @boostsnippet Blade directive for better guideline management
1 parent 1937c8e commit c13a03d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/BoostServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,22 @@ private function registerBrowserLogger(): void
166166
private function registerBladeDirectives(BladeCompiler $bladeCompiler): void
167167
{
168168
$bladeCompiler->directive('boostJs', fn () => '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>');
169+
170+
$bladeCompiler->directive('boostsnippet', function (string $expression) {
171+
preg_match('/^(.*?),\s*(.*?)$/', $expression, $matches);
172+
$name = isset($matches[1]) ? trim($matches[1], '\'"` ') : '';
173+
$lang = isset($matches[2]) ? trim($matches[2], '\'"` ') : 'html';
174+
$name = str_replace('___SINGLE_BACKTICK___', '`', $name);
175+
176+
return "<?php \$__snippetStack[] = ['name' => '{$name}', 'lang' => '{$lang}']; ob_start(); ?>";
177+
});
178+
179+
$bladeCompiler->directive('endboostsnippet', function () {
180+
return '<?php $__snippetContent = ob_get_clean(); $__snippet = array_pop($__snippetStack); ?>'
181+
.'<code-snippet name="<?php echo $__snippet[\'name\']; ?>" lang="<?php echo $__snippet[\'lang\']; ?>">'."\n"
182+
.'<?php echo $__snippetContent; ?>'
183+
.'</code-snippet>'."\n\n";
184+
});
169185
}
170186

171187
private static function mapJsTypeToPsr3Level(string $type): string

0 commit comments

Comments
 (0)