Skip to content

Commit 3e44d8b

Browse files
committed
Enhance clipboard-parser to correctly extract file content when nested markdown code blocks are present, preventing incorrect content inclusion
1 parent 3dcc2d6 commit 3e44d8b

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ describe('clipboard-parser', () => {
285285
)
286286
})
287287

288+
it('parses markdown files with nested markdown code blocks', () => {
289+
const test_case = 'markdown-inside-markdown'
290+
const text = load_test_case_file(test_case, `${test_case}.txt`)
291+
const result = parse_multiple_files({
292+
response: text,
293+
is_single_root_folder_workspace: true
294+
})
295+
296+
expect(result).toHaveLength(1)
297+
expect(result[0].file_path).toBe('src/lorem.ts')
298+
expect(result[0].content).toBe(
299+
load_test_case_file(test_case, 'file-1.txt')
300+
)
301+
})
302+
288303
it('parses PHP files when the opening tag appears before the file path comment', () => {
289304
const test_case = 'php-opening-tag'
290305
const text = load_test_case_file(test_case, `${test_case}.txt`)

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ export const parse_multiple_files = (params: {
346346
(current_content.trim() == '' ||
347347
((current_language == 'markdown' || current_language == 'md') &&
348348
i > 0 &&
349-
(lines[i - 1].trim() == '' || lines[i - 1].trim() == '```'))) &&
349+
(lines[i - 1].trim() == '' ||
350+
lines[i - 1].trim().endsWith('```')))) &&
350351
(() => {
351352
for (let j = i + 1; j < lines.length; j++) {
352353
const next_line = lines[j].trim()
@@ -505,6 +506,13 @@ export const parse_multiple_files = (params: {
505506
params.is_single_root_folder_workspace
506507
)
507508
current_file_name = relative_path
509+
if (
510+
backtick_nesting_level > 1 &&
511+
(current_language === 'markdown' || current_language === 'md')
512+
) {
513+
current_content = ''
514+
}
515+
508516
if (workspace_name) {
509517
current_workspace_name = workspace_name
510518
}
@@ -529,12 +537,32 @@ export const parse_multiple_files = (params: {
529537
continue
530538
}
531539

532-
is_first_content_line = false
540+
const lang_is_markdown =
541+
current_language === 'markdown' || current_language === 'md'
533542

534-
if (current_content) {
535-
current_content += '\n' + line
536-
} else {
537-
current_content = line
543+
const is_markdown_file = current_file_name.endsWith('.md')
544+
545+
if (
546+
is_markdown_file ||
547+
!(
548+
lang_is_markdown &&
549+
current_file_name &&
550+
line.trim().startsWith('```')
551+
)
552+
) {
553+
if (current_content) {
554+
current_content += '\n' + line
555+
} else {
556+
current_content = line
557+
}
558+
}
559+
560+
if (
561+
is_first_content_line &&
562+
line.trim() !== '' &&
563+
!line.trim().startsWith('```')
564+
) {
565+
is_first_content_line = false
538566
}
539567
}
540568
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello World!')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```markdown
2+
```ts
3+
// src/lorem.ts
4+
console.log('Hello World!')
5+
```
6+
```

0 commit comments

Comments
 (0)