Skip to content

Commit f1cc90f

Browse files
committed
fix(code): inline code correctly displays html entities
1 parent 1645d0d commit f1cc90f

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

src/__tests__/Markdown.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ const CODE_SPAN_MARKDOWN = `
143143
This has some \`inline code\` to see.
144144
`;
145145

146+
const CODE_SPAN_ADVANCED_MARKDOWN = `
147+
This has some \`inline code that "contains" html 'entities' and <other></other>\`
148+
149+
Some other html entities:
150+
151+
- \`¢\` cent
152+
- \`£\` pound
153+
- \`¥\` yen
154+
- \`€\` euro
155+
- \`©\` copyright
156+
- \`®\` registered trademark
157+
`;
158+
146159
const UNORDERED_LIST_MARKDOWN = `
147160
- Item 1
148161
- Item 2
@@ -450,6 +463,17 @@ describe("Markdown", () => {
450463
expect(container).toMatchSnapshot();
451464
});
452465

466+
it("should be able to render inline code that has html entities", () => {
467+
const { container } = render(
468+
<Markdown markdown={CODE_SPAN_ADVANCED_MARKDOWN} />
469+
);
470+
471+
expect(container.textContent).not.toMatch(
472+
/&(quot|lt|gt|amp|cent|pound|yen|euro|copy|reg);/
473+
);
474+
expect(container).toMatchSnapshot();
475+
});
476+
453477
it("should be able to highlight code with the highlightCode option", () => {
454478
const getLanguage: GetCodeLanguage = (lang) => {
455479
lang = lang === "sh" ? "bash" : lang;

src/__tests__/__snapshots__/Markdown.tsx.snap

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,60 @@ exports[`Markdown should be able to render inline code 1`] = `
296296
</div>
297297
`;
298298
299+
exports[`Markdown should be able to render inline code that has html entities 1`] = `
300+
<div>
301+
<p>
302+
This has some
303+
<code>
304+
inline code that "contains" html 'entities' and &lt;other&gt;&lt;/other&gt;
305+
</code>
306+
</p>
307+
308+
<p>
309+
Some other html entities:
310+
</p>
311+
312+
<ul>
313+
<li>
314+
<code>
315+
¢
316+
</code>
317+
cent
318+
</li>
319+
<li>
320+
<code>
321+
£
322+
</code>
323+
pound
324+
</li>
325+
<li>
326+
<code>
327+
¥
328+
</code>
329+
yen
330+
</li>
331+
<li>
332+
<code>
333+
334+
</code>
335+
euro
336+
</li>
337+
<li>
338+
<code>
339+
©
340+
</code>
341+
copyright
342+
</li>
343+
<li>
344+
<code>
345+
®
346+
</code>
347+
registered trademark
348+
</li>
349+
</ul>
350+
</div>
351+
`;
352+
299353
exports[`Markdown should be able to render links that reference specific ids 1`] = `
300354
<div>
301355
<p>

src/renderers/tokens.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ export function TokenRenderer({
120120
</Code>
121121
);
122122
case "codespan":
123-
return <Codespan {...token}>{token.text}</Codespan>;
123+
return (
124+
<Codespan {...token}>
125+
{token.raw.substring(1, token.raw.length - 1)}
126+
</Codespan>
127+
);
124128
case "heading": {
125129
const { depth, ...props } = token;
126130
return (

0 commit comments

Comments
 (0)