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
8 changes: 6 additions & 2 deletions src/lib/utils/url-link-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ export function convertUrlsToLinks(text) {
// First, handle code blocks (```...```) to protect them from other processing
const codeBlockRegex = /```([\s\S]*?)```/g;
const codeBlocks = [];
let placeholderPrefix = '__QRYPTCHAT_CODE_BLOCK_';
while (text.includes(placeholderPrefix)) {
placeholderPrefix = `_${placeholderPrefix}`;
}
let textWithPlaceholders = text;
let codeMatch;
let codeIndex = 0;

// Extract code blocks and replace with placeholders
while ((codeMatch = codeBlockRegex.exec(text)) !== null) {
const placeholder = `__QRYPTCHAT_CODE_BLOCK_${codeIndex}__`;
const placeholder = `${placeholderPrefix}${codeIndex}__`;
const codeContent = codeMatch[1];
codeBlocks.push(`<pre><code>${escapeHtml(codeContent)}</code></pre>`);
textWithPlaceholders = textWithPlaceholders.replace(codeMatch[0], placeholder);
Expand Down Expand Up @@ -87,7 +91,7 @@ export function convertUrlsToLinks(text) {

// Restore code blocks from placeholders
codeBlocks.forEach((codeBlock, index) => {
const placeholder = `__QRYPTCHAT_CODE_BLOCK_${index}__`;
const placeholder = `${placeholderPrefix}${index}__`;
result = result.replace(placeholder, codeBlock);
});

Expand Down
10 changes: 10 additions & 0 deletions src/lib/utils/url-link-converter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ describe('convertUrlsToLinks', () => {
expect(html).toContain('&lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt;');
expect(html).not.toContain('<script>');
});

it('preserves text that resembles an internal code block placeholder', () => {
const html = convertUrlsToLinks(
'Literal __QRYPTCHAT_CODE_BLOCK_0__ then ```const value = 1;```'
);

expect(html).toBe(
'Literal __QRYPTCHAT_CODE_BLOCK_0__ then <pre><code>const value = 1;</code></pre>'
);
});
});
Loading