MindMap/frontend/test-table.html

121 lines
3.7 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>表格渲染测试</title>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.test-container {
border: 1px solid #ccc;
padding: 20px;
margin: 10px 0;
}
.markdown-table {
border-collapse: collapse;
width: 100%;
margin: 4px 0;
font-size: 11px;
}
.markdown-table th,
.markdown-table td {
border: 1px solid #ddd;
padding: 4px 6px;
text-align: left;
}
.markdown-table th {
background-color: #f8f9fa;
font-weight: 600;
color: #333;
}
.markdown-table tr:nth-child(even) {
background-color: #f8f9fa;
}
</style>
</head>
<body>
<h1>表格渲染测试</h1>
<div class="test-container">
<h2>测试1: 简单表格</h2>
<div id="test1"></div>
</div>
<div class="test-container">
<h2>测试2: 复杂表格</h2>
<div id="test2"></div>
</div>
<div class="test-container">
<h2>测试3: 你的测试表格</h2>
<div id="test3"></div>
</div>
<script>
// 配置marked
marked.setOptions({
breaks: true,
gfm: true,
tables: true,
sanitize: false,
});
// 测试1: 简单表格
const test1Markdown = `| 产品 | 价格 |
|------|------|
| 苹果 | 4元 |
| 香蕉 | 2元 |`;
document.getElementById('test1').innerHTML = marked.parse(test1Markdown);
// 测试2: 复杂表格
const test2Markdown = `| 评估维度 | 权重 | 评分标准 | 计算公式 |
|---------|------|---------|---------|
| 完整性 | 0.3 | 缺失值比例 < 5% | $\\text{完整性} = 1 - \\frac{\\text{缺失值数量}}{\\text{总数据量}}$ |
| 准确性 | 0.3 | 误差率 < 3% | $\\text{准确性} = 1 - \\frac{\\text{错误记录数}}{\\text{总记录数}}$ |`;
document.getElementById('test2').innerHTML = marked.parse(test2Markdown);
// 测试3: 你的测试表格
const test3Markdown = `| 产品 | 价格 | 库存 |
|------|------|------|
| 苹果 | 4元 | 100个 |
| 香蕉 | 2元 | 50个 |`;
document.getElementById('test3').innerHTML = marked.parse(test3Markdown);
// 测试表格检测函数
function hasTable(content) {
if (!content || typeof content !== 'string') return false;
const lines = content.split('\n');
let hasTableRow = false;
let hasSeparator = false;
for (const line of lines) {
const trimmedLine = line.trim();
// 检查表格行(包含|字符)
if (trimmedLine.includes('|') && trimmedLine.split('|').length >= 3) {
hasTableRow = true;
}
// 检查分隔符行(包含-和|
if (trimmedLine.includes('|') && trimmedLine.includes('-') && /^[\s\|\-\:]+$/.test(trimmedLine)) {
hasSeparator = true;
}
}
return hasTableRow && hasSeparator;
}
// 测试表格检测
console.log('测试1表格检测:', hasTable(test1Markdown));
console.log('测试2表格检测:', hasTable(test2Markdown));
console.log('测试3表格检测:', hasTable(test3Markdown));
</script>
</body>
</html>