update code

This commit is contained in:
lychang
2025-11-20 02:27:22 +08:00
parent 8ff1b80be6
commit bf7ec6737d
10 changed files with 229 additions and 243 deletions

View File

@@ -1,6 +1,30 @@
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;
}
};
// 设置默认日期为今天
document.getElementById('date').value = new Date().toISOString().split('T')[0];
function showLoading() {
document.getElementById('loading').style.display = 'block';
document.getElementById('report-container').style.display = 'none';
@@ -38,14 +62,14 @@ function generateReport() {
if (data.success) {
// 保存原始Markdown内容到data属性
document.getElementById('report-content').setAttribute('data-markdown', data.report);
// 将Markdown转换为HTML并显示
const htmlContent = markdownToHtml(data.report);
// 使用专业的Markdown解析器将Markdown转换为HTML并显示
const htmlContent = parseMarkdownWithTables(data.report);
document.getElementById('report-content').innerHTML = htmlContent;
// 重置为预览模式
togglePreview('preview');
document.getElementById('report-container').style.display = 'block';
// 显示统计数据
@@ -95,8 +119,8 @@ function exportReport() {
showError('请先生成日报');
return;
}
const blob = new Blob([reportContent], { type: 'text/markdown' });
const blob = new Blob([reportContent], {type: 'text/markdown'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
@@ -110,4 +134,15 @@ function exportReport() {
// 页面加载时自动生成今日日报
window.addEventListener('load', function () {
generateTodayReport();
});
});
// 使用专业的Markdown解析器解析Markdown文本,支持表格
function parseMarkdownWithTables(markdown) {
if (typeof MarkdownParser !== 'undefined' && typeof MergeTableParser !== 'undefined') {
const parser = new MarkdownParser();
return parser.parseFull(markdown);
} else {
// 回退到简单的markdown渲染
return markdownToHtml(markdown);
}
}

View File

@@ -1,132 +0,0 @@
// 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;
}
};

View File

@@ -1,3 +1,27 @@
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;
}
};
// 设置默认周次为当前周
function getCurrentWeek() {
const now = new Date();
@@ -8,7 +32,7 @@ function getCurrentWeek() {
}
// 页面加载时设置默认周次
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('week').value = getCurrentWeek();
});
@@ -33,42 +57,41 @@ function generateCurrentWeekReport() {
}
function generateReport() {
const week = document.getElementById('week').value;
if (!week) {
showError('请选择周次');
return;
}
// 转换格式:YYYY-WWW 到 YYYY-WW
const weekFormatted = week.replace('W', '');
showLoading();
fetch(`/api/reports/weekly?week=${weekFormatted}`)
.then(response => response.json())
.then(data => {
hideLoading();
if (data.success) {
// 保存原始Markdown内容到data属性
document.getElementById('report-content').setAttribute('data-markdown', data.report);
// 将Markdown转换为HTML并显示
const htmlContent = markdownToHtml(data.report);
// 使用专业的Markdown解析器将Markdown转换为HTML并显示
const htmlContent = parseMarkdownWithTables(data.report);
document.getElementById('report-content').innerHTML = htmlContent;
// 重置为预览模式
togglePreview('preview');
document.getElementById('report-container').style.display = 'block';
// 显示统计数据
if (data.statistics) {
displayStatistics(data.statistics);
document.getElementById('statistics').style.display = 'block';
// 显示分类图表
if (data.statistics.category_breakdown) {
displayCategoryChart(data.statistics.category_breakdown);
@@ -88,17 +111,17 @@ function generateReport() {
function displayStatistics(stats) {
const statGrid = document.getElementById('stat-grid');
statGrid.innerHTML = '';
const statistics = [
{ label: '本周消息量', value: stats.week_message_count, unit: '条' },
{ label: '上周消息量', value: stats.last_week_message_count, unit: '条' },
{ label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%' },
{ label: '本周活跃用户', value: stats.week_active_users, unit: '人' },
{ label: '群组数量', value: stats.group_count, unit: '个' },
{ label: '成员总数', value: stats.member_count, unit: '人' },
{ label: '问答记录数', value: stats.qa_count, unit: '条' }
{label: '本周消息量', value: stats.week_message_count, unit: '条'},
{label: '上周消息量', value: stats.last_week_message_count, unit: '条'},
{label: '消息变化率', value: stats.message_change_rate.toFixed(1), unit: '%'},
{label: '本周活跃用户', value: stats.week_active_users, unit: '人'},
{label: '群组数量', value: stats.group_count, unit: '个'},
{label: '成员总数', value: stats.member_count, unit: '人'},
{label: '问答记录数', value: stats.qa_count, unit: '条'}
];
statistics.forEach(stat => {
const statItem = document.createElement('div');
statItem.className = 'stat-item';
@@ -113,7 +136,7 @@ function displayStatistics(stats) {
function displayCategoryChart(categories) {
const categoryItems = document.getElementById('category-items');
categoryItems.innerHTML = '';
Object.entries(categories).forEach(([category, percentage]) => {
const categoryItem = document.createElement('div');
categoryItem.className = 'category-item';
@@ -132,8 +155,8 @@ function exportReport() {
showError('请先生成周报');
return;
}
const blob = new Blob([reportContent], { type: 'text/markdown' });
const blob = new Blob([reportContent], {type: 'text/markdown'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
@@ -145,13 +168,24 @@ function exportReport() {
}
// 页面加载时自动生成本周周报
window.addEventListener('load', function() {
window.addEventListener('load', function () {
generateCurrentWeekReport();
});
// 支持回车键生成报告
document.getElementById('week').addEventListener('keypress', function(e) {
document.getElementById('week').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
generateReport();
}
});
});
// 使用专业的Markdown解析器解析Markdown文本,支持表格
function parseMarkdownWithTables(markdown) {
if (typeof MarkdownParser !== 'undefined' && typeof MergeTableParser !== 'undefined') {
const parser = new MarkdownParser();
return parser.parseFull(markdown);
} else {
// 回退到简单的markdown渲染
return markdownToHtml(markdown);
}
}