From 653a354decddd528707c8c533ec400307a8970f8 Mon Sep 17 00:00:00 2001 From: Luna <82669166+Lunanuuu@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:09:55 +0200 Subject: [PATCH 1/2] Add HTML to Markdown conversion for Discord embeds ### Description Add the `convertHtmlToMarkdown` function to convert HTML content into Markdown format to increase the quality of the Discord Webhook feature. 1. **Anchor Tags (``)**: Converts links to Markdown links - Example: `Example` becomes `[Example](https://www.example.com)` 2. **Headings (`
`)**: Converts blockquotes to Markdown blockquotes - Example: `text` becomes `> text` 8. **Code (``)**: Converts code tags to Markdown code - Example: `
code
` becomes `` `code` `` 9. **Horizontal Rules (`
`)**: Converts horizontal rules to Markdown horizontal rules - Example: `
` becomes `---` (Discord does not yet support horizontal rules, but I think it's likely that they will be added in the future) Screenshots of my tests: ![Screenshot 1](https://www.skydinse.net/storage/img/changelog-plugin-update-1.png) ![Screenshot 2](https://www.skydinse.net/storage/img/changelog-plugin-update-2.png) ![Screenshot 3](https://www.skydinse.net/storage/img/changelog-plugin-update-3.png) --- src/Models/Update.php | 51 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Models/Update.php b/src/Models/Update.php index c406bc6..097b4e4 100644 --- a/src/Models/Update.php +++ b/src/Models/Update.php @@ -45,12 +45,61 @@ public function category() return $this->belongsTo(Category::class); } + public function convertHtmlToMarkdown($text) + { + // Convert tags to Markdown links + $text = preg_replace_callback('/]*>(.*?)<\/a>/i', function ($matches) { + return '[' . $matches[2] . '](' . $matches[1] . ')'; + }, $text); + + // Convert to Markdown bold + $text = preg_replace('/(.*?)<\/strong>/i', '**$1**', $text); + + // Convert to Markdown italic + $text = preg_replace('/(.*?)<\/i>/i', '*$1*', $text); + + // Convertto
into Markdown headings with a limit of 3 # + for ($i = 1; $i <= 6; $i++) { + $text = preg_replace('/
]*>(.*?)<\/h' . $i . '>/i', str_repeat('#', min($i, 3)) . ' $1', $text); + } + + // Convert and
- to Markdown lists + $text = preg_replace_callback('/
]*>(.*?)<\/ul>/is', function ($matches) { + return preg_replace('/
- ]*>(.*?)<\/li>/is', '- $1', $matches[1]); + }, $text); + + // Convert
and
- to numbered Markdown lists + $text = preg_replace_callback('/
]*>(.*?)<\/ol>/is', function ($matches) { + $markdownList = ''; + $items = preg_match_all('/
- ]*>(.*?)<\/li>/is', $matches[1], $liMatches); + foreach ($liMatches[1] as $index => $item) { + $markdownList .= ($index + 1) . '. ' . strip_tags($item) . PHP_EOL; + } + return $markdownList; + }, $text); + + // Convert
to Markdown blockquotes + $text = preg_replace('/]*>(.*?)<\/blockquote>/is', '> $1', $text); + + // Convertto Markdown inline code + $text = preg_replace('/
(.*?)<\/code>/is', '`$1`', $text); + + // Convert
to Markdown horizontal rule + $text = preg_replace('/
]*>/i', '---', $text); + + // Remove all remaining HTML tags + $text = strip_tags($text); + + return $text; + } + public function createDiscordWebhook(User $author): DiscordWebhook { + $embed = Embed::create() ->title($this->name) ->author($author->name, null, $author->getAvatar()) - ->description(Str::limit(strip_tags($this->description), 1995)) + ->description(Str::limit($this->convertHtmlToMarkdown($this->description), 1995)) ->url(route('changelog.categories.show', $this->category)) ->footer($this->category->name) ->timestamp(now()); From dabca89afe08cb27a3b4e187ef220998263d9b0d Mon Sep 17 00:00:00 2001 From: Luna <82669166+Lunanuuu@users.noreply.github.com> Date: Tue, 30 Jul 2024 14:56:05 +0200 Subject: [PATCH 2/2] Fix new line issue with
tags --- src/Models/Update.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Models/Update.php b/src/Models/Update.php index 097b4e4..b21c6fc 100644 --- a/src/Models/Update.php +++ b/src/Models/Update.php @@ -87,6 +87,9 @@ public function convertHtmlToMarkdown($text) // Convert
to Markdown horizontal rule $text = preg_replace('/
]*>/i', '---', $text); + // Convert
and
to new lines (\n) + $text = preg_replace('/
/i', "\n", $text); + // Remove all remaining HTML tags $text = strip_tags($text);