From af80e76365005ba38aa9d4cc22ae3fec84d0bc1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Parient=C3=A9?= <41431456+arthurpar06@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:44:20 +0100 Subject: [PATCH] Use Markdown in discord notifications (#1743) * Add league/html-to-markdown * Convert HTML to Markdown in NewsAdded notification * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot Co-authored-by: Nabeel S --- .../Messages/Broadcast/NewsAdded.php | 5 +- composer.json | 3 +- composer.lock | 93 ++++++++++++++++++- 3 files changed, 97 insertions(+), 4 deletions(-) diff --git a/app/Notifications/Messages/Broadcast/NewsAdded.php b/app/Notifications/Messages/Broadcast/NewsAdded.php index 1f96bfe2..021ffaa7 100644 --- a/app/Notifications/Messages/Broadcast/NewsAdded.php +++ b/app/Notifications/Messages/Broadcast/NewsAdded.php @@ -6,6 +6,7 @@ use App\Contracts\Notification; use App\Models\News; use App\Notifications\Channels\Discord\DiscordMessage; use Illuminate\Contracts\Queue\ShouldQueue; +use League\HTMLToMarkdown\HtmlConverter; class NewsAdded extends Notification implements ShouldQueue { @@ -31,6 +32,8 @@ class NewsAdded extends Notification implements ShouldQueue public function toDiscordChannel($news): ?DiscordMessage { $dm = new DiscordMessage(); + $markdown = (new HtmlConverter(['header_style' => 'atx']))->convert($news->body); + return $dm->webhook(setting('notifications.discord_public_webhook_url')) ->success() ->title('News: '.$news->subject) @@ -39,7 +42,7 @@ class NewsAdded extends Notification implements ShouldQueue 'url' => '', 'icon_url' => $news->user->resolveAvatarUrl(), ]) - ->description($news->body); + ->description($markdown); } /** diff --git a/composer.json b/composer.json index 48fdb4c1..a823d70d 100644 --- a/composer.json +++ b/composer.json @@ -85,7 +85,8 @@ "spatie/laravel-backup": "*", "laravel/socialite": "^5.11", "socialiteproviders/discord": "^4.2", - "symfony/postmark-mailer": "^6.0" + "symfony/postmark-mailer": "^6.0", + "league/html-to-markdown": "^5.1" }, "require-dev": { "barryvdh/laravel-debugbar": "^3.8.1", diff --git a/composer.lock b/composer.lock index 1e00d5d7..76b74bf9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b4044ac9103746eae3b32bd6a15560ed", + "content-hash": "dfdeb674b83929dc487d333bb5856a2d", "packages": [ { "name": "akaunting/laravel-money", @@ -4597,6 +4597,95 @@ }, "time": "2022-10-26T19:43:53+00:00" }, + { + "name": "league/html-to-markdown", + "version": "5.1.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/html-to-markdown.git", + "reference": "0b4066eede55c48f38bcee4fb8f0aa85654390fd" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/html-to-markdown/zipball/0b4066eede55c48f38bcee4fb8f0aa85654390fd", + "reference": "0b4066eede55c48f38bcee4fb8f0aa85654390fd", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-xml": "*", + "php": "^7.2.5 || ^8.0" + }, + "require-dev": { + "mikehaertl/php-shellcommand": "^1.1.0", + "phpstan/phpstan": "^1.8.8", + "phpunit/phpunit": "^8.5 || ^9.2", + "scrutinizer/ocular": "^1.6", + "unleashedtech/php-coding-standard": "^2.7 || ^3.0", + "vimeo/psalm": "^4.22 || ^5.0" + }, + "bin": [ + "bin/html-to-markdown" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.2-dev" + } + }, + "autoload": { + "psr-4": { + "League\\HTMLToMarkdown\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Colin O'Dell", + "email": "colinodell@gmail.com", + "homepage": "https://www.colinodell.com", + "role": "Lead Developer" + }, + { + "name": "Nick Cernis", + "email": "nick@cern.is", + "homepage": "http://modernnerd.net", + "role": "Original Author" + } + ], + "description": "An HTML-to-markdown conversion helper for PHP", + "homepage": "https://github.com/thephpleague/html-to-markdown", + "keywords": [ + "html", + "markdown" + ], + "support": { + "issues": "https://github.com/thephpleague/html-to-markdown/issues", + "source": "https://github.com/thephpleague/html-to-markdown/tree/5.1.1" + }, + "funding": [ + { + "url": "https://www.colinodell.com/sponsor", + "type": "custom" + }, + { + "url": "https://www.paypal.me/colinpodell/10.00", + "type": "custom" + }, + { + "url": "https://github.com/colinodell", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/league/html-to-markdown", + "type": "tidelift" + } + ], + "time": "2023-07-12T21:21:09+00:00" + }, { "name": "league/iso3166", "version": "4.2.1", @@ -14498,5 +14587,5 @@ "ext-zip": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" }