Skip to content

Commit 5dbb63f

Browse files
fix(widget): drop the whole language tag from a code block (#579)
Co-authored-by: Anurag Verma <78868769+anurag629@users.noreply.github.com>
1 parent b90d9d7 commit 5dbb63f

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎.changeset/fence-language-tag.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@chatcops/widget": patch
3+
---
4+
5+
A code block tagged `c++`, `c#` or `objective-c` no longer shows part of the tag as its first line, and a one-line code block keeps all of its text.

‎packages/widget/src/markdown.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function renderMarkdown(input: string): string {
4242

4343
// Code blocks (``` ... ```) — stashed before any markdown transformation so
4444
// their content stays literal and their newlines survive the <br> step.
45-
html = html.replace(/```(?:\w*)\n?([\s\S]*?)```/g, (_match, code: string) => {
45+
html = html.replace(/```(?:[^`\n]*\n)?([\s\S]*?)```/g, (_match, code: string) => {
4646
return stashHtml(`<pre><code>${code.trim()}</code></pre>`);
4747
});
4848

‎packages/widget/tests/markdown.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ describe('renderMarkdown', () => {
2121
expect(result).toContain('const x = 1;');
2222
});
2323

24+
it.each(['c++', 'c#', 'objective-c', 'ts'])('drops a %s language tag from a code block', (lang) => {
25+
expect(renderMarkdown(`\`\`\`${lang}\nint x;\n\`\`\``)).toBe('<pre><code>int x;</code></pre>');
26+
});
27+
28+
it('keeps all the text of a one-line code block', () => {
29+
expect(renderMarkdown('```const x```')).toBe('<pre><code>const x</code></pre>');
30+
});
31+
32+
it('drops a language tag with a space after it', () => {
33+
expect(renderMarkdown('```ts \nconst x = 1;\n```')).toBe('<pre><code>const x = 1;</code></pre>');
34+
});
35+
36+
it('leaves a code block that is still streaming as text', () => {
37+
expect(renderMarkdown('```ts\nconst x = 1;')).toBe('```ts<br>const x = 1;');
38+
expect(renderMarkdown('```c++\nint x;')).toBe('```c++<br>int x;');
39+
});
40+
2441
it('renders links', () => {
2542
const result = renderMarkdown('[Click here](https://example.com)');
2643
expect(result).toContain('<a href="https://example.com"');

0 commit comments

Comments
 (0)