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
7 changes: 4 additions & 3 deletions lib/confluence-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,10 +1073,11 @@ class ConfluenceClient {
// so they appear as literal characters in the CDATA output
const decodedCode = code.replace(/\n$/, '')
.replace(/"/g, '"')
.replace(/&/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
return `<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">${language}</ac:parameter><ac:plain-text-body><![CDATA[${decodedCode}]]></ac:plain-text-body></ac:structured-macro>`;
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&'); // & last to avoid double-decoding
const safeCode = decodedCode.replace(/]]>/g, ']]]]><![CDATA[>');
return `<ac:structured-macro ac:name="code"><ac:parameter ac:name="language">${language}</ac:parameter><ac:plain-text-body><![CDATA[${safeCode}]]></ac:plain-text-body></ac:structured-macro>`;
});

// Convert inline code
Expand Down
10 changes: 10 additions & 0 deletions tests/confluence-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ describe('ConfluenceClient', () => {
expect(result).toContain('<td><p>Cell 1</p></td>');
});

test('should escape CDATA terminators in code blocks', () => {
const markdown = '```xml\n<![CDATA[some data]]>\n```';
const result = client.markdownToStorage(markdown);

expect(result).toContain('<![CDATA[');
expect(result).toContain(']]]]><![CDATA[>');
// The literal ]]> from user code should not appear unescaped
expect(result).not.toContain('some data]]>');
});

test('should convert links to smart link format on Cloud instances', () => {
const markdown = '[Example Link](https://example.com)';
const result = client.markdownToStorage(markdown);
Expand Down
Loading