Skip to content

Commit c5deefe

Browse files
committed
fix(html): correctly render escaped html
1 parent 37ea3a5 commit c5deefe

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/__tests__/Markdown.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ Here is some \\*escaped\\* stuff.
121121
Allow \\_\\_tests\\_\\_.
122122
`;
123123

124+
const ESCAPED_HTML_MARKDOWN = `
125+
- \\<img>
126+
- \\<video>
127+
- \\<iframe>
128+
- \\<embed>
129+
- \\<object>
130+
`;
131+
124132
const BR_MARKDOWN = `
125133
Trailing two spaces to force break
126134
Second Line of text with trailing slash to force break\\
@@ -426,6 +434,14 @@ describe("Markdown", () => {
426434
expect(container).toMatchSnapshot();
427435
});
428436

437+
it("should be able to render escaped (backslash-prefixed) html", () => {
438+
const { container } = render(<Markdown markdown={ESCAPED_HTML_MARKDOWN} />);
439+
const firstLi = document.querySelector("li");
440+
expect(firstLi?.innerHTML).toBe("&lt;img&gt;");
441+
442+
expect(container).toMatchSnapshot();
443+
});
444+
429445
it("should be able to render breaks (<br>)", () => {
430446
const { container } = render(<Markdown markdown={BR_MARKDOWN} />);
431447

src/__tests__/__snapshots__/Markdown.tsx.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,34 @@ exports[`Markdown should be able to render emphasis (italic) text 1`] = `
201201
</div>
202202
`;
203203
204+
exports[`Markdown should be able to render escaped (backslash-prefixed) html 1`] = `
205+
<div>
206+
207+
<ul>
208+
<li>
209+
&lt;
210+
img&gt;
211+
</li>
212+
<li>
213+
&lt;
214+
video&gt;
215+
</li>
216+
<li>
217+
&lt;
218+
iframe&gt;
219+
</li>
220+
<li>
221+
&lt;
222+
embed&gt;
223+
</li>
224+
<li>
225+
&lt;
226+
object&gt;
227+
</li>
228+
</ul>
229+
</div>
230+
`;
231+
204232
exports[`Markdown should be able to render escaped (backslash-prefixed) text 1`] = `
205233
<div>
206234

src/renderers/html.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export type EscapeRendererProps = marked.Tokens.Escape;
3636
* ```
3737
*/
3838
export function EscapeRenderer({ text }: EscapeRendererProps): ReactElement {
39+
if (text === "&lt;") {
40+
return <>&lt;</>;
41+
}
42+
3943
return <>{text}</>;
4044
}
4145

0 commit comments

Comments
 (0)