@@ -210,35 +210,33 @@ open class AddApplyFileDiffLinks {
210210 )
211211 }
212212 val headerPattern = """ (?<![^\n])#+\s*([^\n]+)""" .toRegex() // capture filename
213- val codeblockPattern = """ (?s)(?<![^\n])```([^\n]*)(\n.*?\n)```""" .toRegex() // capture filename
213+ val codeblockPattern = """ (?s)(?<![^\n])```([^\n]*)\n(.*?)\n```""" .toRegex() // capture filename
214+ val codeblockGreedyPattern = """ (?s)(?<![^\n])```([^\n]*)\n(.*)\n```""" .toRegex() // capture filename
214215 val headers = headerPattern.findAll(response).map { it.range to it.groupValues[1 ] }.toList()
215- val findAll = codeblockPattern.findAll(response).toList()
216- val codeblocks = findAll.filter { block ->
216+ val findAll = codeblockPattern.findAll(response).toList().groupBy { block -> headers.lastOrNull { it.first.last <= block.range.first }?.second ? : defaultFile }
217+ val findAllGreedy = codeblockGreedyPattern.findAll(response).toList().groupBy { block -> headers.lastOrNull { it.first.last <= block.range.first }?.second ? : defaultFile }
218+ val resolvedMatches = mutableMapOf<String ?,List <MatchResult >>()
219+ if (findAllGreedy.values.flatten().any { it.groupValues[1 ] == " markdown" }) {
220+ resolvedMatches.putAll(findAllGreedy)
221+ } else {
222+ resolvedMatches.putAll(findAll)
223+ }
224+ val codeblocks = resolvedMatches.filter { (header, block) ->
217225 try {
218- val header = headers.lastOrNull { it.first.last <= block.range.first }?.second ? : defaultFile
219- if (header == null ) {
220- return @filter false
221- }
222- val filename = resolve(root, header)
223- ! root.resolve(filename).toFile().exists()
226+ ! root.resolve(resolve(root, header ? : return @filter false )).toFile().exists()
224227 } catch (e: Throwable ) {
225228 log.info(" Error processing code block" , e)
226229 false
227230 }
228- }.map { it.range to it }.toList()
229- val patchBlocks = findAll .filter { block ->
231+ }.flatMap { it.value }. map { it.range to it }.toList()
232+ val patchBlocks = resolvedMatches .filter { (header, block) ->
230233 try {
231- val header = headers.lastOrNull { it.first.last <= block.range.first }?.second ? : defaultFile
232- if (header == null ) {
233- return @filter false
234- }
235- val filename = resolve(root, header)
236- root.resolve(filename).toFile().exists()
234+ root.resolve(resolve(root, header ? : return @filter false )).toFile().exists()
237235 } catch (e: Throwable ) {
238236 log.info(" Error processing code block" , e)
239237 false
240238 }
241- }.map { it.range to it }.toList()
239+ }.flatMap { it.value }. map { it.range to it }.toList()
242240
243241 // Process diff blocks and add patch links
244242 val withPatchLinks: String = patchBlocks.foldIndexed(response) { index, markdown, diffBlock ->
0 commit comments