Add markdown format support (#25)

Co-authored-by: Dawid Dziurla <dawidd0811@gmail.com>
This commit is contained in:
dougpagani
2020-11-30 12:51:34 -05:00
committed by GitHub
parent d1b07d9ed7
commit 2095e6ffe3
941 changed files with 37481 additions and 15 deletions

35
main.js
View File

@ -1,11 +1,21 @@
const nodemailer = require("nodemailer")
const core = require("@actions/core")
const fs = require("fs")
const showdown = require('showdown')
function getBody(body) {
if (body.startsWith("file://")) {
const file = body.replace("file://", "")
return fs.readFileSync(file, "utf8")
function getBody(bodyOrFile, convertMarkdown) {
let body = bodyOrFile
// Read body from file
if (bodyOrFile.startsWith("file://")) {
const file = bodyOrFile.replace("file://", "")
body = fs.readFileSync(file, "utf8")
}
// Convert Markdown to HTML
if (convertMarkdown) {
const converter = new showdown.Converter()
body = converter.makeHtml(body)
}
return body
@ -21,21 +31,22 @@ function getFrom(from, username) {
async function main() {
try {
const server_address = core.getInput("server_address", { required: true })
const server_port = core.getInput("server_port", { required: true })
const serverAddress = core.getInput("server_address", { required: true })
const serverPort = core.getInput("server_port", { required: true })
const username = core.getInput("username", { required: true })
const password = core.getInput("password", { required: true })
const subject = core.getInput("subject", { required: true })
const body = core.getInput("body", { required: true })
const to = core.getInput("to", { required: true })
const from = core.getInput("from", { required: true })
const content_type = core.getInput("content_type", { required: true })
const contentType = core.getInput("content_type", { required: true })
const attachments = core.getInput("attachments", { required: false })
const convertMarkdown = core.getInput("convert_markdown", { required: false })
const transport = nodemailer.createTransport({
host: server_address,
port: server_port,
secure: server_port == "465",
host: serverAddress,
port: serverPort,
secure: serverPort == "465",
auth: {
user: username,
pass: password,
@ -48,8 +59,8 @@ async function main() {
from: getFrom(from, username),
to: to,
subject: subject,
text: content_type != "text/html" ? getBody(body) : undefined,
html: content_type == "text/html" ? getBody(body) : undefined,
text: contentType != "text/html" ? getBody(body, convertMarkdown) : undefined,
html: contentType == "text/html" ? getBody(body, convertMarkdown) : undefined,
attachments: attachments ? attachments.split(',').map(f => ({ path: f.trim() })) : undefined
})