import { writeFile } from 'fs/promises'; import CommonContent, { GetContentListParams, type GetContentDetailItem, type GetContentListItem } from '../../api/CommonContent'; import ProjectsContent from '../../api/inheritor/ProjectsContent'; import InheritorContent from '../../api/inheritor/InheritorContent'; import SeminarContent from '../../api/inheritor/SeminarContent'; import UnitContent from '../../api/inheritor/UnitContent'; import UnmoveableContent from '../../api/inheritor/UnmoveableContent'; import path from 'path'; import fs from 'fs'; import { argv, cwd } from 'process'; const data = [] as Array; // HTML转Markdown的简单实现 function htmlToMarkdown(html: string): string { if (!html) return ''; // 处理标题 html = html.replace(/]*>(.*?)<\/h1>/gi, '# $1\n\n'); html = html.replace(/]*>(.*?)<\/h2>/gi, '## $1\n\n'); html = html.replace(/]*>(.*?)<\/h3>/gi, '### $1\n\n'); // 处理段落 html = html.replace(/]*>(.*?)<\/p>/gi, '$1\n\n'); // 处理加粗 html = html.replace(/]*>(.*?)<\/strong>/gi, '**$1**'); html = html.replace(/]*>(.*?)<\/b>/gi, '**$1**'); // 处理斜体 html = html.replace(/]*>(.*?)<\/em>/gi, '*$1*'); html = html.replace(/]*>(.*?)<\/i>/gi, '*$1*'); // 处理列表 html = html.replace(/]*>([\s\S]*?)<\/ul>/gi, (match, content) => { return content.replace(/]*>(.*?)<\/li>/gi, '- $1\n') + '\n'; }); html = html.replace(/]*>([\s\S]*?)<\/ol>/gi, (match, content, index) => { let count = 1; return content.replace(/]*>(.*?)<\/li>/gi, () => { return `${count++}. $1\n`; }) + '\n'; }); // 处理图片 html = html.replace(/]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/gi, '![$2]($1)'); // 处理链接 html = html.replace(/]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)'); // 处理换行 html = html.replace(/]*>/gi, '\n'); // 去除所有HTML标签 html = html.replace(/<[^>]*>/g, ''); // 处理多余的换行 html = html.replace(/\n\s*\n/g, '\n\n'); return html.trim(); } // 生成Markdown文本 async function generateMarkdownIch(subDir: string, type: string) { for (const item of data) { let md = ''; // 基本信息 md += `# ${item.title}\n\n`; if (item.desc) md += `${item.desc}\n\n`; md += `## 基本信息\n\n`; md += `类型:${type}\n\n`; function addRow(key: string, value: any) { if (value) md += `- ${key}: ${value}\n`; } addRow('级别', item.levelText); addRow('类别', item.ichTypeText); addRow('地区', item.regionText); addRow('批次', item.batchText); addRow('保护单位', item.unit); addRow('地址', item.address); addRow('字号名称', item.fontName); addRow('认定类型', item.brandType); addRow('其他级别保护单位', item.detail?.otherLevel && item.detail.otherLevel.length > 0 ? `${item.detail.otherLevel.length}个` : ''); md += `\n## 数据库索引ID\n\n`; md += `- 类型: intangible\n`; md += `- ID: ${item.id || '无'}\n\n`; // 详细信息 if (item.detail) { const detail = item.detail as GetContentDetailItem; // 简介 if (detail.intro) { md += `## 简介\n\n`; md += htmlToMarkdown(detail.intro) + '\n\n'; } // 内容 if (detail.content) { md += `## 内容\n\n`; md += htmlToMarkdown(detail.content) + '\n\n'; } // 传承谱系 if (detail.pedigree) { md += `## 传承谱系\n\n`; md += htmlToMarkdown(detail.pedigree as string) + '\n\n'; } // 视频 if (detail.video) { md += `## 视频\n\n`; md += `[视频](${detail.video})\n\n`; } if (detail.publishVideo) md += `[介绍视频](${detail.publishVideo})\n\n`; // 传承人 if (detail.inheritorsList && detail.inheritorsList.length > 0) { md += `## 相关传承人\n\n`; if (detail.inheritor) { md += htmlToMarkdown(detail.inheritor) + '\n\n'; } detail.inheritorsList.forEach(inheritor => { md += `### ${inheritor.title}\n\n`; md += `级别:${inheritor.levelLext || '无'}\n\n`; md += `#### 数据库索引ID\n\n`; md += `- 类型: inheritor\n`; md += `- ID: ${inheritor.id || '无'}\n\n`; }); } // 传习所 if (detail.ichSitesList && detail.ichSitesList.length > 0) { md += `## 相关传习所\n\n`; detail.ichSitesList.forEach(site => { md += `### ${site.title}\n\n`; md += `级别:${site.levelLext || '无'}\n\n`; md += `地址:${site.address || '无'}\n\n`; md += `#### 数据库索引ID\n\n`; md += `- 类型: seminar\n`; md += `- ID: ${site.id || '无'}\n\n`; }); } // 同级别项目 if (detail.otherLevel && detail.otherLevel.length > 0) { md += `## 其他级别非遗项目\n\n`; detail.otherLevel.forEach(project => { md += `### ${project.title}\n\n`; md += `级别:${project.levelLext || '无'}\n\n`; md += `保护单位:${project.unit || '无'}\n\n`; md += `#### 数据库索引ID\n\n`; md += `- 类型: intangible\n`; md += `- ID: ${project.id || '无'}\n\n`; }); } } await writeFile(path.join(subDir, `${item.id}.md`), md); } } async function generateMarkdownInheritor(subDir: string) { for (const item of data) { let md = ''; // 基本信息 md += `# ${item.title}\n\n`; if (item.desc) md += `${item.desc}\n\n`; md += `## 基本信息\n\n`; md += `类型:非遗传承人\n\n`; function addRow(key: string, value: any) { if (value) md += `- ${key}: ${value}\n`; } addRow('民族', item.detail?.nation); addRow('性别', item.detail?.gender == '1'? '男' : '女'); addRow('出生日期', item.detail?.dateBirth); addRow('出生地区', item.detail?.birthplace); addRow('单位', item.detail?.unit); addRow('传承项目', item.detail?.associationMeList[0]?.title); addRow('传承人级别', item.detail?.batchText); addRow('公布批次', item.detail?.batchText); md += `\n## 数据库索引ID\n\n`; md += `- 类型: inheritor\n`; md += `- ID: ${item.id || '无'}\n\n`; // 详细信息 if (item.detail) { const detail = item.detail as GetContentDetailItem; // 简介 if (detail.intro) { md += `## 简介\n\n`; md += htmlToMarkdown(detail.intro) + '\n\n'; } if (detail.content) { md += `## 详情\n\n`; md += htmlToMarkdown(detail.content) + '\n\n'; } // 奖项 if (detail.prize) { md += `## 奖项\n\n`; md += htmlToMarkdown(detail.prize as string) + '\n\n'; } // 相关项目 if (detail.associationMeList && detail.associationMeList.length > 0) { md += `## 相关项目\n\n`; detail.associationMeList.forEach(inheritor => { md += `### ${inheritor.title}\n\n`; md += `#### 数据库索引ID\n\n`; md += `- 类型: intangible\n`; md += `- ID: ${inheritor.id || '无'}\n\n`; }); } // 传习所 if (detail.ichSitesList && detail.ichSitesList.length > 0) { md += `## 相关传习所\n\n`; detail.ichSitesList.forEach(site => { md += `### ${site.title}\n\n`; md += `级别:${site.levelLext || '无'}\n\n`; md += `地址:${site.address || '无'}\n\n`; md += `##### 数据库索引ID\n\n`; md += `- 类型: seminar\n`; md += `- ID: ${site.id || '无'}\n\n`; }); } } await writeFile(path.join(subDir, `${item.id}.md`), md); } } async function generateMarkdownArtifact(subDir: string) { for (const item of data) { let md = ''; // 基本信息 md += `# ${item.title}\n\n`; if (item.desc) md += `${item.desc}\n\n`; md += `## 基本信息\n\n`; md += `类型:非遗传承人\n\n`; function addRow(key: string, value: any) { if (value) md += `- ${key}: ${value}\n`; } addRow('开放时间', item.detail?.openStatusText); addRow('年代', item.age); addRow('级别', item.levelText); addRow('所属区域', item.regionText); addRow('文物类型', item.crTypeText); addRow('单位', item.detail?.unit); md += `\n## 数据库索引ID\n\n`; md += `- 类型: artifact\n`; md += `- ID: ${item.id || '无'}\n\n`; if (item.video) { md += `## 视频\n\n`; md += `![${item.title}视频](${item.video})\n\n`; } // 详细信息 if (item.detail) { const detail = item.detail as GetContentDetailItem; // 简介 if (detail.intro) { md += `## 简介\n\n`; md += htmlToMarkdown(detail.intro) + '\n\n'; } if (detail.content) { md += `## 详情\n\n`; md += htmlToMarkdown(detail.content) + '\n\n'; } // 奖项 if (detail.protectedArea) { md += `## 保护范围\n\n`; md += htmlToMarkdown(detail.protectedArea as string) + '\n\n'; } if (detail.environment) { md += `## 建筑环境\n\n`; md += htmlToMarkdown(detail.environment as string) + '\n\n'; } if (detail.价值评估) { md += `## 价值评估\n\n`; md += htmlToMarkdown(detail.价值评估 as string) + '\n\n'; } } await writeFile(path.join(subDir, `${item.id}.md`), md); } } async function main() { const type = argv[2]; function makeDir(nanme: string) { const dir = path.join(cwd(), `dist/${nanme}`); if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); return dir; } switch (type) { case 'ich': { const dir = makeDir('ich'); (await ProjectsContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => { data.push(item); }); for (const item of data) item.detail = (await ProjectsContent.getContentDetail(item.id)) as GetContentDetailItem; generateMarkdownIch(dir, '非遗项目'); break; } case 'seminar': { const dir = makeDir('seminar'); (await SeminarContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => { data.push(item); }); for (const item of data) item.detail = (await SeminarContent.getContentDetail(item.id)) as GetContentDetailItem; generateMarkdownIch(dir, '非遗传习所'); break; } case 'old': { const dir = makeDir('old'); (await CommonContent.getContentList(new GetContentListParams() .setModelId(17) .setMainBodyColumnId(312) , 1, 1000)).list.forEach(item => { data.push(item); }); for (const item of data) item.detail = (await CommonContent.getContentDetail(item.id)) as GetContentDetailItem; generateMarkdownIch(dir, '老字号'); break; } case 'unit': { const dir = makeDir('unit'); (await UnitContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => { data.push(item); }); for (const item of data) item.detail = (await UnitContent.getContentDetail(item.id)) as GetContentDetailItem; generateMarkdownIch(dir, '非遗保护单位'); break; } case 'inheritor': { const dir = makeDir('inheritor'); (await InheritorContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => { data.push(item); }); for (const item of data) item.detail = (await InheritorContent.getContentDetail(item.id)) as GetContentDetailItem; generateMarkdownInheritor(dir); break; } case 'artifact': { const dir = makeDir('artifact'); (await UnmoveableContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => { data.push(item); }); for (const item of data) item.detail = (await UnmoveableContent.getContentDetail(item.id)) as GetContentDetailItem; generateMarkdownArtifact(dir); break; } default: console.log('不支持的类型'); break; } } main();