From 9fc2bdaff26dd60b2713fa4427b07ba9d7e8a628 Mon Sep 17 00:00:00 2001 From: Niloy Sikdar Date: Tue, 28 Jun 2022 14:01:52 +0530 Subject: [PATCH] feat(generateEmail): add generateEmail - add generateEmail method to extract plain optimized HTML from the react based layout - optional template override options with a default optimized template for baseTemplate resolves #21 Signed-off-by: Niloy Sikdar --- package.json | 2 +- src/index.ts | 1 + src/server/generateEmail/defaultHTML.ts | 1 + src/server/generateEmail/generateEmail.ts | 31 +++++++++++++++++++++++ src/server/generateEmail/index.ts | 1 + src/server/index.ts | 1 + 6 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/server/generateEmail/defaultHTML.ts create mode 100644 src/server/generateEmail/generateEmail.ts create mode 100644 src/server/generateEmail/index.ts create mode 100644 src/server/index.ts diff --git a/package.json b/package.json index 64562c9..0f8910e 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "build-storybook": "build-storybook" }, "lint-staged": { - "src/*.{ts,tsx}": [ + "src/**/*": [ "yarn pretty:fix", "yarn lint:fix", "yarn pretty", diff --git a/src/index.ts b/src/index.ts index 590a7f4..d0c0a1e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ export * from './components'; export * from './utils'; +export * from './server'; diff --git a/src/server/generateEmail/defaultHTML.ts b/src/server/generateEmail/defaultHTML.ts new file mode 100644 index 0000000..00024a7 --- /dev/null +++ b/src/server/generateEmail/defaultHTML.ts @@ -0,0 +1 @@ +export const deafultHTML = ` `; diff --git a/src/server/generateEmail/generateEmail.ts b/src/server/generateEmail/generateEmail.ts new file mode 100644 index 0000000..302be98 --- /dev/null +++ b/src/server/generateEmail/generateEmail.ts @@ -0,0 +1,31 @@ +import ReactDOMServer from 'react-dom/server'; +import fs from 'fs'; +import { deafultHTML } from './defaultHTML'; + +interface GenerateEmailOptions { + baseTemplate?: string; + baseStyles?: string; +} + +export const generateEmail = ( + jsxElement: React.ReactElement, + options?: GenerateEmailOptions, +): string => { + let baseHTML = ''; + options?.baseTemplate + ? (baseHTML = fs.readFileSync(options.baseTemplate, 'utf8')).toString() + : (baseHTML = deafultHTML); + + let baseCSS = ''; + options?.baseStyles && (baseCSS = fs.readFileSync(options.baseStyles, 'utf8')).toString(); + + const JSXtoHTML = ReactDOMServer.renderToStaticMarkup(jsxElement); + + const styleEndPos = baseHTML.indexOf(''); + const styledHTML = baseHTML.slice(0, styleEndPos) + '\n' + baseCSS + baseHTML.slice(styleEndPos); + + const bodyEndPos = styledHTML.indexOf(''); + const finalHTML = styledHTML.slice(0, bodyEndPos) + JSXtoHTML + styledHTML.slice(bodyEndPos); + + return finalHTML; +}; diff --git a/src/server/generateEmail/index.ts b/src/server/generateEmail/index.ts new file mode 100644 index 0000000..308c7cb --- /dev/null +++ b/src/server/generateEmail/index.ts @@ -0,0 +1 @@ +export { generateEmail } from './generateEmail'; diff --git a/src/server/index.ts b/src/server/index.ts new file mode 100644 index 0000000..308c7cb --- /dev/null +++ b/src/server/index.ts @@ -0,0 +1 @@ +export { generateEmail } from './generateEmail';