-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
130 lines (116 loc) · 3.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const babel = require("@babel/core");
const React = require("react");
const { renderToStaticMarkup } = require("react-dom/server");
const mdx = require("@mdx-js/mdx");
const { MDXProvider, mdx: createElement } = require("@mdx-js/react");
const { createCompiler } = require("@mdx-js/mdx");
const detectFrontmatter = require("remark-frontmatter");
const sectionize = require("./src/remark-sectionize-alt");
const vfile = require("vfile");
const visit = require("unist-util-visit");
const remove = require("unist-util-remove");
const yaml = require("yaml");
const path = require("path");
const fs = require("fs");
const open = require("open");
const temp = require("temp").track();
const mjml2html = require("mjml");
const nodemailer = require("nodemailer");
const aws = require("aws-sdk");
aws.config.region = "ap-southeast-2";
const { getComponents } = require("import-jsx")("./src/template");
(async function () {
const mdx = fs.readFileSync(path.resolve("content/example.md"), {
encoding: "utf-8",
});
const config = yaml.parse(
fs.readFileSync(path.resolve("content/config.yaml"), {
encoding: "utf-8",
})
);
const mjml = await renderWithReact(vfile(mdx), config);
// console.log(html);
// await writeTempAndOpen(mdx, { suffix: ".md" }, ["sublime text"]);
const html = mjml2html(mjml).html;
// console.log(html);
await writeTempAndOpen(html, { suffix: ".html" }, ["brave browser"]);
const transporter = nodemailer.createTransport({
SES: new aws.SES({ apiVersion: "2010-12-01" }),
});
// Requires SES roles.
// {
// "Statement": [
// {
// "Effect": "Allow",
// "Action": "ses:SendRawEmail",
// "Resource": "*"
// }
// ]
// }
transporter.sendMail(
{
from: "[email protected]",
to: "[email protected]",
subject: "Test Message",
html: html,
text: mdx,
},
(err, info) => {
if (err) {
console.log(err);
}
if (info) {
console.log(info.envelope);
console.log(info.messageId);
}
}
);
})();
function extractFrontmatter() {
return function transformer(tree, file) {
visit(tree, "yaml", function visitor(node) {
file.data.frontmatter = yaml.parse(node.value);
});
remove(tree, "yaml");
};
}
const transform = (code) =>
babel.transform(code, {
plugins: [
"@babel/plugin-transform-react-jsx",
"@babel/plugin-proposal-object-rest-spread",
],
}).code;
async function renderWithReact(file, config) {
const mdxCompiler = createCompiler({
skipExport: true,
remarkPlugins: [detectFrontmatter, extractFrontmatter, sectionize],
});
const { contents, data } = await mdxCompiler.process(file);
const jsx = `/* @jsx mdx */
${contents}`;
const code = transform(jsx);
const scope = { mdx: createElement };
const fn = new Function(
"React",
...Object.keys(scope),
`${code}; return React.createElement(MDXContent)`
);
const element = fn(React, ...Object.values(scope));
const components = getComponents({ ...config, ...data.frontmatter });
const elementWithProvider = React.createElement(
MDXProvider,
{ components },
element
);
return renderToStaticMarkup(elementWithProvider);
}
async function writeTempAndOpen(data, affixes, app) {
const info = temp.openSync(affixes);
fs.writeSync(info.fd, data);
fs.closeSync(info.fd);
await open(`file://${info.path}`, {
wait: true,
app,
});
}