fix(editor): Remove trailing newline from AI Assistant code blocks (no-changelog) (#40731)

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Mutasem Aldmour
2026-10-09 09:24:16 +00:00
committed by GitHub
co-authored by Claude Sonnet 5.5
parent 24b9db41b3
commit 9f85b8379c
2 changed files with 38 additions and 0 deletions
@@ -0,0 +1,29 @@
import { useMarkdown } from './useMarkdown';
describe('useMarkdown', () => {
const { renderMarkdown } = useMarkdown();
it('should not add a trailing newline inside a fenced code block', () => {
const html = renderMarkdown('```\nhttps://example.com/webhook/abc\n```');
expect(html).toContain('<code>https://example.com/webhook/abc</code>');
});
it('should keep newlines between lines of a fenced code block', () => {
const html = renderMarkdown('```\nline 1\nline 2\n```');
expect(html).toContain('<code>line 1\nline 2</code>');
});
it('should keep the language class on a fenced code block', () => {
const html = renderMarkdown('```js\nconst a = 1;\n```');
expect(html).toContain('<code class="language-js">const a = 1;</code>');
});
it('should keep an intentional blank last line in a fenced code block', () => {
const html = renderMarkdown('```\nline 1\n\n```');
expect(html).toContain('<code>line 1\n</code>');
});
});
@@ -17,6 +17,15 @@ export function useMarkdown() {
},
});
// markdown-it ends fenced code with a newline. Triple-click copy would include it.
const renderFence = md.renderer.rules.fence;
md.renderer.rules.fence = (tokens, idx, options, env, self) => {
tokens[idx].content = tokens[idx].content.replace(/\n$/, '');
return renderFence
? renderFence(tokens, idx, options, env, self)
: self.renderToken(tokens, idx, options);
};
function renderMarkdown(content: string) {
try {
return md.render(content);