mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-12 02:19:43 +00:00
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:
co-authored by
Claude Sonnet 5.5
parent
24b9db41b3
commit
9f85b8379c
+29
@@ -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>');
|
||||
});
|
||||
});
|
||||
+9
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user