POLRAMBORA commited on
Commit
c03d572
·
verified ·
1 Parent(s): ca81988

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -5,8 +5,7 @@ import os
5
  import markdown
6
  from markdown.extensions.codehilite import CodeHiliteExtension
7
  import markdown.extensions.fenced_code
8
- from markdown_it import MarkdownIt
9
- from markdown_it.plugins import plugin
10
 
11
  API_URL = "https://host.palple.polrambora.com/pmsq"
12
 
@@ -149,16 +148,34 @@ def render_message(history):
149
 
150
 
151
  def escape_and_format(text):
152
- md = MarkdownIt()
 
153
 
154
- html = md.render(text)
155
 
156
- html = html.replace("\n", "<br>")
157
 
158
- allowed_tags = {'<b>', '</b>', '<i>', '</i>', '<h1>', '</h1>', '<h2>', '</h2>', '<h3>', '</h3>', '<code>', '</code>', '<br>', '<ul>', '</ul>', '<li>', '</li>', '<blockquote>', '</blockquote>', '<p>', '</p>'}
159
- html = ''.join(f"&#{ord(char)};" if char not in allowed_tags else char for char in html)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
- return html
162
 
163
  css="""
164
  .chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}
 
5
  import markdown
6
  from markdown.extensions.codehilite import CodeHiliteExtension
7
  import markdown.extensions.fenced_code
8
+ import mistune
 
9
 
10
  API_URL = "https://host.palple.polrambora.com/pmsq"
11
 
 
148
 
149
 
150
  def escape_and_format(text):
151
+ allowed_tags = {'<b>', '</b>', '<i>', '</i>', '<h1>', '</h1>', '<h2>', '</h2>', '<h3>', '</h3>',
152
+ '<code>', '</code>', '<br>', '<ul>', '</ul>', '<li>', '</li>', '<blockquote>', '</blockquote>', '<p>', '</p>'}
153
 
154
+ markdown = mistune.create_markdown(renderer="html")
155
 
156
+ html = markdown(text)
157
 
158
+ filtered_html = ""
159
+ i = 0
160
+ while i < len(html):
161
+ if html[i] == '<':
162
+ tag_end = html.find('>', i) + 1
163
+ if tag_end == 0:
164
+ filtered_html += f"&#{ord(html[i])};"
165
+ i += 1
166
+ continue
167
+
168
+ tag = html[i:tag_end]
169
+ if tag in allowed_tags:
170
+ filtered_html += tag
171
+ else:
172
+ filtered_html += ''.join(f"&#{ord(c)};" for c in tag)
173
+ i = tag_end
174
+ else:
175
+ filtered_html += f"&#{ord(html[i])};"
176
+ i += 1
177
 
178
+ return filtered_html
179
 
180
  css="""
181
  .chatbox {height: 400px; overflow: auto; border: 1px solid #262626; padding: 10px; background-color: #171717; display: flex; flex-direction: column-reverse;}