Spaces:
Sleeping
Sleeping
Keldos commited on
Commit ·
f029128
1
Parent(s): 1c21e1a
feat: 更改在对话气泡中切换md渲染显示
Browse files- assets/custom.css +12 -2
- assets/custom.js +26 -10
- modules/utils.py +5 -4
assets/custom.css
CHANGED
|
@@ -229,7 +229,7 @@ ol:not(.options), ul:not(.options) {
|
|
| 229 |
}
|
| 230 |
}
|
| 231 |
/* 对话气泡 */
|
| 232 |
-
|
| 233 |
border-radius: var(--radius-xl) !important;
|
| 234 |
border: none;
|
| 235 |
padding: var(--spacing-xl) !important;
|
|
@@ -247,7 +247,17 @@ ol:not(.options), ul:not(.options) {
|
|
| 247 |
width: auto !important;
|
| 248 |
border-bottom-right-radius: 0 !important;
|
| 249 |
}
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
display: none;
|
| 252 |
}
|
| 253 |
|
|
|
|
| 229 |
}
|
| 230 |
}
|
| 231 |
/* 对话气泡 */
|
| 232 |
+
.message {
|
| 233 |
border-radius: var(--radius-xl) !important;
|
| 234 |
border: none;
|
| 235 |
padding: var(--spacing-xl) !important;
|
|
|
|
| 247 |
width: auto !important;
|
| 248 |
border-bottom-right-radius: 0 !important;
|
| 249 |
}
|
| 250 |
+
|
| 251 |
+
.message .md-message {
|
| 252 |
+
display: block;
|
| 253 |
+
padding: 0 !important;
|
| 254 |
+
}
|
| 255 |
+
.message .raw-message {
|
| 256 |
+
display: block;
|
| 257 |
+
padding: 0 !important;
|
| 258 |
+
white-space: pre-wrap;
|
| 259 |
+
}
|
| 260 |
+
.raw-message.hideM, .md-message.hideM {
|
| 261 |
display: none;
|
| 262 |
}
|
| 263 |
|
assets/custom.js
CHANGED
|
@@ -277,20 +277,21 @@ const copiedIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2
|
|
| 277 |
const toggleIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><rect x="4" y="4" width="16" height="16" rx="2" ry="2"></rect><circle cx="8" cy="12" r="2"></circle><circle cx="16" cy="12" r="2"></circle></svg></span>';
|
| 278 |
|
| 279 |
function addChuanhuButton(botElement) {
|
| 280 |
-
var copyButton = null;
|
| 281 |
-
var toggleButton = null;
|
| 282 |
var rawMessage = null;
|
|
|
|
| 283 |
rawMessage = botElement.querySelector('.raw-message');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
copyButton = botElement.querySelector('button.copy-bot-btn');
|
| 285 |
toggleButton = botElement.querySelector('button.toggle-md-btn');
|
|
|
|
| 286 |
if (!rawMessage) return;
|
| 287 |
-
if (copyButton)
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
toggleButton.remove();
|
| 292 |
-
}
|
| 293 |
-
rawMessage.style.display = "none";
|
| 294 |
var copyButton = document.createElement('button');
|
| 295 |
copyButton.classList.add('chuanhu-btn');
|
| 296 |
copyButton.classList.add('copy-bot-btn');
|
|
@@ -317,11 +318,26 @@ function addChuanhuButton(botElement) {
|
|
| 317 |
toggleButton.setAttribute('aria-label', 'Toggle');
|
| 318 |
toggleButton.innerHTML = toggleIcon;
|
| 319 |
toggleButton.addEventListener('click', () => {
|
| 320 |
-
|
|
|
|
|
|
|
| 321 |
});
|
| 322 |
botElement.insertBefore(toggleButton, copyButton);
|
| 323 |
}
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
var rendertime = 0; // for debugging
|
| 326 |
var mathjaxUpdated = false;
|
| 327 |
|
|
|
|
| 277 |
const toggleIcon = '<span><svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" height=".8em" width=".8em" xmlns="http://www.w3.org/2000/svg"><rect x="4" y="4" width="16" height="16" rx="2" ry="2"></rect><circle cx="8" cy="12" r="2"></circle><circle cx="16" cy="12" r="2"></circle></svg></span>';
|
| 278 |
|
| 279 |
function addChuanhuButton(botElement) {
|
|
|
|
|
|
|
| 280 |
var rawMessage = null;
|
| 281 |
+
var mdMessage = null;
|
| 282 |
rawMessage = botElement.querySelector('.raw-message');
|
| 283 |
+
mdMessage = botElement.querySelector('.md-message');
|
| 284 |
+
var renderMarkdown = true;
|
| 285 |
+
var copyButton = null;
|
| 286 |
+
var toggleButton = null;
|
| 287 |
copyButton = botElement.querySelector('button.copy-bot-btn');
|
| 288 |
toggleButton = botElement.querySelector('button.toggle-md-btn');
|
| 289 |
+
|
| 290 |
if (!rawMessage) return;
|
| 291 |
+
if (copyButton) copyButton.remove();
|
| 292 |
+
if (toggleButton) toggleButton.remove();
|
| 293 |
+
|
| 294 |
+
// Copy bot button
|
|
|
|
|
|
|
|
|
|
| 295 |
var copyButton = document.createElement('button');
|
| 296 |
copyButton.classList.add('chuanhu-btn');
|
| 297 |
copyButton.classList.add('copy-bot-btn');
|
|
|
|
| 318 |
toggleButton.setAttribute('aria-label', 'Toggle');
|
| 319 |
toggleButton.innerHTML = toggleIcon;
|
| 320 |
toggleButton.addEventListener('click', () => {
|
| 321 |
+
console.log('toggle')
|
| 322 |
+
renderMarkdown = mdMessage.classList.contains('hideM');
|
| 323 |
+
renderMarkdown ? renderMarkdownText(botElement) : removeMarkdownText(botElement);
|
| 324 |
});
|
| 325 |
botElement.insertBefore(toggleButton, copyButton);
|
| 326 |
}
|
| 327 |
|
| 328 |
+
function renderMarkdownText(message) {
|
| 329 |
+
var mdDiv = message.querySelector('.md-message');
|
| 330 |
+
if (mdDiv) mdDiv.classList.remove('hideM');
|
| 331 |
+
var rawDiv = message.querySelector('.raw-message');
|
| 332 |
+
if (rawDiv) rawDiv.classList.add('hideM');
|
| 333 |
+
}
|
| 334 |
+
function removeMarkdownText(message) {
|
| 335 |
+
var rawDiv = message.querySelector('.raw-message');
|
| 336 |
+
if (rawDiv) rawDiv.classList.remove('hideM');
|
| 337 |
+
var mdDiv = message.querySelector('.md-message');
|
| 338 |
+
if (mdDiv) mdDiv.classList.add('hideM');
|
| 339 |
+
}
|
| 340 |
+
|
| 341 |
var rendertime = 0; // for debugging
|
| 342 |
var mathjaxUpdated = false;
|
| 343 |
|
modules/utils.py
CHANGED
|
@@ -183,7 +183,7 @@ def convert_mdtext(md_text):
|
|
| 183 |
non_code_parts = code_block_pattern.split(md_text)[::2]
|
| 184 |
|
| 185 |
result = []
|
| 186 |
-
raw = f'<
|
| 187 |
for non_code, code in zip(non_code_parts, code_blocks + [""]):
|
| 188 |
if non_code.strip():
|
| 189 |
non_code = normalize_markdown(non_code)
|
|
@@ -195,9 +195,10 @@ def convert_mdtext(md_text):
|
|
| 195 |
code = markdown_to_html_with_syntax_highlight(code)
|
| 196 |
result.append(code)
|
| 197 |
result = "".join(result)
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
| 201 |
|
| 202 |
|
| 203 |
def convert_asis(userinput):
|
|
|
|
| 183 |
non_code_parts = code_block_pattern.split(md_text)[::2]
|
| 184 |
|
| 185 |
result = []
|
| 186 |
+
raw = f'<div class="raw-message hideM">{html.escape(md_text)}</span>'
|
| 187 |
for non_code, code in zip(non_code_parts, code_blocks + [""]):
|
| 188 |
if non_code.strip():
|
| 189 |
non_code = normalize_markdown(non_code)
|
|
|
|
| 195 |
code = markdown_to_html_with_syntax_highlight(code)
|
| 196 |
result.append(code)
|
| 197 |
result = "".join(result)
|
| 198 |
+
output = f'<div class="md-message">{result}</div>'
|
| 199 |
+
output += raw
|
| 200 |
+
output += ALREADY_CONVERTED_MARK
|
| 201 |
+
return output
|
| 202 |
|
| 203 |
|
| 204 |
def convert_asis(userinput):
|