feat/unsafe html (#11)

* add unsafe html render and plugins

* small env fix

* update UNSAFE_HTML env

Co-authored-by: Maas Lalani <maas@lalani.dev>

---------

Co-authored-by: Maas Lalani <maas@lalani.dev>
This commit is contained in:
egor 2023-07-20 23:42:37 +03:00 committed by GitHub
parent 85b1f54563
commit f8dfdafcd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -9,6 +9,8 @@ import (
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/resendlabs/resend-go" "github.com/resendlabs/resend-go"
"github.com/yuin/goldmark" "github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
renderer "github.com/yuin/goldmark/renderer/html"
) )
const TO_SEPARATOR = "," const TO_SEPARATOR = ","
@ -41,7 +43,21 @@ func sendEmail(to []string, from, subject, body string, paths []string) error {
html := bytes.NewBufferString("") html := bytes.NewBufferString("")
// If the conversion fails, we'll simply send the plain-text body. // If the conversion fails, we'll simply send the plain-text body.
_ = goldmark.Convert([]byte(body), html) if unsafe {
markdown := goldmark.New(
goldmark.WithRendererOptions(
renderer.WithUnsafe(),
),
goldmark.WithExtensions(
extension.Strikethrough,
extension.Table,
extension.Linkify,
),
)
_ = markdown.Convert([]byte(body), html)
} else {
_ = goldmark.Convert([]byte(body), html)
}
request := &resend.SendEmailRequest{ request := &resend.SendEmailRequest{
From: from, From: from,

View file

@ -15,6 +15,7 @@ import (
) )
const RESEND_API_KEY = "RESEND_API_KEY" const RESEND_API_KEY = "RESEND_API_KEY"
const UNSAFE_HTML = "POP_UNSAFE_HTML"
const POP_FROM = "POP_FROM" const POP_FROM = "POP_FROM"
const POP_SIGNATURE = "POP_SIGNATURE" const POP_SIGNATURE = "POP_SIGNATURE"
@ -25,6 +26,7 @@ var (
body string body string
attachments []string attachments []string
preview bool preview bool
unsafe bool
signature string signature string
) )
@ -151,6 +153,8 @@ func init() {
rootCmd.Flags().StringVarP(&from, "from", "f", envFrom, "Email's sender "+commentStyle.Render("($"+POP_FROM+")")) rootCmd.Flags().StringVarP(&from, "from", "f", envFrom, "Email's sender "+commentStyle.Render("($"+POP_FROM+")"))
rootCmd.Flags().StringVarP(&subject, "subject", "s", "", "Email's subject") rootCmd.Flags().StringVarP(&subject, "subject", "s", "", "Email's subject")
rootCmd.Flags().BoolVarP(&preview, "preview", "p", false, "Whether to preview the email before sending") rootCmd.Flags().BoolVarP(&preview, "preview", "p", false, "Whether to preview the email before sending")
envUnsafe := os.Getenv(UNSAFE_HTML) == "true"
rootCmd.Flags().BoolVarP(&unsafe, "unsafe", "u", envUnsafe, "Whether to allow unsafe HTML in the email body, also enable some extra markdown features (Experimental)")
envSignature := os.Getenv("POP_SIGNATURE") envSignature := os.Getenv("POP_SIGNATURE")
rootCmd.Flags().StringVarP(&signature, "signature", "x", envSignature, "Signature to display at the end of the email. "+commentStyle.Render("($"+POP_SIGNATURE+")")) rootCmd.Flags().StringVarP(&signature, "signature", "x", envSignature, "Signature to display at the end of the email. "+commentStyle.Render("($"+POP_SIGNATURE+")"))