Files
SmartChat/static/js/markdown-render.js
2025-11-14 01:21:48 +08:00

132 lines
5.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Markdown转HTML的简单实现
function markdownToHtml(markdown) {
// 替换标题
let html = markdown
.replace(/^# (.*$)/gm, '<h1>$1</h1>')
.replace(/^## (.*$)/gm, '<h2>$1</h2>')
.replace(/^### (.*$)/gm, '<h3>$1</h3>')
.replace(/^#### (.*$)/gm, '<h4>$1</h4>')
.replace(/^##### (.*$)/gm, '<h5>$1</h5>')
.replace(/^###### (.*$)/gm, '<h6>$1</h6>');
// 替换粗体
html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
// 替换斜体
html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
// 替换代码块
html = html.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
// 替换行内代码
html = html.replace(/`(.*?)`/g, '<code>$1</code>');
// 替换无序列表
html = html.replace(/^\s*[-*] (.*$)/gm, '<li>$1</li>');
html = html.replace(/(<li>.*<\/li>)/s, '<ul>$1</ul>');
// 替换有序列表
html = html.replace(/^\s*\d+\. (.*$)/gm, '<li>$1</li>');
html = html.replace(/(<li>.*<\/li>)/s, '<ol>$1</ol>');
// 替换引用块
html = html.replace(/^> (.*$)/gm, '<blockquote>$1</blockquote>');
// 替换链接
html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2" target="_blank">$1</a>');
// 替换图片
html = html.replace(/!\[(.*?)\]\((.*?)\)/g, '<img src="$2" alt="$1" style="max-width: 100%;">');
// 替换表格(增强实现)
html = html.replace(/\|(.+)\|\n\|([-|: ]+)\|\n((?:\|.+\|\n)*)/g, function(match, headerRow, separatorRow, bodyRows) {
// 解析表头
const headers = headerRow.split('|').filter(cell => cell.trim());
// 解析分隔行,确定对齐方式
const separators = separatorRow.split('|').filter(cell => cell.trim());
const alignments = separators.map(sep => {
if (sep.startsWith(':') && sep.endsWith(':')) return 'center';
if (sep.endsWith(':')) return 'right';
return 'left';
});
// 构建表头HTML
let tableHtml = '<table style="border-collapse: collapse; width: 100%; margin: 10px 0; border: 1px solid #ddd;">';
tableHtml += '<thead><tr style="background-color: #f5f5f5;">';
headers.forEach((header, index) => {
const alignment = alignments[index] || 'left';
tableHtml += `<th style="border: 1px solid #ddd; padding: 8px; text-align: ${alignment}; font-weight: bold;">${header.trim()}</th>`;
});
tableHtml += '</tr></thead>';
// 解析表格主体
const bodyRowsArray = bodyRows.trim().split('\n');
if (bodyRowsArray.length > 0) {
tableHtml += '<tbody>';
bodyRowsArray.forEach(row => {
const cells = row.split('|').filter(cell => cell.trim());
if (cells.length > 0) {
tableHtml += '<tr>';
cells.forEach((cell, index) => {
const alignment = alignments[index] || 'left';
tableHtml += `<td style="border: 1px solid #ddd; padding: 8px; text-align: ${alignment};">${cell.trim()}</td>`;
});
tableHtml += '</tr>';
}
});
tableHtml += '</tbody>';
}
tableHtml += '</table>';
return tableHtml;
});
// 处理简单的单行表格(向后兼容)
html = html.replace(/^\|(.+)\|$/gm, function(match) {
const cells = match.split('|').filter(cell => cell.trim());
if (cells.length > 0) {
return '<table style="border-collapse: collapse; width: 100%; margin: 10px 0; border: 1px solid #ddd;"><tr>' +
cells.map(cell => `<td style="border: 1px solid #ddd; padding: 8px;">${cell.trim()}</td>`).join('') +
'</tr></table>';
}
return match;
});
// 替换水平分隔线(---、***、___)
html = html.replace(/^\s*[-*_]{3,}\s*$/gm, '<hr>');
// 将连续的空行转换为段落
html = html.replace(/\n\n+/g, '</p><p>');
html = '<p>' + html.replace(/\n/g, '<br>') + '</p>';
// 清理多余的段落标签
html = html.replace(/<p><\/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;
}
};