|
213 | 213 | /* Add space below summary */ |
214 | 214 | } |
215 | 215 |
|
| 216 | + #html-output summary small { |
| 217 | + font-size: 0.8em; |
| 218 | + color: #999; |
| 219 | + } |
| 220 | + |
216 | 221 | /* Ensure first element inside details (after summary) has correct top margin */ |
217 | 222 | #html-output details>*:nth-child(2) { |
218 | 223 | /* Selects first element after summary */ |
@@ -450,38 +455,63 @@ <h1>Markdown Preview <small>with MathJax</small></h1> |
450 | 455 | }; |
451 | 456 | } |
452 | 457 |
|
| 458 | + function text_eclipse(text, max_length = 64) { |
| 459 | + if (text.length <= max_length) { |
| 460 | + return text; |
| 461 | + } |
| 462 | + const half_length = Math.floor(max_length / 2); |
| 463 | + return (text.slice(0, half_length) + '...' + text.slice(-half_length)) |
| 464 | + // 需要去除 latex 语法,不然会被 math jax 再解析 |
| 465 | + .replace(/\\\]|\\\[|\\\(|\\\)|\$/ig, ""); |
| 466 | + } |
| 467 | + |
| 468 | + /** |
| 469 | + * 将所有第一层里面除了文本标准tag都转为 details |
| 470 | + * @param {HTMLElement} container - 要转换的容器元素 |
| 471 | + */ |
| 472 | + function convert_monologue_to_details(container) { |
| 473 | + // 将所有第一层里面除了文本标准tag都转为 details |
| 474 | + const text_tags = "p, h1, h2, h3, h4, h5, h6, pre, ul, ol, details, blockquote, code, img, hr, a".split(", "); |
| 475 | + const monologue_elems = [...container.children].filter(elem => !text_tags.includes(elem.localName)); |
| 476 | + monologue_elems.forEach(elem => { |
| 477 | + const details = document.createElement('details'); |
| 478 | + const summary = document.createElement('summary'); |
| 479 | + summary.innerHTML = `[${elem.localName}] <small>${text_eclipse(elem.textContent ?? "")}</small>`; // Set the summary text |
| 480 | + |
| 481 | + // Move all child nodes from <think> to <details> |
| 482 | + while (elem.firstChild && elem.firstChild.tagName !== 'SUMMARY') { |
| 483 | + details.appendChild(elem.firstChild); |
| 484 | + } |
| 485 | + |
| 486 | + // Add the summary as the first child of <details> |
| 487 | + details.insertBefore(summary, details.firstChild); |
| 488 | + |
| 489 | + // Replace the original <think> element with the new <details> element |
| 490 | + elem.parentNode.replaceChild(details, elem); |
| 491 | + }); |
| 492 | + } |
| 493 | + |
453 | 494 | // --- Update Preview Function --- |
454 | 495 | function updatePreview() { |
455 | 496 | try { |
456 | 497 | // 1. Render Markdown to HTML (keeping <think> tags) |
457 | 498 | // Note: Backslash replacement is usually handled by markdown-it-mathjax correctly |
458 | 499 | // if using $, $$, \(, \), \[, \]. Avoid double-escaping unless specific issues arise. |
459 | | - const markdownText = markdownInput.value.replace(/\\/ig, "\\\\") |
| 500 | + const markdownText = markdownInput.value |
| 501 | + .replace(/\\/ig, "\\\\") |
| 502 | + .replace(/^\s*<[^>]+?>\s*$/igm, s => `\n${s}\n`) |
| 503 | + // 将代码模块渲染为 code block |
| 504 | + .replace(/<code>([\s\S]+?)<\/code>/ig, (_, s1) => `\`\`\`python\n${s1.trim()}\n\`\`\``) |
| 505 | + .replace(/<execute>([\s\S]+?)<\/execute>/ig, (_, s1) => `\`\`\`python\n${s1.trim()}\n\`\`\``); |
| 506 | + console.log(markdownText) |
460 | 507 | // const markdownText = markdownInput.value; |
461 | 508 | const rawHtml = md.render(markdownText); |
462 | 509 |
|
463 | 510 | // 2. Post-process HTML to convert <think> to <details> |
464 | 511 | const tempDiv = document.createElement('div'); |
465 | 512 | tempDiv.innerHTML = rawHtml; // Parse the rendered HTML |
466 | 513 |
|
467 | | - const thinkElements = tempDiv.querySelectorAll('think'); // Find all <think> elements |
468 | | - |
469 | | - thinkElements.forEach(thinkElement => { |
470 | | - const details = document.createElement('details'); |
471 | | - const summary = document.createElement('summary'); |
472 | | - summary.textContent = 'Thinking Process'; // Set the summary text |
473 | | - |
474 | | - // Move all child nodes from <think> to <details> |
475 | | - while (thinkElement.firstChild) { |
476 | | - details.appendChild(thinkElement.firstChild); |
477 | | - } |
478 | | - |
479 | | - // Add the summary as the first child of <details> |
480 | | - details.insertBefore(summary, details.firstChild); |
481 | | - |
482 | | - // Replace the original <think> element with the new <details> element |
483 | | - thinkElement.parentNode.replaceChild(details, thinkElement); |
484 | | - }); |
| 514 | + convert_monologue_to_details(tempDiv); |
485 | 515 |
|
486 | 516 | // 3. Put the modified HTML into the output container |
487 | 517 | htmlOutput.innerHTML = tempDiv.innerHTML; |
|
0 commit comments