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

Proposal: Add HTML to Markdown conversion for Discord embeds #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
54 changes: 53 additions & 1 deletion src/Models/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,64 @@ public function category()
return $this->belongsTo(Category::class);
}

public function convertHtmlToMarkdown($text)
{
// Convert <a> tags to Markdown links
$text = preg_replace_callback('/<a href="([^"]+)"[^>]*>(.*?)<\/a>/i', function ($matches) {
return '[' . $matches[2] . '](' . $matches[1] . ')';
}, $text);

// Convert <strong> to Markdown bold
$text = preg_replace('/<strong>(.*?)<\/strong>/i', '**$1**', $text);

// Convert <i> to Markdown italic
$text = preg_replace('/<i>(.*?)<\/i>/i', '*$1*', $text);

// Convert <h1> to <h6> into Markdown headings with a limit of 3 #
for ($i = 1; $i <= 6; $i++) {
$text = preg_replace('/<h' . $i . '[^>]*>(.*?)<\/h' . $i . '>/i', str_repeat('#', min($i, 3)) . ' $1', $text);
}

// Convert <ul> and <li> to Markdown lists
$text = preg_replace_callback('/<ul[^>]*>(.*?)<\/ul>/is', function ($matches) {
return preg_replace('/<li[^>]*>(.*?)<\/li>/is', '- $1', $matches[1]);
}, $text);

// Convert <ol> and <li> to numbered Markdown lists
$text = preg_replace_callback('/<ol[^>]*>(.*?)<\/ol>/is', function ($matches) {
$markdownList = '';
$items = preg_match_all('/<li[^>]*>(.*?)<\/li>/is', $matches[1], $liMatches);
foreach ($liMatches[1] as $index => $item) {
$markdownList .= ($index + 1) . '. ' . strip_tags($item) . PHP_EOL;
}
return $markdownList;
}, $text);

// Convert <blockquote> to Markdown blockquotes
$text = preg_replace('/<blockquote[^>]*>(.*?)<\/blockquote>/is', '> $1', $text);

// Convert <code> to Markdown inline code
$text = preg_replace('/<code>(.*?)<\/code>/is', '`$1`', $text);

// Convert <hr> to Markdown horizontal rule
$text = preg_replace('/<hr[^>]*>/i', '---', $text);

// Convert <br> and <br /> to new lines (\n)
$text = preg_replace('/<br\s*\/?>/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());
Expand Down