-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathconvert.js
More file actions
117 lines (103 loc) · 3.72 KB
/
Copy pathconvert.js
File metadata and controls
117 lines (103 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const cheerio = require('cheerio');
const fs = require('fs');
const htmlContent = fs.readFileSync('/Users/canghe/.claude/projects/-Users-canghe/1a7db960-136f-49cc-a1fc-983884c098fd/tool-results/b97eb13.txt', 'utf8');
const data = JSON.parse(htmlContent);
const $ = cheerio.load(data.msg_content, { decodeEntities: false });
let markdown = `---
title: "${data.msg_title}"
author: "${data.msg_author || data.account_name}"
date: "${data.msg_publish_time_str}"
source: "${data.account_name}"
original_url: "${data.msg_link}"
---
# ${data.msg_title}
**作者**: ${data.msg_author || data.account_name}
**发布时间**: ${data.msg_publish_time_str}
**公众号**: ${data.account_name}
**原文链接**: ${data.msg_link}
---
`;
function processElement(elem, isRoot = false) {
const $elem = $(elem);
const tagName = elem.tagName?.toLowerCase();
if (!tagName) return;
if (tagName === 'h2') {
markdown += '\n## ' + $elem.text().trim() + '\n\n';
} else if (tagName === 'h3') {
markdown += '\n### ' + $elem.text().trim() + '\n\n';
} else if (tagName === 'p') {
// Check for nested elements
let text = '';
$elem.contents().each((i, child) => {
if (child.type === 'text') {
text += child.data;
} else if (child.type === 'tag') {
const $child = $(child);
if (child.tagName === 'strong' || child.tagName === 'b' || child.tagName === 'span') {
const childText = $child.text().trim();
if (childText.startsWith('「') && childText.endsWith('」')) {
text += '**' + childText + '**';
} else {
text += childText;
}
} else if (child.tagName === 'br') {
text += '\n';
} else {
text += $child.text();
}
}
});
text = text.trim();
if (text) markdown += text + '\n\n';
} else if (tagName === 'blockquote') {
const text = $elem.text().trim();
if (text) markdown += '> ' + text + '\n\n';
} else if (tagName === 'ol') {
$elem.children('li').each((i, li) => {
const text = $(li).text().trim();
if (text) markdown += (i + 1) + '. ' + text + '\n';
});
markdown += '\n';
} else if (tagName === 'ul') {
$elem.children('li').each((i, li) => {
const text = $(li).text().trim();
if (text) markdown += '- ' + text + '\n';
});
markdown += '\n';
} else if (tagName === 'img') {
const src = $elem.attr('data-src') || $elem.attr('src');
if (src) markdown += '\n\n\n';
} else if (tagName === 'code') {
const text = $elem.text().trim();
if (text) markdown += '`' + text + '`';
} else if (tagName === 'pre' || (tagName === 'section' && $elem.hasClass('code-snippet__fix'))) {
const codeText = $elem.find('code').text() || $elem.text();
if (codeText.trim()) {
markdown += '\n```\n' + codeText.trim() + '\n```\n\n';
}
} else if (tagName === 'section' || tagName === 'div' || tagName === 'center') {
// 递归处理子元素
$elem.children().each((i, child) => {
processElement(child);
});
}
}
// 处理主要内容
const mainSection = $('section[data-plugin="note-to-mp"]');
if (mainSection.length) {
mainSection.children().each((i, child) => {
processElement(child, true);
});
} else {
// 如果没有找到主section,处理body
$('body').children().each((i, child) => {
processElement(child, true);
});
}
// 清理多余的空行
markdown = markdown.replace(/\n{3,}/g, '\n\n');
// 保存文件
const outputPath = '/Users/canghe/Downloads/wechat/爆肝2天用GLM5开发OpenClaw接入微信bot.md';
fs.writeFileSync(outputPath, markdown, 'utf8');
console.log('文件已保存到:', outputPath);
console.log('文件大小:', (markdown.length / 1024).toFixed(2), 'KB');