Skip to content
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

refactor: ignore markdown on metadata #287

Merged
merged 9 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
17 changes: 16 additions & 1 deletion app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Spatie\Image\Manipulations;
use Spatie\LaravelMarkdown\MarkdownRenderer;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\Sluggable\HasSlug;
Expand Down Expand Up @@ -70,6 +72,11 @@ public function registerMediaCollections(): void
});
}

public function renderedMarkdown(): string
{
return app(MarkdownRenderer::class)->toHtml($this->content);
}

/**
* @return BelongsToMany<Collection>
*/
Expand Down Expand Up @@ -148,7 +155,15 @@ public function scopeWithFeaturedCollections(Builder $query): Builder

public function metaDescription(): string
{
return $this->meta_description ?? Str::limit(strip_tags($this->content), 157);
return Cache::rememberForever("article:{$this->id}:meta_description", function () {
if (boolval($this->meta_description)) {
return $this->meta_description;
}

$rawContent = $this->renderedMarkdown();

return Str::limit(trim(preg_replace("/\r?\n/", ' ', html_entity_decode(strip_tags($rawContent)))), 157);
});
}

public function isNotPublished(): bool
Expand Down
34 changes: 34 additions & 0 deletions app/Observers/ArticleObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Observers;

use App\Models\Article;
use Illuminate\Support\Facades\Cache;

class ArticleObserver
{
public function created(Article $article): void
{
$this->forgeMetaDescriptionCache($article);
}

public function updated(Article $article): void
{
$this->forgeMetaDescriptionCache($article);
}

private function forgeMetaDescriptionCache(Article $article): void
alfonsobries marked this conversation as resolved.
Show resolved Hide resolved
{
if ($article->isNotPublished()) {
return;
}

if (! ($article->wasRecentlyCreated || $article->wasChanged(['meta_description', 'content', 'is_published']))) {
return;
}

Cache::forget("article:{$article->id}:meta_description");
}
}
3 changes: 3 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

use App\Events\WalletBecameActive;
use App\Listeners\WalletBecameActiveListener;
use App\Models\Article;
use App\Models\CoingeckoToken;
use App\Models\SpamToken;
use App\Models\Token;
use App\Observers\ArticleObserver;
use App\Observers\CoingeckoTokenObserver;
use App\Observers\SpamTokenObserver;
use App\Observers\TokenObserver;
Expand Down Expand Up @@ -39,6 +41,7 @@ public function boot()
Token::observe(TokenObserver::class);
CoingeckoToken::observe(CoingeckoTokenObserver::class);
SpamToken::observe(SpamTokenObserver::class);
Article::observe(ArticleObserver::class);
}

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"simplito/elliptic-php": "^1.0",
"spatie/browsershot": "^3.59",
"spatie/laravel-data": "^3.9",
"spatie/laravel-markdown": "^2.4",
"spatie/laravel-medialibrary": "^10.13",
"spatie/laravel-permission": "^5.11",
"spatie/laravel-schemaless-attributes": "^2.4",
Expand Down
201 changes: 200 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/App/Models/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Article;
use App\Models\Collection;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

it('should create an article', function () {
Expand Down Expand Up @@ -49,6 +50,25 @@
expect($article->metaDescription())->toBe('This is the content');
});

it('removes markdown in meta description', function () {
$article = Article::factory()->create([
'meta_description' => null,
'content' => "### This is the content\n\nwith some *markdown*\n",
]);

expect($article->metaDescription())->toBe('This is the content with some markdown');
});

it('gets meta description from cache', function () {
$article = Article::factory()->create();

Cache::shouldReceive('rememberForever')
->with('article:'.$article->id.':meta_description', Closure::class)
->andReturn('This is the content');

expect($article->metaDescription())->toBe('This is the content');
});

it('truncates the content if used as meta description', function () {
$article = Article::factory()->create([
'meta_description' => null,
Expand Down
Loading
Loading