// Markdown转HTML的简单实现 function markdownToHtml(markdown) { // 替换标题 let html = markdown .replace(/^# (.*$)/gm, '

$1

') .replace(/^## (.*$)/gm, '

$1

') .replace(/^### (.*$)/gm, '

$1

') .replace(/^#### (.*$)/gm, '

$1

') .replace(/^##### (.*$)/gm, '
$1
') .replace(/^###### (.*$)/gm, '
$1
'); // 替换粗体 html = html.replace(/\*\*(.*?)\*\*/g, '$1'); // 替换斜体 html = html.replace(/\*(.*?)\*/g, '$1'); // 替换代码块 html = html.replace(/```([\s\S]*?)```/g, '
$1
'); // 替换行内代码 html = html.replace(/`(.*?)`/g, '$1'); // 替换无序列表 html = html.replace(/^\s*[-*] (.*$)/gm, '
  • $1
  • '); html = html.replace(/(
  • .*<\/li>)/s, ''); // 替换有序列表 html = html.replace(/^\s*\d+\. (.*$)/gm, '
  • $1
  • '); html = html.replace(/(
  • .*<\/li>)/s, '
      $1
    '); // 替换引用块 html = html.replace(/^> (.*$)/gm, '
    $1
    '); // 替换链接 html = html.replace(/\[(.*?)\]\((.*?)\)/g, '$1'); // 替换图片 html = html.replace(/!\[(.*?)\]\((.*?)\)/g, '$1'); // 替换表格(简单实现) html = html.replace(/\|(.+)\|/g, function(match) { const cells = match.split('|').filter(cell => cell.trim()); if (cells.length > 0) { return '' + cells.join('') + ''; } return match; }); // 替换水平分隔线(---、***、___) html = html.replace(/^\s*[-*_]{3,}\s*$/gm, '
    '); // 将连续的空行转换为段落 html = html.replace(/\n\n+/g, '

    '); html = '

    ' + html.replace(/\n/g, '
    ') + '

    '; // 清理多余的段落标签 html = html.replace(/

    <\/p>/g, ''); return html; } // 切换预览模式函数 togglePreview = function(mode) { const previewBtn = document.getElementById('preview-toggle'); const sourceBtn = document.getElementById('source-toggle'); const reportContent = document.getElementById('report-content'); const markdownSource = document.getElementById('markdown-source'); const sourceContent = document.getElementById('source-content'); if (mode === 'preview') { previewBtn.classList.add('active'); sourceBtn.classList.remove('active'); reportContent.style.display = 'block'; markdownSource.style.display = 'none'; } else { previewBtn.classList.remove('active'); sourceBtn.classList.add('active'); reportContent.style.display = 'none'; markdownSource.style.display = 'block'; // 显示原始Markdown源代码 const markdown = reportContent.getAttribute('data-markdown'); sourceContent.textContent = markdown; } };