@@ -338,6 +338,31 @@ local function resolve_code_spans_and_add_links(s)
338338 end
339339 end
340340
341+ -- Extract code blocks to prevent backtick parity shifting
342+ local code_blocks = {}
343+ local block_count = 0
344+ local cursor = 1
345+ local parts = {}
346+ while true do
347+ local start_idx , end_idx = main_content :find (" ```" , cursor )
348+ if not start_idx then
349+ table.insert (parts , main_content :sub (cursor ))
350+ break
351+ end
352+ local close_start , close_end = main_content :find (" ```" , end_idx + 1 )
353+ if not close_start then
354+ table.insert (parts , main_content :sub (cursor ))
355+ break
356+ end
357+ local block = main_content :sub (start_idx , close_end )
358+ block_count = block_count + 1
359+ code_blocks [block_count ] = block
360+ table.insert (parts , main_content :sub (cursor , start_idx - 1 ))
361+ table.insert (parts , string.format (" \001 CODEBLOCK%d\002 " , block_count ))
362+ cursor = close_end + 1
363+ end
364+ main_content = table.concat (parts )
365+
341366 local new_refs = {}
342367 local modified_content = main_content :gsub (" ([%[%w_]?)`([^`]+)`([%]%w_]?)" , function (before , content , after )
343368 if before == " [" or after == " ]" then
@@ -373,6 +398,13 @@ local function resolve_code_spans_and_add_links(s)
373398 return before .. " `" .. content .. " `" .. after
374399 end )
375400
401+ -- Restore code blocks
402+ for i = 1 , block_count do
403+ modified_content = modified_content :gsub (" \001 CODEBLOCK" .. i .. " \002 " , function ()
404+ return code_blocks [i ]
405+ end )
406+ end
407+
376408 for name , url in pairs (new_refs ) do
377409 existing_refs [name ] = url
378410 end
0 commit comments