Skip to content

Commit

Permalink
Add markdown format support (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Dawid Dziurla <[email protected]>
  • Loading branch information
dougpagani and dawidd6 authored Nov 30, 2020
1 parent d1b07d9 commit 2095e6f
Show file tree
Hide file tree
Showing 941 changed files with 37,481 additions and 15 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master
workflow_dispatch:

jobs:
main:
Expand All @@ -13,9 +14,10 @@ jobs:
matrix:
include:
- content_type: text/markdown
body: file://README.md
attachments: action.yml
body: file://README.md
- content_type: text/html
attachments: package.json,package-lock.json
body: |
<!DOCTYPE html>
<html>
Expand All @@ -24,7 +26,16 @@ jobs:
<p>Paragraph</p>
</body>
</html>
attachments: package.json,package-lock.json
- content_type: text/html
convert_markdown: true
body: |
# h1
## h2
### h3
**bold**
_italics_
- bullet 1
- bullet 2
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -40,4 +51,5 @@ jobs:
to: ${{github.event.pusher.email}}
from: github-actions
content_type: ${{matrix.content_type}}
convert_markdown: ${{matrix.convert_markdown}}
attachments: ${{matrix.attachments}}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ An action that simply sends a mail to multiple recipients.
from: Luke Skywalker # <[email protected]>
# Optional content type (defaults to text/plain):
content_type: text/html
# Optional converting Markdown to HTML (set content_type to text/html too):
convert_markdown: true
# Optional attachments:
attachments: attachments.zip,git.diff,./dist/static/main.js
```
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ inputs:
description: Content-Type HTTP header (text/html or text/plain)
required: false
default: text/plain
convert_markdown:
description: Convert body from Markdown to HTML (set content_type input as text/html too)
required: false
attachments:
description: Files that will be added to mail message attachments (separated with comma)
required: false
Expand Down
35 changes: 23 additions & 12 deletions main.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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
})

Expand Down
1 change: 1 addition & 0 deletions node_modules/.bin/showdown

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions node_modules/ansi-regex/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions node_modules/ansi-regex/license

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions node_modules/ansi-regex/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions node_modules/ansi-regex/readme.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2095e6f

Please sign in to comment.