Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fence-language-tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chatcops/widget": patch
---

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.
2 changes: 1 addition & 1 deletion packages/widget/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function renderMarkdown(input: string): string {

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

Expand Down
17 changes: 17 additions & 0 deletions packages/widget/tests/markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ describe('renderMarkdown', () => {
expect(result).toContain('const x = 1;');
});

it.each(['c++', 'c#', 'objective-c', 'ts'])('drops a %s language tag from a code block', (lang) => {
expect(renderMarkdown(`\`\`\`${lang}\nint x;\n\`\`\``)).toBe('<pre><code>int x;</code></pre>');
});

it('keeps all the text of a one-line code block', () => {
expect(renderMarkdown('```const x```')).toBe('<pre><code>const x</code></pre>');
});

it('drops a language tag with a space after it', () => {
expect(renderMarkdown('```ts \nconst x = 1;\n```')).toBe('<pre><code>const x = 1;</code></pre>');
});

it('leaves a code block that is still streaming as text', () => {
expect(renderMarkdown('```ts\nconst x = 1;')).toBe('```ts<br>const x = 1;');
expect(renderMarkdown('```c++\nint x;')).toBe('```c++<br>int x;');
});

it('renders links', () => {
const result = renderMarkdown('[Click here](https://example.com)');
expect(result).toContain('<a href="https://example.com"');
Expand Down