Skip to content

Commit 745c3c8

Browse files
committed
feat: support code act
1 parent 064984b commit 745c3c8

1 file changed

Lines changed: 49 additions & 19 deletions

File tree

pages/mathjax-md-preview/index.html

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@
213213
/* Add space below summary */
214214
}
215215

216+
#html-output summary small {
217+
font-size: 0.8em;
218+
color: #999;
219+
}
220+
216221
/* Ensure first element inside details (after summary) has correct top margin */
217222
#html-output details>*:nth-child(2) {
218223
/* Selects first element after summary */
@@ -450,38 +455,63 @@ <h1>Markdown Preview <small>with MathJax</small></h1>
450455
};
451456
}
452457

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+
453494
// --- Update Preview Function ---
454495
function updatePreview() {
455496
try {
456497
// 1. Render Markdown to HTML (keeping <think> tags)
457498
// Note: Backslash replacement is usually handled by markdown-it-mathjax correctly
458499
// 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)
460507
// const markdownText = markdownInput.value;
461508
const rawHtml = md.render(markdownText);
462509

463510
// 2. Post-process HTML to convert <think> to <details>
464511
const tempDiv = document.createElement('div');
465512
tempDiv.innerHTML = rawHtml; // Parse the rendered HTML
466513

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);
485515

486516
// 3. Put the modified HTML into the output container
487517
htmlOutput.innerHTML = tempDiv.innerHTML;

0 commit comments

Comments
 (0)