Parse code blocks

This commit is contained in:
andrepimenta
2025-07-09 13:36:25 +01:00
parent b261f5cb8a
commit 234516c1ef
2 changed files with 69 additions and 1 deletions

View File

@@ -830,6 +830,43 @@ const styles = `
padding-left: 6px;
}
/* Code blocks generated by markdown parser only */
.message-content pre.code-block {
background-color: var(--vscode-textCodeBlock-background);
border: 1px solid var(--vscode-panel-border);
border-radius: 4px;
padding: 12px;
margin: 8px 0;
overflow-x: auto;
font-family: var(--vscode-editor-font-family);
font-size: 13px;
line-height: 1.5;
white-space: pre;
}
.message-content pre.code-block code {
background: none;
border: none;
padding: 0;
color: var(--vscode-editor-foreground);
}
.code-line {
white-space: pre-wrap;
word-break: break-word;
}
/* Inline code */
.message-content code {
background-color: var(--vscode-textCodeBlock-background);
border: 1px solid var(--vscode-panel-border);
border-radius: 3px;
padding: 2px 4px;
font-family: var(--vscode-editor-font-family);
font-size: 0.9em;
color: var(--vscode-editor-foreground);
}
.priority-badge {
display: inline-block;
padding: 2px 6px;

View File

@@ -2260,13 +2260,44 @@ const html = `<!DOCTYPE html>
function parseSimpleMarkdown(markdown) {
const lines = markdown.split('\\n');
// First, handle code blocks before line-by-line processing
let processedMarkdown = markdown;
// Handle multi-line code blocks with triple backticks
// Using RegExp constructor to avoid backtick conflicts in template literal
const codeBlockRegex = new RegExp('\\\`\\\`\\\`(\\\\w*)\\n([\\\\s\\\\S]*?)\\\`\\\`\\\`', 'g');
processedMarkdown = processedMarkdown.replace(codeBlockRegex, function(match, lang, code) {
const language = lang || 'plaintext';
// Process code line by line to preserve formatting like diff implementation
const codeLines = code.split('\\n');
let codeHtml = '';
for (const line of codeLines) {
const escapedLine = escapeHtml(line);
codeHtml += '<div class="code-line">' + escapedLine + '</div>';
}
return '\\n<pre class="code-block"><code class="language-' + language + '">' + codeHtml + '</code></pre>\\n';
});
// Handle inline code with single backticks
const inlineCodeRegex = new RegExp('\\\`([^\\\`]+)\\\`', 'g');
processedMarkdown = processedMarkdown.replace(inlineCodeRegex, '<code>$1</code>');
const lines = processedMarkdown.split('\\n');
let html = '';
let inUnorderedList = false;
let inOrderedList = false;
for (let line of lines) {
line = line.trim();
// Check if this is a code block
if (line.includes('<pre class="code-block"><code')) {
// This is a complete code block on potentially multiple lines
html += line;
continue;
}
// Bold
line = line.replace(/\\*\\*(.*?)\\*\\*/g, '<strong>$1</strong>');