ich.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { writeFile } from 'fs/promises';
  2. import { GetContentListParams, type GetContentDetailItem, type GetContentListItem } from '~/api/CommonContent';
  3. import ProjectsContent from '~/api/inheritor/ProjectsContent';
  4. const data = [] as Array<GetContentListItem & { detail?: GetContentDetailItem }>;
  5. const markdownText = ref('');
  6. // HTML转Markdown的简单实现
  7. function htmlToMarkdown(html: string): string {
  8. if (!html) return '';
  9. // 处理标题
  10. html = html.replace(/<h1[^>]*>(.*?)<\/h1>/gi, '# $1\n\n');
  11. html = html.replace(/<h2[^>]*>(.*?)<\/h2>/gi, '## $1\n\n');
  12. html = html.replace(/<h3[^>]*>(.*?)<\/h3>/gi, '### $1\n\n');
  13. // 处理段落
  14. html = html.replace(/<p[^>]*>(.*?)<\/p>/gi, '$1\n\n');
  15. // 处理加粗
  16. html = html.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '**$1**');
  17. html = html.replace(/<b[^>]*>(.*?)<\/b>/gi, '**$1**');
  18. // 处理斜体
  19. html = html.replace(/<em[^>]*>(.*?)<\/em>/gi, '*$1*');
  20. html = html.replace(/<i[^>]*>(.*?)<\/i>/gi, '*$1*');
  21. // 处理列表
  22. html = html.replace(/<ul[^>]*>([\s\S]*?)<\/ul>/gi, (match, content) => {
  23. return content.replace(/<li[^>]*>(.*?)<\/li>/gi, '- $1\n') + '\n';
  24. });
  25. html = html.replace(/<ol[^>]*>([\s\S]*?)<\/ol>/gi, (match, content, index) => {
  26. let count = 1;
  27. return content.replace(/<li[^>]*>(.*?)<\/li>/gi, () => {
  28. return `${count++}. $1\n`;
  29. }) + '\n';
  30. });
  31. // 处理图片
  32. html = html.replace(/<img[^>]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/gi, '![$2]($1)');
  33. // 处理链接
  34. html = html.replace(/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)');
  35. // 处理换行
  36. html = html.replace(/<br[^>]*>/gi, '\n');
  37. // 去除所有HTML标签
  38. html = html.replace(/<[^>]*>/g, '');
  39. // 处理多余的换行
  40. html = html.replace(/\n\s*\n/g, '\n\n');
  41. return html.trim();
  42. }
  43. // 生成Markdown文本
  44. function generateMarkdown(): string {
  45. let md = '';
  46. data.forEach((item, index) => {
  47. if (index > 0) {
  48. md += '\n\n---\n\n';
  49. }
  50. // 基本信息
  51. md += `# ${item.title}\n\n`;
  52. md += `${item.desc || '无描述'}\n\n`;
  53. md += `## 基本信息\n\n`;
  54. md += `- 非遗级别: ${item.levelText || '无'}\n`;
  55. md += `- 非遗类别: ${item.ichTypeText || '无'}\n`;
  56. md += `- 地区: ${item.district || '无'}\n`;
  57. md += `- 批次: ${item.batchText || '无'}\n`;
  58. md += `- 保护单位: ${item.unit || '无'}\n\n`;
  59. // 图片
  60. if (item.images && item.images.length > 0) {
  61. md += `## 图片\n\n`;
  62. item.images.forEach(image => {
  63. md += `![${item.title}图片](${image})\n\n`;
  64. });
  65. }
  66. // 详细信息
  67. if (item.detail) {
  68. // 简介
  69. if (item.detail.intro) {
  70. md += `## 简介\n\n`;
  71. md += htmlToMarkdown(item.detail.intro) + '\n\n';
  72. }
  73. // 传承人
  74. if (item.detail.inheritorsList && item.detail.inheritorsList.length > 0) {
  75. md += `## 相关传承人\n\n`;
  76. if (item.detail.inheritor) {
  77. md += htmlToMarkdown(item.detail.inheritor) + '\n\n';
  78. }
  79. item.detail.inheritorsList.forEach(inheritor => {
  80. md += `### ${inheritor.title}\n\n`;
  81. md += `级别:${inheritor.levelLext || '无'}\n\n`;
  82. if (inheritor.image) {
  83. md += `![传承人头像](${inheritor.image})\n\n`;
  84. }
  85. });
  86. }
  87. // 传习所
  88. if (item.detail.ichSitesList && item.detail.ichSitesList.length > 0) {
  89. md += `## 相关传习所\n\n`;
  90. item.detail.ichSitesList.forEach(site => {
  91. md += `### ${site.title}\n\n`;
  92. md += `级别:${site.levelLext || '无'}\n\n`;
  93. md += `地址:${site.address || '无'}\n\n`;
  94. if (site.image) {
  95. md += `![传习所图片](${site.image})\n\n`;
  96. }
  97. });
  98. }
  99. }
  100. });
  101. return md;
  102. }
  103. (await ProjectsContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => {
  104. data.push(item);
  105. });
  106. for (const item of data) {
  107. item.detail = (await ProjectsContent.getContentDetail(item.id)) as GetContentDetailItem;
  108. }
  109. // 生成Markdown文本
  110. markdownText.value = generateMarkdown();
  111. await writeFile('非遗项目数据.md', markdownText.value);