Skip to content

Commit

Permalink
default to opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 15, 2024
1 parent 5e74ba9 commit 9d6b14b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
4 changes: 2 additions & 2 deletions config/dotoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

return [
'enabled' => env('DOTOO_ENABLED', app()->isLocal()),
'open' => 'TODO:',
'close' => 'ENDTODO',
'open' => '|TODO',
'close' => '|ENDTODO',
];
33 changes: 10 additions & 23 deletions src/CommentsPrecompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static function execute(string $view): string
protected static function compileTodosMark($view, $openComment, $closeComment)
{
// Regular expression to match TODO comments
$keyword = config('dotoo.open');
$keyword = preg_quote(config('dotoo.open'));
$pattern = "/{$openComment}\s*{$keyword}(.+?){$closeComment}/s";

// Find all matches
Expand All @@ -27,7 +27,7 @@ protected static function compileTodosMark($view, $openComment, $closeComment)
// Process each match
foreach ($matches as $match) {
$fullComment = $match[0]; // Full comment including {{-- and --}}
$commentBody = trim($match[1]); // Comment body without {{-- TODO: and --}}
$commentBody = self::trimColons($match[1]); // Comment body without {{-- TODO: and --}}

$dotooComponent = <<< BLADE
<x-dotoo::highlight todo="{$commentBody}" />
Expand All @@ -42,13 +42,8 @@ protected static function compileTodosMark($view, $openComment, $closeComment)

protected static function compileWrappedBladeTodos($view, $openComment, $closeComment)
{
$openKeyword = config('dotoo.open');
$closeKeyword = config('dotoo.close');

// (?:{$openComment}\s*{$openKeyword}\s*.*?{$closeComment}\s*)* # Non-capturing group to skip any previous TODO comments
// {$openComment}\s*{$openKeyword}\s*(.*?)\s*{$closeComment} # Capture the content of the relevant TODO comment
// ((?:(?!<!--\s*{$openKeyword}).)*?) # Capture content up to ENDTODO, non-greedy
// {$openComment}\s*{$closeKeyword}\s*{$closeComment} # Match ENDTODO comment
$openKeyword = preg_quote(config('dotoo.open'));
$closeKeyword = preg_quote(config('dotoo.close'));

// Regular expression to match content between TODO and ENDTODO comments
$pattern = "/
Expand All @@ -63,8 +58,9 @@ protected static function compileWrappedBladeTodos($view, $openComment, $closeCo

// Process each match
foreach ($matches as $match) {

$todoBlock = trim($match[1]); // Everything between TODO & ENDTODO including the wrapping comments
$todoComment = trim($match[2]); // Just the text inside the TODO comment
$todoComment = self::trimColons($match[2]); // Just the text inside the TODO comment
$content = trim($match[3]); // The content between TODO and ENDTODO

$dotooComponent = <<< BLADE
Expand All @@ -78,19 +74,10 @@ protected static function compileWrappedBladeTodos($view, $openComment, $closeCo
}

return $view;
}

// // Replace the matched content
// $replacement = function ($matches) {
// $todoComment = trim($matches[1]); // Just the text inside the TODO comment
// $content = trim($matches[2]); // The content between TODO and ENDTODO

// return <<< BLADE
// <x-dotoo::highlight todo="{$todoComment}">
// {$content}
// </x-dotoo:highlight>
// BLADE;
// };

// return preg_replace_callback($pattern, $replacement, $view);
private static function trimColons($string)
{
return preg_replace('/^[:\s]+|[:\s]+$/u', '', $string);
}
}
20 changes: 14 additions & 6 deletions tests/Feature/BladeCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Illuminate\Support\Facades\Blade;

it('compiles Blade TODO comments', function () {
$html = Blade::render('{{-- TODO: Foo Bar baz --}}');
$html = Blade::render('{{-- |TODO: Foo Bar baz --}}');

expect($html)
->not->toContain('{{-- TODO')
Expand All @@ -13,7 +13,7 @@
it('compiles multiline Blade TODO comments', function () {
$html = Blade::render(<<< 'BLADE'
{{--
TODO: Foo Bar baz
|TODO: Foo Bar baz
Dipsum dolor sit amet
--}}
BLADE);
Expand All @@ -28,7 +28,7 @@

it('compiles empty Blade TODO comments', function () {
$html = Blade::render(<<< 'BLADE'
{{-- TODO: --}}
{{-- |TODO: --}}
BLADE);

expect($html)
Expand All @@ -40,9 +40,9 @@

it('compiles wrappped Blade TODO comments', function () {
$html = Blade::render(<<< 'BLADE'
{{-- TODO: Fooz --}}
{{-- |TODO: Fooz --}}
this should be wrapped
{{-- ENDTODO --}}
{{-- |ENDTODO --}}
BLADE);

expect($html)
Expand All @@ -57,7 +57,7 @@
it('keeps surrounding html intact', function () {
$html = Blade::render(<<< 'BLADE'
Foozbal
{{-- TODO: --}}
{{-- |TODO: --}}
Guacamole
BLADE);

Expand All @@ -71,3 +71,11 @@
Guacamole
HTML);
});

it('trims : from tooltip', function () {
$html = Blade::render('{{-- |TODO:: Foo Bar baz : --}}');

expect($html)
->not->toContain('{{-- TODO')
->toContain('<span class="dotoo-mark" data-todo="Foo Bar baz">');
});
20 changes: 14 additions & 6 deletions tests/Feature/HtmlCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Illuminate\Support\Facades\Blade;

it('compiles HTML TODO comments', function () {
$html = Blade::render('<!-- TODO: Foo Bar baz -->');
$html = Blade::render('<!-- |TODO: Foo Bar baz -->');

expect($html)
->not->toContain('<!-- TODO')
Expand All @@ -13,7 +13,7 @@
it('compiles multiline HTML TODO comments', function () {
$html = Blade::render(<<< 'BLADE'
<!--
TODO: Foo Bar baz
|TODO: Foo Bar baz
Dipsum dolor sit amet
-->
BLADE);
Expand All @@ -27,7 +27,7 @@
});

it('compiles empty HTML TODO comments', function () {
$html = Blade::render('<!-- TODO: -->');
$html = Blade::render('<!-- |TODO: -->');

expect($html)
->not->toContain('<!-- TODO')
Expand All @@ -38,9 +38,9 @@

it('compiles wrappped HTML TODO comments', function () {
$html = Blade::render(<<< 'BLADE'
<!-- TODO: Fooz -->
<!-- |TODO: Fooz -->
this should be wrapped
<!-- ENDTODO -->
<!-- |ENDTODO -->
BLADE);

expect($html)
Expand All @@ -55,7 +55,7 @@
it('keeps surrounding html intact', function () {
$html = Blade::render(<<< 'BLADE'
Foozbal
<!-- TODO: -->
<!-- |TODO: -->
Guacamole
BLADE);

Expand All @@ -69,3 +69,11 @@
Guacamole
HTML);
});

it('trims : from tooltip', function () {
$html = Blade::render('<!-- |TODO:: Foo Bar baz : -->');

expect($html)
->not->toContain('<!-- TODO')
->toContain('<span class="dotoo-mark" data-todo="Foo Bar baz">');
});
5 changes: 0 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@
abstract class TestCase extends BaseTestCase
{
use WithWorkbench;

protected function setUp(): void
{
parent::setUp();
}
}

0 comments on commit 9d6b14b

Please sign in to comment.