Tuchuanhuhuhu commited on
Commit
70ec742
1 Parent(s): a8983c5

解决大段代码的渲染问题

Browse files
Files changed (2) hide show
  1. overwrites.py +5 -1
  2. utils.py +6 -2
overwrites.py CHANGED
@@ -30,5 +30,9 @@ def postprocess(
30
  """
31
  if y is None or y == []:
32
  return []
33
- y[-1] = (y[-1][0].replace("\n", "<br>"), convert_mdtext(y[-1][1]))
 
 
 
 
34
  return y
 
30
  """
31
  if y is None or y == []:
32
  return []
33
+ tag_regex = re.compile(r"<\w+>[^<]+</\w+>")
34
+ if tag_regex.search(y[-1][1]):
35
+ y[-1] = (y[-1][0].replace("\n", "<br>"), y[-1][1])
36
+ else:
37
+ y[-1] = (y[-1][0].replace("\n", "<br>"), convert_mdtext(y[-1][1]))
38
  return y
utils.py CHANGED
@@ -86,6 +86,7 @@ def normalize_markdown(md_text: str) -> str:
86
 
87
  def convert_mdtext(md_text):
88
  code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
 
89
  code_blocks = code_block_pattern.findall(md_text)
90
  non_code_parts = code_block_pattern.split(md_text)[::2]
91
 
@@ -93,10 +94,13 @@ def convert_mdtext(md_text):
93
  for non_code, code in zip(non_code_parts, code_blocks + [""]):
94
  if non_code.strip():
95
  non_code = normalize_markdown(non_code)
96
- result.append(mdtex2html.convert(non_code, extensions=["tables"]))
 
 
 
97
  if code.strip():
98
  # _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
99
- code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
100
  code = f"```{code}\n\n```"
101
  code = markdown_to_html_with_syntax_highlight(code)
102
  result.append(code)
 
86
 
87
  def convert_mdtext(md_text):
88
  code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
89
+ inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
90
  code_blocks = code_block_pattern.findall(md_text)
91
  non_code_parts = code_block_pattern.split(md_text)[::2]
92
 
 
94
  for non_code, code in zip(non_code_parts, code_blocks + [""]):
95
  if non_code.strip():
96
  non_code = normalize_markdown(non_code)
97
+ if inline_code_pattern.search(non_code):
98
+ result.append(markdown(non_code, extensions=["tables"]))
99
+ else:
100
+ result.append(mdtex2html.convert(non_code, extensions=["tables"]))
101
  if code.strip():
102
  # _, code = detect_language(code) # 暂时去除代码高亮功能,因为在大段代码的情况下会出现问题
103
+ # code = code.replace("\n\n", "\n") # 暂时去除代码中的空行,因为在大段代码的情况下会出现问题
104
  code = f"```{code}\n\n```"
105
  code = markdown_to_html_with_syntax_highlight(code)
106
  result.append(code)