fix nested codeblock escaping

This commit is contained in:
dousha 2025-01-06 15:14:54 +08:00
parent 514bf7ac93
commit b89f4e83fd

View File

@ -109,6 +109,10 @@ def fix_nesting_code_blocks(content):
return '\n'.join(out) return '\n'.join(out)
def fix_code_block_content(content):
return content.replace(r'\_', '_').replace(r'\*', '*')
def trim_code_blocks(content): def trim_code_blocks(content):
codeblock_regex = re.compile(r'```(.+?)```', re.DOTALL) codeblock_regex = re.compile(r'```(.+?)```', re.DOTALL)
pos = 0 pos = 0
@ -116,7 +120,7 @@ def trim_code_blocks(content):
while m := codeblock_regex.search(content, pos): while m := codeblock_regex.search(content, pos):
out = out + content[pos:m.start()].strip() out = out + content[pos:m.start()].strip()
parts = re.split(r'[\n\r ]', m.group(1), 1) parts = re.split(r'[\n\r ]', m.group(1), 1)
out = out + '\n\n```' + parts[0].strip() + '\n' + parts[1].strip() + '\n```\n\n' out = out + '\n\n```' + parts[0].strip() + '\n' + fix_code_block_content(parts[1]).strip() + '\n```\n\n'
pos = m.end() pos = m.end()
return out + content[pos:].strip() return out + content[pos:].strip()