escapehtml

This commit is contained in:
Cypro Freelance 2024-07-04 00:00:11 +05:30
parent 1b16108d55
commit 6dd363fa4a

View file

@ -7,19 +7,30 @@ const CONTACT_MESSAGE_FIELDS: Record<string, string> = {
message: "Message", message: "Message",
}; };
const escapeHtml = (text: string) => {
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\n/g, "<br/>");
};
const generateEmailContent = (data: Record<string, string>) => { const generateEmailContent = (data: Record<string, string>) => {
const stringData = Object.entries(data).reduce( const stringData = Object.entries(data).reduce(
(str, [key, val]) => (str, [key, val]) =>
str + `${CONTACT_MESSAGE_FIELDS[key] || key}: \n${val} \n\n`, str +
`${CONTACT_MESSAGE_FIELDS[key] || escapeHtml(key)}: \n${escapeHtml(
val
)} \n\n`,
"" ""
); );
const htmlData = Object.entries(data).reduce( const htmlData = Object.entries(data).reduce(
(str, [key, val]) => (str, [key, val]) =>
str + str +
`<h3 class="form-heading">${ `<h3 class="form-heading">${escapeHtml(
CONTACT_MESSAGE_FIELDS[key] || key CONTACT_MESSAGE_FIELDS[key] || key
}</h3><p class="form-answer">${val}</p>`, )}</h3><p class="form-answer">${escapeHtml(val)}</p>`,
"" ""
); );