Skip to content

Commit

Permalink
Added emailManager, which convertTemplate into string. Added that tem…
Browse files Browse the repository at this point in the history
…plates can use tailwindcss #55
  • Loading branch information
StefanPenchev05 committed Mar 13, 2024
1 parent f3f7222 commit 63f4741
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/emailTemplates/styles/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./src/emailTemplates/**/*.html'],
theme: {
extend: {},
},
plugins: [],
}

34 changes: 34 additions & 0 deletions server/utils/emailTemplateManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ejs from "ejs"
import path from "path"
import juice from "juice"
import tailwindcss from "tailwindcss"

/**
* Load and convert an HTML template.
* @param {string} templateName - The name of the template to load. This should be the name of an HTML file in the emailTemplates directory, without the .html extension.
* @param {Object} data - The data to insert into the template. This should be an object where the keys are the names of placeholders in the template and the values are the values to replace the placeholders with.
* @param {Boolean} useTailwindcss - Indicates whether the template uses Tailwind CSS for styling. If true, the function will apply Tailwind CSS styles to the template. If false, it will apply vanila css styles.
* @returns {Promise<string>} The converted HTML template.
*/

export function convertTemplate(templateName, data, useTailwindcss = true){
return new Promise((resolve, reject) => {
const templatePath = path.resolve(__dirname, `../emailTemplates/${templateName}.html`);
ejs.renderFile(templatePath, data, {}, (err, str) => {
if(err){
reject(err);
}else{
if(useTailwindcss){
// Apply Tailwind CSS styles
const css = tailwindcss(path.resolve(__dirname, "../emailTemplates/style/tailwind.config.js"));
const inlineHtml = juice.inlineContent(str,css);
resolve(inlineHtml);
}else{
const css = path.resolve(__dirname, `../emailTemplates/${templateName}/style.css`);
const inlineHtml = juice.inlineContent(str,css);
resolve(inlineHtml);
}
}
});
})
}

0 comments on commit 63f4741

Please sign in to comment.