Skip to content

Commit d4dd77a

Browse files
authored
Merge pull request #11 from The-3Labs-Team/add-after/before-blacklist
Add after/before blacklist
2 parents 5c86a29 + 376bdbd commit d4dd77a

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ php artisan vendor:publish --tag="laravel-ads-post-parser-views"
3535

3636
## Usage
3737

38+
You must encapsulate your post content in a `div` tag with the id `adv__parsed__content`:
39+
40+
```html
41+
<div id="adv__parsed__content">
42+
{!! $parsedContent !!}
43+
</div>
44+
```
45+
46+
Then you can use the `AdsPostParser` class to append advertising to your post:
47+
3848
```php
3949
$parsedContent = (new AdsPostParser($content))->appendAdvertising();
4050
```

config/ads-post-parser.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* Blacklist to avoid adding advertising in some elements
2323
* The blacklist is a regex
2424
* Example:
25-
* '/\[shortcode|\[image|\[product|\[widget|\[twitter|\[facebook]|\[youtube|\[gallery|\[media|<img|<quote|<h1|<h2|<h3|<h4|<h5/'
25+
* '/<img|<quote|<h1|<h2|<h3|<h4|<h5/'
2626
*/
27-
'blacklist' => '/\[shortcode|\[image|\[product|\[widget|\[twitter|\[facebook|\[instagram|\[youtube|\[gallery|\[media|\[video|\[quiz|\[challenge|\[spotify|\[faq|\[movie|\[tv|\[spoiler|<img|<quote|<h1|<h2|<h3|<h4|<h5/',
27+
'blacklist' => [
28+
'before' => '/<iframe|<img|<h2|<h3|<ul|<li|<div/',
29+
'after' => '/<iframe|<img|<ul|<li|<table|<div/',
30+
],
2831
];

src/AdsPostParser.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
class AdsPostParser
99
{
10-
public string $blacklist;
10+
public string $blacklistBefore;
11+
12+
public string $blacklistAfter;
1113

1214
public $dom;
1315

@@ -18,7 +20,8 @@ class AdsPostParser
1820
*/
1921
public function __construct(string $content)
2022
{
21-
$this->blacklist = config('ads-post-parser.blacklist');
23+
$this->blacklistBefore = config('ads-post-parser.blacklist.before');
24+
$this->blacklistAfter = config('ads-post-parser.blacklist.after');
2225
$this->dom = HtmlDomParser::str_get_html("<div id='adv__parsed__content'>$content</div>");
2326
$this->content = $content;
2427
}
@@ -52,29 +55,18 @@ public function appendSingleAdvertising(int $index, int $advIndex): string
5255
return $this->dom->save();
5356
}
5457

55-
$currentItem = $items[$index];
58+
$beforeItem = $items[$index];
5659
$nextItem = $index < $maxLoop - 1 ? $items[$index + 1] : null;
5760

5861
if (
59-
! preg_match($this->blacklist, $currentItem->outertext)
60-
&& ($nextItem === null || ! preg_match($this->blacklist, $nextItem->outertext))
62+
! preg_match($this->blacklistBefore, $beforeItem->outertext)
63+
&& ($nextItem === null || ! preg_match($this->blacklistAfter, $nextItem->outertext))
6164
) {
62-
$currentItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
65+
$beforeItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
6366
} else {
6467
$this->appendSingleAdvertising($index + 1, $advIndex);
6568
}
6669

6770
return $this->dom->save();
6871
}
69-
70-
/**
71-
* Remove the wrapping div
72-
*/
73-
public function removeWrappingDiv(): string
74-
{
75-
$contentDiv = $this->dom->find('#adv__parsed__content', 0);
76-
$html = $contentDiv->save();
77-
78-
return $html;
79-
}
8072
}

tests/AdsPostParserTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
expect($content)->toContain('YOUR AD1 HERE');
3939
});
4040

41-
//it('can remove wrapping div', function () {
42-
// $content = (new AdsPostParser($this->content))->appendAdvertising()->removeWrappingDiv();
43-
// expect($content)->not()->toContain('<div id="adv__parsed__content">');
44-
//});
41+
it('can append single advertising with index greater than the number of paragraphs', function () {
42+
$content = (new AdsPostParser($this->content))
43+
->appendSingleAdvertising(100, 1);
44+
expect($content)->not()->toContain('YOUR AD1 HERE');
45+
});

0 commit comments

Comments
 (0)