Skip to content

Add after/before blacklist #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ php artisan vendor:publish --tag="laravel-ads-post-parser-views"

## Usage

You must encapsulate your post content in a `div` tag with the id `adv__parsed__content`:

```html
<div id="adv__parsed__content">
{!! $parsedContent !!}
</div>
```

Then you can use the `AdsPostParser` class to append advertising to your post:

```php
$parsedContent = (new AdsPostParser($content))->appendAdvertising();
```
Expand Down
7 changes: 5 additions & 2 deletions config/ads-post-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
* Blacklist to avoid adding advertising in some elements
* The blacklist is a regex
* Example:
* '/\[shortcode|\[image|\[product|\[widget|\[twitter|\[facebook]|\[youtube|\[gallery|\[media|<img|<quote|<h1|<h2|<h3|<h4|<h5/'
* '/<img|<quote|<h1|<h2|<h3|<h4|<h5/'
*/
'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/',
'blacklist' => [
'before' => '/<iframe|<img|<h2|<h3|<ul|<li|<div/',
'after' => '/<iframe|<img|<ul|<li|<table|<div/',
],
];
26 changes: 9 additions & 17 deletions src/AdsPostParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

class AdsPostParser
{
public string $blacklist;
public string $blacklistBefore;

public string $blacklistAfter;

public $dom;

Expand All @@ -18,7 +20,8 @@ class AdsPostParser
*/
public function __construct(string $content)
{
$this->blacklist = config('ads-post-parser.blacklist');
$this->blacklistBefore = config('ads-post-parser.blacklist.before');
$this->blacklistAfter = config('ads-post-parser.blacklist.after');
$this->dom = HtmlDomParser::str_get_html("<div id='adv__parsed__content'>$content</div>");
$this->content = $content;
}
Expand Down Expand Up @@ -52,29 +55,18 @@ public function appendSingleAdvertising(int $index, int $advIndex): string
return $this->dom->save();
}

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

if (
! preg_match($this->blacklist, $currentItem->outertext)
&& ($nextItem === null || ! preg_match($this->blacklist, $nextItem->outertext))
! preg_match($this->blacklistBefore, $beforeItem->outertext)
&& ($nextItem === null || ! preg_match($this->blacklistAfter, $nextItem->outertext))
) {
$currentItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
$beforeItem->outertext .= Blade::render('ads-post-parser::ads'.$advIndex);
} else {
$this->appendSingleAdvertising($index + 1, $advIndex);
}

return $this->dom->save();
}

/**
* Remove the wrapping div
*/
public function removeWrappingDiv(): string
{
$contentDiv = $this->dom->find('#adv__parsed__content', 0);
$html = $contentDiv->save();

return $html;
}
}
9 changes: 5 additions & 4 deletions tests/AdsPostParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
expect($content)->toContain('YOUR AD1 HERE');
});

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