Skip to content

Commit 66bedb5

Browse files
authored
Merge pull request #37 from wvdongen/main
Add config option to exclude sections
2 parents 3aff082 + d4dcdee commit 66bedb5

13 files changed

+86
-35
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,18 @@ return [
9090
*/
9191
'includes' => [
9292

93-
]
93+
],
94+
95+
/**
96+
* Add sections you don't want to be affected by the package here.
97+
* These sections will not have HTML comments added around @yield directives
98+
* For example:
99+
* 'header',
100+
* 'message',
101+
*/
102+
'sections' => [
103+
104+
],
94105
]
95106
];
96107
```
@@ -103,6 +114,10 @@ After the package is installed, you'll immediately see that HTML comments are in
103114
Sometimes you might not want to have an HTML comment being wrapped around an include. For example when you use a partial to add some CSS to a page.
104115
In these cases you can add views to the `excludes.includes` array in the config file.
105116

117+
## Excluding sections
118+
Sometimes you might not want HTML comments wrapping around `@yield` directives. For example when they're used within HTML attributes or meta tags.
119+
In these cases you can add sections to the `excludes.sections` array in the config file.
120+
106121
### Using your own Blade Commenters
107122

108123
You can easily extend the package to add more comments. In the `blade_commenters` key of the `blade_commenters` config file, you can add your own `BladeCommenter`. A `BladeCommenter` is any class that implements the following interface:

config/blade-comments.php

+11
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,16 @@
5252
'includes' => [
5353

5454
],
55+
56+
/**
57+
* Add sections you don't want to be affected by the package here.
58+
* These sections will not have HTML comments added around @yield directives
59+
* For example:
60+
* 'header',
61+
* 'message',
62+
*/
63+
'sections' => [
64+
65+
],
5566
],
5667
];

src/Commenters/BladeCommenters/SectionCommenter.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@ class SectionCommenter implements BladeCommenterWithCallback
66
{
77
public function pattern(): string
88
{
9-
return "/@yield(\((?:[^)(]+|(?1))*+\))(?![^<>]*<\/title>)/";
9+
$excludes = config('blade-comments.excludes.sections', []);
10+
11+
if (count($excludes)) {
12+
$excludesRegex = '(?!\s*[\'"](?:' . implode('|', $excludes) . ')[\'"])';
13+
$regex = "/@yield(\({$excludesRegex}(?:[^)(]+|(?1))*+\))(?![^<>]*<\/title>)/";
14+
} else {
15+
$regex = "/@yield(\((?:[^)(]+|(?1))*+\))(?![^<>]*<\/title>)/";
16+
}
17+
18+
return $regex;
1019
}
1120

1221
public function replacementCallback(array $matches): string

tests/PrecompilerTests/BladeExtendsTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,19 @@
77

88
assertMatchesHtmlSnapshot($renderedView);
99
});
10+
11+
it('should filter excluded sections', function () {
12+
config(['blade-comments.excludes.sections' => ['excluded-section']]);
13+
$renderedView = view('extends.page')->render();
14+
15+
assertMatchesHtmlSnapshot($renderedView);
16+
expect($renderedView)->not->toContain('<!-- Start section: excluded-section -->');
17+
});
18+
19+
it('will add comments for extends with invalid excluded sections', function () {
20+
config(['blade-comments.excludes.sections' => ['exclude-section-that-does-not-exist']]);
21+
$renderedView = view('extends.page')->render();
22+
23+
assertMatchesHtmlSnapshot($renderedView);
24+
expect($renderedView)->toContain('<!-- Start section: excluded-section -->');
25+
});

tests/TestSupport/views/extends/layout.blade.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
<body>
66
<h1>@yield('title', config('app.name'))</h1>
77
@yield('content')
8+
@yield('excluded-section')
89
</body>
910
</html>

tests/TestSupport/views/extends/page.blade.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
@section('content')
33
This is the content
44
@endsection
5+
@section('excluded-section')
6+
Depending on the config, this section should not be wrapped with a comment.
7+
@endsection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- View extends: extends.layout --><html>
2+
<head>
3+
<title>Laravel</title>
4+
</head>
5+
<body>
6+
<h1>
7+
<!-- Start section: title -->Laravel<!-- End section: title -->
8+
</h1>
9+
<!-- Start section: content --> This is the content
10+
<!-- End section: content -->
11+
Depending on the config, this section should not be wrapped with a comment.
12+
</body>
13+
</html>

tests/__snapshots__/BladeExtendsTest__it_will_add_comments_for_extends__1.html

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ <h1>
88
</h1>
99
<!-- Start section: content --> This is the content
1010
<!-- End section: content -->
11+
<!-- Start section: excluded-section --> Depending on the config, this section should not be wrapped with a comment.
12+
<!-- End section: excluded-section -->
1113
</body>
1214
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!-- View extends: extends.layout --><html>
2+
<head>
3+
<title>Laravel</title>
4+
</head>
5+
<body>
6+
<h1>
7+
<!-- Start section: title -->Laravel<!-- End section: title -->
8+
</h1>
9+
<!-- Start section: content --> This is the content
10+
<!-- End section: content -->
11+
<!-- Start section: excluded-section --> Depending on the config, this section should not be wrapped with a comment.
12+
<!-- End section: excluded-section -->
13+
</body>
14+
</html>

tests/__snapshots__/BladeIncludeTest__it_should_filter_blacklisted_includeIfs__1.html

-8
This file was deleted.

tests/__snapshots__/BladeIncludeTest__it_should_filter_blacklisted_includeUnless__1.html

-9
This file was deleted.

tests/__snapshots__/BladeIncludeTest__it_should_filter_blacklisted_includeWhens__1.html

-8
This file was deleted.

tests/__snapshots__/BladeIncludeTest__it_should_filter_blacklisted_includes__1.html

-8
This file was deleted.

0 commit comments

Comments
 (0)