Skip to content

Commit 0d517bf

Browse files
committed
Refine file content extraction within markdown code blocks to correctly handle nested structures and exclude surrounding non-code text
1 parent 7a5981c commit 0d517bf

2 files changed

Lines changed: 37 additions & 23 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.ts‎

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.ts
12
import { cleanup_api_response } from '@/utils/cleanup-api-response'
23
import { extract_path_from_line_of_code } from '@shared/utils/extract-path-from-line-of-code'
34
import { Diff, extract_diffs } from './extract-diff-patches'
@@ -339,6 +340,12 @@ export const parse_multiple_files = (params: {
339340

340341
// Handle nested backticks
341342
if (trimmed_line.startsWith('```') && trimmed_line !== '```') {
343+
if (
344+
backtick_nesting_level === 1 &&
345+
(current_language === 'markdown' || current_language === 'md')
346+
) {
347+
is_first_content_line = true
348+
}
342349
backtick_nesting_level++
343350
} else if (
344351
trimmed_line == '```' &&
@@ -505,25 +512,24 @@ export const parse_multiple_files = (params: {
505512
}
506513

507514
if (extracted_filename) {
508-
if (
509-
(current_language === 'markdown' || current_language === 'md') &&
510-
current_file_name
511-
) {
512-
// This is a new file inside a markdown block. Save the previous one.
513-
const cleaned_content = current_content.trim()
514-
if (has_real_code(cleaned_content)) {
515-
const file_key = `${
516-
current_workspace_name || ''
517-
}:${current_file_name}`
518-
if (files_map.has(file_key)) {
519-
const existing_file = files_map.get(file_key)!
520-
existing_file.content += '\n\n' + cleaned_content
521-
} else {
522-
files_map.set(file_key, {
523-
file_path: current_file_name,
524-
content: cleaned_content,
525-
workspace_name: current_workspace_name
526-
})
515+
if (current_language === 'markdown' || current_language === 'md') {
516+
if (current_file_name) {
517+
// This is a new file inside a markdown block. Save the previous one.
518+
const cleaned_content = current_content.trim()
519+
if (has_real_code(cleaned_content)) {
520+
const file_key = `${
521+
current_workspace_name || ''
522+
}:${current_file_name}`
523+
if (files_map.has(file_key)) {
524+
const existing_file = files_map.get(file_key)!
525+
existing_file.content += '\n\n' + cleaned_content
526+
} else {
527+
files_map.set(file_key, {
528+
file_path: current_file_name,
529+
content: cleaned_content,
530+
workspace_name: current_workspace_name
531+
})
532+
}
527533
}
528534
}
529535
current_content = ''
@@ -579,10 +585,16 @@ export const parse_multiple_files = (params: {
579585
line.trim().startsWith('```')
580586
)
581587
) {
582-
if (current_content) {
583-
current_content += '\n' + line
584-
} else {
585-
current_content = line
588+
if (
589+
!lang_is_markdown ||
590+
is_markdown_file ||
591+
backtick_nesting_level > 1
592+
) {
593+
if (current_content) {
594+
current_content += '\n' + line
595+
} else {
596+
current_content = line
597+
}
586598
}
587599
}
588600

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/test-cases/unclosed-markdown-wrapper/unclosed-markdown-wrapper.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
console.log('lorem');
55
```
66

7+
Lorem ipsum.
8+
79
```ts
810
// src/ipsum.ts
911
console.log('ipsum');

0 commit comments

Comments
 (0)