简化推送格式:使用普通文本和直接URL链接

This commit is contained in:
bojunc 2026-02-25 12:19:31 +08:00
parent 797deb62f4
commit cbd1c0bfcf

View File

@ -0,0 +1,139 @@
#!/usr/bin/env node
/**
* 格式化AI新闻简报 - 飞书友好版本
* 生成易读链接可点击的消息格式
*/
const fs = require('fs');
const path = require('path');
const DAILY_DIR = path.join(__dirname, '../../daily');
function getYesterdayDate() {
const d = new Date();
d.setDate(d.getDate() - 1);
return d.toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone: 'Asia/Shanghai'
}).replace(/\//g, '-');
}
function parseMarkdown(markdown) {
const lines = markdown.split('\n');
const items = [];
let currentDate = '';
let currentTime = '';
let totalItems = 0;
// 解析元数据
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('> 采集时间:')) {
currentTime = line.replace('> 采集时间:', '').trim();
}
if (line.startsWith('> 总条目:')) {
totalItems = parseInt(line.replace('> 总条目:', '').trim());
}
}
// 解析新闻条目
const itemRegex = /^(\d+)\.\s+\[([^\]]+)\]\(([^)]+)\)\s+-\s+\*\*([^*]+)\*\*/;
let currentItem = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const match = line.match(itemRegex);
if (match) {
if (currentItem) {
items.push(currentItem);
}
// 清理URL移除登录页面
let url = match[3];
let title = match[2];
// 如果是登录页面,提取真实路径
if (url.includes('login?return_to=')) {
const realPath = decodeURIComponent(url.split('return_to=')[1]);
url = `https://github.com${realPath}`;
title = realPath.replace('/', ''); // 使用真实仓库名
}
currentItem = {
number: match[1],
title: title,
url: url,
source: match[4],
description: ''
};
} else if (currentItem && line.trim().startsWith('>') && !line.includes('采集时间') && !line.includes('总条目')) {
// 移除所有 > 符号和前导空格
currentItem.description = line.replace(/^\s*>\s*/, '').trim();
}
}
if (currentItem) {
items.push(currentItem);
}
return { currentTime, totalItems, items };
}
function formatForFeishu(data) {
const { currentTime, totalItems, items } = data;
let message = `📰 AI Daily Brief - ${getYesterdayDate()}\n\n`;
message += `⏰ 采集时间: ${currentTime}\n`;
message += `📊 总条目: ${totalItems}\n\n`;
message += `━━━━━━━━━━━━━━━━━━━━\n\n`;
message += `🔥 今日热点\n\n`;
items.forEach((item, index) => {
const num = ['1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣', '🔟'][index] || `${index + 1}.`;
message += `${num} ${item.title}\n`;
message += `${item.source}\n`;
if (item.description) {
message += `${item.description}\n`;
}
message += `🔗 ${item.url}\n\n`;
});
message += `━━━━━━━━━━━━━━━━━━━━\n`;
message += `💡 点击链接跳转到GitHub查看详情\n`;
return message;
}
async function main() {
const yesterday = getYesterdayDate();
const reportPath = path.join(DAILY_DIR, `${yesterday}.md`);
if (!fs.existsSync(reportPath)) {
console.log(`❌ 未找到昨日简报: ${reportPath}`);
return { success: false, error: 'Report not found' };
}
const markdown = fs.readFileSync(reportPath, 'utf8');
const data = parseMarkdown(markdown);
const formattedMessage = formatForFeishu(data);
console.log('格式化后的消息:');
console.log(formattedMessage);
return {
success: true,
date: yesterday,
message: formattedMessage
};
}
if (require.main === module) {
main().catch(console.error);
}
module.exports = { main, formatForFeishu, parseMarkdown };