Skip to content

Commit e2e5fc4

Browse files
committed
fix: special characters in inline code
1 parent 982a985 commit e2e5fc4

File tree

5 files changed

+35
-35
lines changed

5 files changed

+35
-35
lines changed

apps/meteor/client/components/MarkdownText.spec.tsx

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ const markdownText = `
3838
gabriel.engel@rocket.chat
3939
+55991999999
4040
\`Inline code\`
41-
\`2 < 3 > 1 & 4 "Test"\`
4241
\`\`\`typescript
4342
const test = 'this is code'
4443
\`\`\`
45-
\`\`\`
46-
Two < Three > One & Four "Test"
47-
\`\`\`
4844
**Bold text within __Italics__**
4945
*Bold text with single asterik and underscore within _Italics_*
5046
__Italics within **Bold** text__
@@ -82,9 +78,7 @@ it('should render html elements as expected using default parser', async () => {
8278

8379
expect(normalizedHtml).toContain('+55991999999');
8480
expect(normalizedHtml).toContain('<code>Inline code</code>');
85-
expect(normalizedHtml).toContain('<code>2 &lt; 3 &gt; 1 &amp; 4 "Test"</code>');
8681
expect(normalizedHtml).toContain('<pre><code class="language-typescript">const test = \'this is code\' </code></pre>');
87-
expect(normalizedHtml).toContain('<pre><code>Two &lt; Three &gt; One &amp; Four "Test" </code></pre>');
8882
expect(normalizedHtml).toContain('<strong>Bold text within <em>Italics</em></strong>');
8983
expect(normalizedHtml).toContain('<strong>Bold text with single asterik and underscore within <em>Italics</em></strong>');
9084
expect(normalizedHtml).toContain('<em>Italics within <strong>Bold</strong> text</em>');
@@ -274,3 +268,33 @@ describe('links handling', () => {
274268
else expect(anchorElement).not.toHaveAttribute('title');
275269
});
276270
});
271+
272+
describe('code handling', () => {
273+
it.each([
274+
{
275+
caseName: 'inline code',
276+
content: '`Inline code`',
277+
expected: '<code>Inline code</code>',
278+
},
279+
{
280+
caseName: 'inline code with special characters',
281+
content: '`2 < 3 > 1 & 4 "Test"`',
282+
expected: '<code>2 &lt; 3 &gt; 1 &amp; 4 "Test"</code>',
283+
},
284+
{
285+
caseName: 'block code with language',
286+
content: "```typescript\nconst test = 'this is code'\n```",
287+
expected: '<code class="language-typescript">const test = \'this is code\' </code>',
288+
},
289+
{
290+
caseName: 'block code without language',
291+
content: '```\nTwo < Three > One & Four "Test"\n```',
292+
expected: '<code>Two &lt; Three &gt; One &amp; Four "Test" </code>',
293+
},
294+
] as const)('should render $caseName', ({ content, expected }) => {
295+
render(<MarkdownText content={`${content}`} variant='document' />, {
296+
wrapper: mockAppRoot().build(),
297+
});
298+
expect(screen.getByRole('code').outerHTML).toEqual(expected);
299+
});
300+
});

apps/meteor/client/components/MarkdownText.stories.tsx

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Meta, StoryObj } from '@storybook/react';
2-
import { within, expect } from '@storybook/test';
32
import outdent from 'outdent';
43

54
import MarkdownText from './MarkdownText';
@@ -70,26 +69,6 @@ export const Document: Story = {
7069
`,
7170
variant: 'document',
7271
},
73-
play: async (test) => {
74-
const canvas = within(test.context.canvasElement);
75-
expect(await canvas.findByRole('heading', { level: 1 })).toHaveTextContent('Title');
76-
expect(await canvas.findByRole('heading', { level: 2 })).toHaveTextContent('Subtitle');
77-
78-
const listItem1 = await canvas.findByText('List item 1');
79-
expect(listItem1).toBeVisible();
80-
81-
const listItem2 = await canvas.findByText('List item 2');
82-
expect(listItem2).toBeVisible();
83-
84-
const listItem3 = await canvas.findByText('List item 3');
85-
expect(listItem3).toBeVisible();
86-
87-
const inlineCode = await canvas.findByText('2 < 3 > 1 & 4 "Test"');
88-
expect(inlineCode).toBeVisible();
89-
90-
const blockCode = await canvas.findByText('Two < Three > One & Four "Test"');
91-
expect(blockCode).toBeVisible();
92-
},
9372
};
9473

9574
export const Inline: Story = {

apps/meteor/client/components/MarkdownText.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ const codeMarked = (code: string, language: string | undefined, _isEscaped: bool
4848
}
4949
return `<pre><code>${code} </code></pre>`;
5050
};
51+
const codespanMarked = (code: string): string => {
52+
return `<code>${code.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&')}</code>`;
53+
};
5154

5255
documentRenderer.link = linkMarked;
5356
documentRenderer.listitem = listItemMarked;
5457
documentRenderer.code = codeMarked;
55-
documentRenderer.codespan = (code: string): string =>
56-
`<code>${code.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>')}</code>`;
58+
documentRenderer.codespan = codespanMarked;
5759

5860
inlineRenderer.link = linkMarked;
5961
inlineRenderer.paragraph = paragraphMarked;

apps/meteor/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"set-version": "node .scripts/set-version.js",
5353
"release": "meteor yarn set-version --silent",
5454
"storybook": "storybook dev -p 6006 --no-version-updates",
55-
"test-storybook": "npx concurrently -k -s first -n \"SB,TEST\" \"yarn storybook --ci\" \"npx wait-on tcp:127.0.0.1:6006 && yarn exec test-storybook --includeTags=\"play-fn\"\"",
5655
"prepare": "node playwright.prepare.mjs",
5756
"docker:start": "docker-compose up"
5857
},
@@ -85,8 +84,6 @@
8584
"@storybook/addon-webpack5-compiler-babel": "^3.0.6",
8685
"@storybook/react": "^8.6.14",
8786
"@storybook/react-webpack5": "^8.6.14",
88-
"@storybook/test": "^8.6.14",
89-
"@storybook/test-runner": "^0.22.0",
9087
"@testing-library/react": "~16.0.1",
9188
"@testing-library/user-event": "~14.5.2",
9289
"@types/adm-zip": "^0.5.6",

yarn.lock

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8407,7 +8407,7 @@ __metadata:
84078407
storybook-dark-mode: "npm:^4.0.2"
84088408
typescript: "npm:~5.7.2"
84098409
peerDependencies:
8410-
"@rocket.chat/apps-engine": 1.51.0
8410+
"@rocket.chat/apps-engine": 1.52.0-rc.0
84118411
"@rocket.chat/eslint-config": 0.7.0
84128412
"@rocket.chat/fuselage": "*"
84138413
"@rocket.chat/fuselage-hooks": "*"
@@ -8891,8 +8891,6 @@ __metadata:
88918891
"@storybook/addon-webpack5-compiler-babel": "npm:^3.0.6"
88928892
"@storybook/react": "npm:^8.6.14"
88938893
"@storybook/react-webpack5": "npm:^8.6.14"
8894-
"@storybook/test": "npm:^8.6.14"
8895-
"@storybook/test-runner": "npm:^0.22.0"
88968894
"@tanstack/react-query": "npm:~5.65.1"
88978895
"@testing-library/react": "npm:~16.0.1"
88988896
"@testing-library/user-event": "npm:~14.5.2"

0 commit comments

Comments
 (0)