Keldos commited on
Commit
d96e918
·
1 Parent(s): 8161b72

fix: 完善流式输出的代码块显示

Browse files
Files changed (2) hide show
  1. assets/custom.css +1 -1
  2. modules/utils.py +16 -5
assets/custom.css CHANGED
@@ -291,7 +291,7 @@ ol:not(.options), ul:not(.options) {
291
  border-bottom-right-radius: 0 !important;
292
  }
293
 
294
- .message.user {
295
  white-space: pre-wrap;
296
  }
297
 
 
291
  border-bottom-right-radius: 0 !important;
292
  }
293
 
294
+ .message.user p {
295
  white-space: pre-wrap;
296
  }
297
 
modules/utils.py CHANGED
@@ -207,13 +207,24 @@ def convert_before_marked(chat_message):
207
  """
208
  注意不能给输出加缩进, 否则会被marked解析成代码块
209
  """
210
- if '<div class="md-message">' or '<div class="md-message">' in chat_message:
211
  return chat_message
212
  else:
213
- return (f"""\
214
- <div class="raw-message hideM">{escape_markdown(chat_message)}</div>
215
- <div class="md-message">{chat_message}</div>
216
- """)
 
 
 
 
 
 
 
 
 
 
 
217
 
218
  def escape_markdown(text):
219
  """
 
207
  """
208
  注意不能给输出加缩进, 否则会被marked解析成代码块
209
  """
210
+ if '<div class="md-message">' in chat_message:
211
  return chat_message
212
  else:
213
+ code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
214
+ code_blocks = code_block_pattern.findall(chat_message)
215
+ non_code_parts = code_block_pattern.split(chat_message)[::2]
216
+ result = []
217
+
218
+ raw = f'<div class="raw-message hideM">{escape_markdown(chat_message)}</div>'
219
+ for non_code, code in zip(non_code_parts, code_blocks + [""]):
220
+ if non_code.strip():
221
+ result.append(non_code)
222
+ if code.strip():
223
+ code = f"\n```{code}\n```"
224
+ result.append(code)
225
+ result = "".join(result)
226
+ md = f'<div class="md-message">{result}\n</div>'
227
+ return raw + md
228
 
229
  def escape_markdown(text):
230
  """