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

BREAKING: 适配sanitize DOM的gradio版本

Browse files
Files changed (4) hide show
  1. assets/custom.css +11 -4
  2. assets/custom.js +2 -2
  3. modules/overwrites.py +2 -2
  4. modules/utils.py +43 -11
assets/custom.css CHANGED
@@ -291,17 +291,24 @@ ol:not(.options), ul:not(.options) {
291
  border-bottom-right-radius: 0 !important;
292
  }
293
 
294
- .message p {
 
 
 
 
295
  margin-top: 0.6em !important;
296
  margin-bottom: 0.6em !important;
297
  }
298
- .message p:first-child { margin-top: 0 !important; }
299
- .message p:last-of-type { margin-bottom: 0 !important; }
300
 
301
  .message .md-message {
302
  display: block;
303
  padding: 0 !important;
304
  }
 
 
 
305
  .message .raw-message {
306
  display: block;
307
  padding: 0 !important;
@@ -410,7 +417,7 @@ thead th {
410
  white-space: break-spaces;
411
  font-family: var(--font-mono);
412
  border-radius: 6px;
413
- /* margin: 0 2px 0 2px; */
414
  padding: .2em .4em .1em .4em;
415
  background-color: rgba(175,184,193,0.2);
416
  }
 
291
  border-bottom-right-radius: 0 !important;
292
  }
293
 
294
+ .message.user {
295
+ white-space: pre-wrap;
296
+ }
297
+
298
+ .message .md-message p {
299
  margin-top: 0.6em !important;
300
  margin-bottom: 0.6em !important;
301
  }
302
+ .message .md-message p:first-child { margin-top: 0 !important; }
303
+ .message .md-message p:last-of-type { margin-bottom: 0 !important; }
304
 
305
  .message .md-message {
306
  display: block;
307
  padding: 0 !important;
308
  }
309
+ .message .raw-message p {
310
+ margin:0 !important;
311
+ }
312
  .message .raw-message {
313
  display: block;
314
  padding: 0 !important;
 
417
  white-space: break-spaces;
418
  font-family: var(--font-mono);
419
  border-radius: 6px;
420
+ margin: 0 2px 0 2px;
421
  padding: .2em .4em .1em .4em;
422
  background-color: rgba(175,184,193,0.2);
423
  }
assets/custom.js CHANGED
@@ -239,10 +239,10 @@ function showOrHideUserInfo() {
239
  function toggleDarkMode(isEnabled) {
240
  if (isEnabled) {
241
  document.body.classList.add("dark");
242
- // document.body.style.setProperty("background-color", "var(--neutral-950)", "important");
243
  } else {
244
  document.body.classList.remove("dark");
245
- // document.body.style.backgroundColor = "";
246
  }
247
  }
248
  function adjustDarkMode() {
 
239
  function toggleDarkMode(isEnabled) {
240
  if (isEnabled) {
241
  document.body.classList.add("dark");
242
+ document.body.style.setProperty("background-color", "var(--neutral-950)", "important");
243
  } else {
244
  document.body.classList.remove("dark");
245
+ document.body.style.backgroundColor = "";
246
  }
247
  }
248
  function adjustDarkMode() {
modules/overwrites.py CHANGED
@@ -66,8 +66,8 @@ def postprocess_chat_messages(
66
  # chat_message = chat_message.replace(" ", " ")
67
  if role == "bot":
68
  chat_message = convert_before_marked(chat_message)
69
- # elif role == "user":
70
- # chat_message = convert_asis(chat_message)
71
  return chat_message
72
  else:
73
  raise ValueError(f"Invalid message for Chatbot component: {chat_message}")
 
66
  # chat_message = chat_message.replace(" ", " ")
67
  if role == "bot":
68
  chat_message = convert_before_marked(chat_message)
69
+ elif role == "user":
70
+ chat_message = escape_markdown(chat_message)
71
  return chat_message
72
  else:
73
  raise ValueError(f"Invalid message for Chatbot component: {chat_message}")
modules/utils.py CHANGED
@@ -133,7 +133,7 @@ def count_token(message):
133
  return length
134
 
135
 
136
- def markdown_to_html_with_syntax_highlight(md_str):
137
  def replacer(match):
138
  lang = match.group(1) or "text"
139
  code = match.group(2)
@@ -155,7 +155,7 @@ def markdown_to_html_with_syntax_highlight(md_str):
155
  return html_str
156
 
157
 
158
- def normalize_markdown(md_text: str) -> str:
159
  lines = md_text.split("\n")
160
  normalized_lines = []
161
  inside_list = False
@@ -179,7 +179,7 @@ def normalize_markdown(md_text: str) -> str:
179
  return "\n".join(normalized_lines)
180
 
181
 
182
- def convert_mdtext(md_text):
183
  code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
184
  inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
185
  code_blocks = code_block_pattern.findall(md_text)
@@ -204,19 +204,51 @@ def convert_mdtext(md_text):
204
  return output
205
 
206
  def convert_before_marked(chat_message):
207
- return (
208
- f'<div class="raw-message hideM">{chat_message}</div><div class="md-message">{chat_message}</div>{ALREADY_CONVERTED_MARK}'
209
- )
210
-
211
-
212
- def convert_asis(userinput):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  return (
214
  f'<p style="white-space:pre-wrap;">{html.escape(userinput)}</p>'
215
  + ALREADY_CONVERTED_MARK
216
  )
217
 
218
 
219
- def detect_converted_mark(userinput):
220
  try:
221
  if userinput.endswith(ALREADY_CONVERTED_MARK):
222
  return True
@@ -226,7 +258,7 @@ def detect_converted_mark(userinput):
226
  return True
227
 
228
 
229
- def detect_language(code):
230
  if code.startswith("\n"):
231
  first_line = ""
232
  else:
 
133
  return length
134
 
135
 
136
+ def markdown_to_html_with_syntax_highlight(md_str): # deprecated
137
  def replacer(match):
138
  lang = match.group(1) or "text"
139
  code = match.group(2)
 
155
  return html_str
156
 
157
 
158
+ def normalize_markdown(md_text: str) -> str: # deprecated
159
  lines = md_text.split("\n")
160
  normalized_lines = []
161
  inside_list = False
 
179
  return "\n".join(normalized_lines)
180
 
181
 
182
+ def convert_mdtext(md_text): # deprecated
183
  code_block_pattern = re.compile(r"```(.*?)(?:```|$)", re.DOTALL)
184
  inline_code_pattern = re.compile(r"`(.*?)`", re.DOTALL)
185
  code_blocks = code_block_pattern.findall(md_text)
 
204
  return output
205
 
206
  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
+ """
220
+ Escape Markdown special characters to HTML-safe equivalents.
221
+ """
222
+ escape_chars = {
223
+ '_': '&#95;',
224
+ '*': '&#42;',
225
+ '[': '&#91;',
226
+ ']': '&#93;',
227
+ '(': '&#40;',
228
+ ')': '&#41;',
229
+ '{': '&#123;',
230
+ '}': '&#125;',
231
+ '#': '&#35;',
232
+ '+': '&#43;',
233
+ '-': '&#45;',
234
+ '.': '&#46;',
235
+ '!': '&#33;',
236
+ '`': '&#96;',
237
+ '>': '&#62;',
238
+ '<': '&#60;',
239
+ '|': '&#124;'
240
+ }
241
+ return ''.join(escape_chars.get(c, c) for c in text)
242
+
243
+
244
+ def convert_asis(userinput): # deprecated
245
  return (
246
  f'<p style="white-space:pre-wrap;">{html.escape(userinput)}</p>'
247
  + ALREADY_CONVERTED_MARK
248
  )
249
 
250
 
251
+ def detect_converted_mark(userinput): # deprecated
252
  try:
253
  if userinput.endswith(ALREADY_CONVERTED_MARK):
254
  return True
 
258
  return True
259
 
260
 
261
+ def detect_language(code): # deprecated
262
  if code.startswith("\n"):
263
  first_line = ""
264
  else: