Created
July 6, 2021 02:25
-
-
Save alexsc6955/21d12b1cf76414e708200598c0c6368d to your computer and use it in GitHub Desktop.
Parse Html Mail Template with node.js replacing string between brackets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require the module | |
const { parseMailTemplate } = require("./path/to/parseMailTemplate"); | |
// The params we need to pass | |
const html = '<html lang="es"><head><meta charset="utf-8"><title>Email Verification</title></head><body><h1>Hey, {{ name }}</h1><p>You have just created an account on <strong>{{ company }}</strong>. Please click the following <a href="{{ link }}" target="_blank">link</a> so you can start using your account.</p></body></html>'; | |
const data = { | |
name: "Jhon Doe", | |
company: "My awesome company", | |
link: "http://example.com, | |
}; | |
// Call the function | |
parseMailTemplate(html, data, (error, parsedHtml) => { | |
if (error) console.log(error); | |
console.log(parsedHtml); | |
// Do whatever you want with your parsed tamplate | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse Html Mail Template | |
* Replace all strings between brackets with the given data | |
* | |
* @param {string} html The hatml sting | |
* @param {object} data The values taht will replace all strings between brackets | |
* @param {function} next Callback function | |
* | |
* @returns {string} parsedHtml || {object} error | |
*/ | |
exports.parseMailTemplate = (html, data, next) => { | |
let error, parsedHtml; | |
// If not html given, sets error | |
if (!html) { | |
error = { | |
name: "RequiredValue", | |
message: "You need to specify a template to parse", | |
}; | |
} | |
// If not data given sets error | |
if (!data) { | |
error = { name: "RequiredValue", message: "You need to specify the data" }; | |
} | |
// If error retunn error | |
if (error) { | |
if (next && typeof next === "function") next(error, null); // If there is a callback function, send the error | |
return error; | |
} | |
// Repalces string between brackets | |
parsedHtml = html.replace(/\{\{(.+?)\}\}/g, (_, g) => { | |
// If there is a wrong data index, returns error | |
if (!data[g.trim()]) { | |
error = { name: "IndexDoesNotExists", message: "Index missing on data" }; | |
if (next && typeof next === "function") next(error, null); | |
return error; | |
} | |
return data[g.trim()]; | |
}); | |
if (next && typeof next === "function") next(error, parsedHtml); // If there is a callback function, send the parsed html | |
return parsedHtml; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment