diff --git a/src/Models/Update.php b/src/Models/Update.php index c406bc6..b21c6fc 100644 --- a/src/Models/Update.php +++ b/src/Models/Update.php @@ -45,12 +45,64 @@ 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); + + // Convert

to

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
    1. 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); + + // Convert to Markdown inline code + $text = preg_replace('/(.*?)<\/code>/is', '`$1`', $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); + + 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());