2021-02-20 01:03:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2021-05-22 14:32:51 +00:00
|
|
|
dg "github.com/bwmarrin/discordgo"
|
2021-02-20 01:03:11 +00:00
|
|
|
stripmd "github.com/writeas/go-strip-markdown"
|
|
|
|
)
|
|
|
|
|
2021-05-21 20:59:50 +00:00
|
|
|
type Link struct {
|
|
|
|
Alt, URL string
|
|
|
|
}
|
|
|
|
|
2021-03-26 23:13:19 +00:00
|
|
|
// StripAltText removes Markdown alt text from links and images and replaces them with just the URL.
|
|
|
|
// Currently uses the deepest alt text when links/images are nested.
|
2021-05-21 20:59:50 +00:00
|
|
|
// If links = true, links are completely removed, and a list of URLs and their alt text is also returned.
|
2021-05-22 14:32:51 +00:00
|
|
|
func StripAltText(md string, links bool) (string, []*dg.MessageEmbed) {
|
2021-03-26 23:13:19 +00:00
|
|
|
altTextStart := -1 // Start of alt text (between '[' & ']')
|
|
|
|
URLStart := -1 // Start of url (between '(' & ')')
|
|
|
|
URLEnd := -1
|
|
|
|
previousURLEnd := -2
|
|
|
|
out := ""
|
2021-05-22 14:32:51 +00:00
|
|
|
embeds := []*dg.MessageEmbed{}
|
2021-03-26 23:13:19 +00:00
|
|
|
for i := range md {
|
|
|
|
if altTextStart != -1 && URLStart != -1 && md[i] == ')' {
|
|
|
|
URLEnd = i - 1
|
2021-05-21 20:59:50 +00:00
|
|
|
out += md[previousURLEnd+2 : altTextStart-1]
|
|
|
|
if links {
|
2021-05-22 14:32:51 +00:00
|
|
|
embed := &dg.MessageEmbed{
|
|
|
|
Type: dg.EmbedTypeLink,
|
|
|
|
Title: md[altTextStart : URLStart-2],
|
|
|
|
}
|
|
|
|
if md[altTextStart-1] == '!' {
|
|
|
|
embed.Title = md[altTextStart+1 : URLStart-2]
|
|
|
|
embed.Type = dg.EmbedTypeImage
|
|
|
|
embed.Image = &dg.MessageEmbedImage{
|
|
|
|
URL: md[URLStart : URLEnd+1],
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
embed.URL = md[URLStart : URLEnd+1]
|
|
|
|
}
|
|
|
|
embeds = append(embeds, embed)
|
2021-05-21 20:59:50 +00:00
|
|
|
} else {
|
|
|
|
out += md[URLStart : URLEnd+1]
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
previousURLEnd = URLEnd
|
2021-05-23 00:05:53 +00:00
|
|
|
// Removing links often leaves a load of extra newlines which look weird, this removes them.
|
|
|
|
if links {
|
|
|
|
next := 2
|
|
|
|
for md[URLEnd+next] == '\n' {
|
|
|
|
next++
|
|
|
|
}
|
|
|
|
if next >= 3 {
|
|
|
|
previousURLEnd += next - 2
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
altTextStart, URLStart, URLEnd = -1, -1, -1
|
2021-02-20 01:03:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
if md[i] == '[' && altTextStart == -1 {
|
|
|
|
altTextStart = i + 1
|
|
|
|
if i > 0 && md[i-1] == '!' {
|
|
|
|
altTextStart--
|
2021-02-20 01:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
if i > 0 && md[i-1] == ']' && md[i] == '(' && URLStart == -1 {
|
|
|
|
URLStart = i + 1
|
2021-02-20 22:49:59 +00:00
|
|
|
}
|
2021-02-20 01:03:11 +00:00
|
|
|
}
|
2021-03-26 23:13:19 +00:00
|
|
|
if previousURLEnd+1 != len(md)-1 {
|
|
|
|
out += md[previousURLEnd+2:]
|
|
|
|
}
|
|
|
|
if out == "" {
|
2021-05-21 20:59:50 +00:00
|
|
|
return md, embeds
|
2021-02-20 01:03:11 +00:00
|
|
|
}
|
2021-05-21 20:59:50 +00:00
|
|
|
return out, embeds
|
2021-03-26 23:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stripMarkdown(md string) string {
|
2021-05-21 20:59:50 +00:00
|
|
|
stripped, _ := StripAltText(md, false)
|
|
|
|
return strings.TrimPrefix(strings.TrimSuffix(stripmd.Strip(stripped), "</p>"), "<p>")
|
2021-02-20 01:03:11 +00:00
|
|
|
}
|