Skip to content

Commit c695e43

Browse files
authored
Merge pull request #23 from codeskills-dev/fix/style-quotes
fix(parse-css-in-js-to-inline-css): fix custom style with quotes parsing errors
2 parents eab6eee + 06bebbb commit c695e43

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

.changeset/good-apes-confess.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"md-to-react-email": patch
3+
---
4+
5+
## Fixes
6+
7+
- Markdown custom styles can't handle quotes properly

__tests__/emailMarkdown.test.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ import { EmailMarkdown } from "../src";
44

55
describe("ReactEmailMarkdown component renders correctly", () => {
66
it("renders the markdown in the correct format for browsers", () => {
7-
const actualOutput = render(<EmailMarkdown markdown={`# Hello, World!`} />);
7+
const actualOutput = render(
8+
<EmailMarkdown
9+
markdown={`# Hello, World!
10+
## Hi, World`}
11+
markdownCustomStyles={{
12+
h1: { font: '700 23px / 32px "Roobert PRO", system-ui, sans-serif' },
13+
h2: { background: 'url("path/to/image")' },
14+
}}
15+
/>
16+
);
17+
818
expect(actualOutput).toMatchInlineSnapshot(
9-
`"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><div><h1 style="font-weight:500;padding-top:20px;font-size:2.5rem">Hello, World!</h1></div>"`
19+
`"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><div><h1 style="font:700 23px / 32px 'Roobert PRO', system-ui, sans-serif">Hello, World!</h1><h2 style="background:url('path/to/image')">Hi, World</h2></div>"`
1020
);
1121
});
1222
});

__tests__/parseCssInJsToInlineCss.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ describe("parseCssInJsToInlineCss", () => {
3333
const cssProperties = {};
3434
expect(parseCssInJsToInlineCss(cssProperties)).toBe("");
3535
});
36+
37+
test("should handle CSS properties with escaped quotes", () => {
38+
const cssProperties = {
39+
font: '700 23px / 32px "Roobert PRO", system-ui, sans-serif',
40+
background: "url('path/to/image')",
41+
};
42+
43+
const expectedOutput =
44+
"font:700 23px / 32px 'Roobert PRO', system-ui, sans-serif;background:url('path/to/image')";
45+
expect(parseCssInJsToInlineCss(cssProperties)).toBe(expectedOutput);
46+
});
3647
});

src/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import { StylesType, initRendererProps } from "./types";
33
import { RendererObject } from "marked";
44
import { styles } from "./styles";
55

6+
function escapeQuotes(value: string) {
7+
if (value.includes('"')) {
8+
return value.replace(/"/g, "'");
9+
}
10+
return value;
11+
}
12+
613
export function camelToKebabCase(str: string): string {
714
return str.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
815
}
@@ -64,7 +71,8 @@ export function parseCssInJsToInlineCss(
6471
) {
6572
return `${camelToKebabCase(property)}:${value}px`;
6673
} else {
67-
return `${camelToKebabCase(property)}:${value}`;
74+
const escapedValue = escapeQuotes(value);
75+
return `${camelToKebabCase(property)}:${escapedValue}`;
6876
}
6977
})
7078
.join(";");

0 commit comments

Comments
 (0)