ich.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import { writeFile } from 'fs/promises';
  2. import CommonContent, { GetContentListParams, type GetContentDetailItem, type GetContentListItem } from '../../api/CommonContent';
  3. import ProjectsContent from '../../api/inheritor/ProjectsContent';
  4. import InheritorContent from '../../api/inheritor/InheritorContent';
  5. import SeminarContent from '../../api/inheritor/SeminarContent';
  6. import UnitContent from '../../api/inheritor/UnitContent';
  7. import UnmoveableContent from '../../api/inheritor/UnmoveableContent';
  8. import path from 'path';
  9. import fs from 'fs';
  10. import { argv, cwd } from 'process';
  11. const data = [] as Array<GetContentListItem & { detail?: GetContentDetailItem }>;
  12. // HTML转Markdown的简单实现
  13. function htmlToMarkdown(html: string): string {
  14. if (!html) return '';
  15. // 处理标题
  16. html = html.replace(/<h1[^>]*>(.*?)<\/h1>/gi, '# $1\n\n');
  17. html = html.replace(/<h2[^>]*>(.*?)<\/h2>/gi, '## $1\n\n');
  18. html = html.replace(/<h3[^>]*>(.*?)<\/h3>/gi, '### $1\n\n');
  19. // 处理段落
  20. html = html.replace(/<p[^>]*>(.*?)<\/p>/gi, '$1\n\n');
  21. // 处理加粗
  22. html = html.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '**$1**');
  23. html = html.replace(/<b[^>]*>(.*?)<\/b>/gi, '**$1**');
  24. // 处理斜体
  25. html = html.replace(/<em[^>]*>(.*?)<\/em>/gi, '*$1*');
  26. html = html.replace(/<i[^>]*>(.*?)<\/i>/gi, '*$1*');
  27. // 处理列表
  28. html = html.replace(/<ul[^>]*>([\s\S]*?)<\/ul>/gi, (match, content) => {
  29. return content.replace(/<li[^>]*>(.*?)<\/li>/gi, '- $1\n') + '\n';
  30. });
  31. html = html.replace(/<ol[^>]*>([\s\S]*?)<\/ol>/gi, (match, content, index) => {
  32. let count = 1;
  33. return content.replace(/<li[^>]*>(.*?)<\/li>/gi, () => {
  34. return `${count++}. $1\n`;
  35. }) + '\n';
  36. });
  37. // 处理图片
  38. html = html.replace(/<img[^>]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/gi, '![$2]($1)');
  39. // 处理链接
  40. html = html.replace(/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)');
  41. // 处理换行
  42. html = html.replace(/<br[^>]*>/gi, '\n');
  43. // 去除所有HTML标签
  44. html = html.replace(/<[^>]*>/g, '');
  45. // 处理多余的换行
  46. html = html.replace(/\n\s*\n/g, '\n\n');
  47. return html.trim();
  48. }
  49. // 生成Markdown文本
  50. async function generateMarkdownIch(subDir: string, type: string) {
  51. for (const item of data) {
  52. let md = '';
  53. // 基本信息
  54. md += `# ${item.title}\n\n`;
  55. if (item.desc)
  56. md += `${item.desc}\n\n`;
  57. md += `## 基本信息\n\n`;
  58. md += `类型:${type}\n\n`;
  59. function addRow(key: string, value: any) {
  60. if (value)
  61. md += `- ${key}: ${value}\n`;
  62. }
  63. addRow('级别', item.levelText);
  64. addRow('类别', item.ichTypeText);
  65. addRow('地区', item.regionText);
  66. addRow('批次', item.batchText);
  67. addRow('保护单位', item.unit);
  68. addRow('地址', item.address);
  69. addRow('字号名称', item.fontName);
  70. addRow('认定类型', item.brandType);
  71. addRow('其他级别保护单位', item.detail?.otherLevel && item.detail.otherLevel.length > 0 ? `${item.detail.otherLevel.length}个` : '');
  72. md += `\n## 数据库索引ID\n\n`;
  73. md += `- 类型: intangible\n`;
  74. md += `- ID: ${item.id || '无'}\n\n`;
  75. // 详细信息
  76. if (item.detail) {
  77. const detail = item.detail as GetContentDetailItem;
  78. // 简介
  79. if (detail.intro) {
  80. md += `## 简介\n\n`;
  81. md += htmlToMarkdown(detail.intro) + '\n\n';
  82. }
  83. // 内容
  84. if (detail.content) {
  85. md += `## 内容\n\n`;
  86. md += htmlToMarkdown(detail.content) + '\n\n';
  87. }
  88. // 传承谱系
  89. if (detail.pedigree) {
  90. md += `## 传承谱系\n\n`;
  91. md += htmlToMarkdown(detail.pedigree as string) + '\n\n';
  92. }
  93. // 视频
  94. if (detail.video) {
  95. md += `## 视频\n\n`;
  96. md += `[视频](${detail.video})\n\n`;
  97. }
  98. if (detail.publishVideo)
  99. md += `[介绍视频](${detail.publishVideo})\n\n`;
  100. // 传承人
  101. if (detail.inheritorsList && detail.inheritorsList.length > 0) {
  102. md += `## 相关传承人\n\n`;
  103. if (detail.inheritor) {
  104. md += htmlToMarkdown(detail.inheritor) + '\n\n';
  105. }
  106. detail.inheritorsList.forEach(inheritor => {
  107. md += `### ${inheritor.title}\n\n`;
  108. md += `级别:${inheritor.levelLext || '无'}\n\n`;
  109. md += `#### 数据库索引ID\n\n`;
  110. md += `- 类型: inheritor\n`;
  111. md += `- ID: ${inheritor.id || '无'}\n\n`;
  112. });
  113. }
  114. // 传习所
  115. if (detail.ichSitesList && detail.ichSitesList.length > 0) {
  116. md += `## 相关传习所\n\n`;
  117. detail.ichSitesList.forEach(site => {
  118. md += `### ${site.title}\n\n`;
  119. md += `级别:${site.levelLext || '无'}\n\n`;
  120. md += `地址:${site.address || '无'}\n\n`;
  121. md += `#### 数据库索引ID\n\n`;
  122. md += `- 类型: seminar\n`;
  123. md += `- ID: ${site.id || '无'}\n\n`;
  124. });
  125. }
  126. // 同级别项目
  127. if (detail.otherLevel && detail.otherLevel.length > 0) {
  128. md += `## 其他级别非遗项目\n\n`;
  129. detail.otherLevel.forEach(project => {
  130. md += `### ${project.title}\n\n`;
  131. md += `级别:${project.levelLext || '无'}\n\n`;
  132. md += `保护单位:${project.unit || '无'}\n\n`;
  133. md += `#### 数据库索引ID\n\n`;
  134. md += `- 类型: intangible\n`;
  135. md += `- ID: ${project.id || '无'}\n\n`;
  136. });
  137. }
  138. }
  139. await writeFile(path.join(subDir, `${item.id}.md`), md);
  140. }
  141. }
  142. async function generateMarkdownInheritor(subDir: string) {
  143. for (const item of data) {
  144. let md = '';
  145. // 基本信息
  146. md += `# ${item.title}\n\n`;
  147. if (item.desc)
  148. md += `${item.desc}\n\n`;
  149. md += `## 基本信息\n\n`;
  150. md += `类型:非遗传承人\n\n`;
  151. function addRow(key: string, value: any) {
  152. if (value)
  153. md += `- ${key}: ${value}\n`;
  154. }
  155. addRow('民族', item.detail?.nation);
  156. addRow('性别', item.detail?.gender == '1'? '男' : '女');
  157. addRow('出生日期', item.detail?.dateBirth);
  158. addRow('出生地区', item.detail?.birthplace);
  159. addRow('单位', item.detail?.unit);
  160. addRow('传承项目', item.detail?.associationMeList[0]?.title);
  161. addRow('传承人级别', item.detail?.batchText);
  162. addRow('公布批次', item.detail?.batchText);
  163. md += `\n## 数据库索引ID\n\n`;
  164. md += `- 类型: inheritor\n`;
  165. md += `- ID: ${item.id || '无'}\n\n`;
  166. // 详细信息
  167. if (item.detail) {
  168. const detail = item.detail as GetContentDetailItem;
  169. // 简介
  170. if (detail.intro) {
  171. md += `## 简介\n\n`;
  172. md += htmlToMarkdown(detail.intro) + '\n\n';
  173. }
  174. if (detail.content) {
  175. md += `## 详情\n\n`;
  176. md += htmlToMarkdown(detail.content) + '\n\n';
  177. }
  178. // 奖项
  179. if (detail.prize) {
  180. md += `## 奖项\n\n`;
  181. md += htmlToMarkdown(detail.prize as string) + '\n\n';
  182. }
  183. // 相关项目
  184. if (detail.associationMeList && detail.associationMeList.length > 0) {
  185. md += `## 相关项目\n\n`;
  186. detail.associationMeList.forEach(inheritor => {
  187. md += `### ${inheritor.title}\n\n`;
  188. md += `#### 数据库索引ID\n\n`;
  189. md += `- 类型: intangible\n`;
  190. md += `- ID: ${inheritor.id || '无'}\n\n`;
  191. });
  192. }
  193. // 传习所
  194. if (detail.ichSitesList && detail.ichSitesList.length > 0) {
  195. md += `## 相关传习所\n\n`;
  196. detail.ichSitesList.forEach(site => {
  197. md += `### ${site.title}\n\n`;
  198. md += `级别:${site.levelLext || '无'}\n\n`;
  199. md += `地址:${site.address || '无'}\n\n`;
  200. md += `##### 数据库索引ID\n\n`;
  201. md += `- 类型: seminar\n`;
  202. md += `- ID: ${site.id || '无'}\n\n`;
  203. });
  204. }
  205. }
  206. await writeFile(path.join(subDir, `${item.id}.md`), md);
  207. }
  208. }
  209. async function generateMarkdownArtifact(subDir: string) {
  210. for (const item of data) {
  211. let md = '';
  212. // 基本信息
  213. md += `# ${item.title}\n\n`;
  214. if (item.desc)
  215. md += `${item.desc}\n\n`;
  216. md += `## 基本信息\n\n`;
  217. md += `类型:非遗传承人\n\n`;
  218. function addRow(key: string, value: any) {
  219. if (value)
  220. md += `- ${key}: ${value}\n`;
  221. }
  222. addRow('开放时间', item.detail?.openStatusText);
  223. addRow('年代', item.age);
  224. addRow('级别', item.levelText);
  225. addRow('所属区域', item.regionText);
  226. addRow('文物类型', item.crTypeText);
  227. addRow('单位', item.detail?.unit);
  228. md += `\n## 数据库索引ID\n\n`;
  229. md += `- 类型: artifact\n`;
  230. md += `- ID: ${item.id || '无'}\n\n`;
  231. if (item.video) {
  232. md += `## 视频\n\n`;
  233. md += `![${item.title}视频](${item.video})\n\n`;
  234. }
  235. // 详细信息
  236. if (item.detail) {
  237. const detail = item.detail as GetContentDetailItem;
  238. // 简介
  239. if (detail.intro) {
  240. md += `## 简介\n\n`;
  241. md += htmlToMarkdown(detail.intro) + '\n\n';
  242. }
  243. if (detail.content) {
  244. md += `## 详情\n\n`;
  245. md += htmlToMarkdown(detail.content) + '\n\n';
  246. }
  247. // 奖项
  248. if (detail.protectedArea) {
  249. md += `## 保护范围\n\n`;
  250. md += htmlToMarkdown(detail.protectedArea as string) + '\n\n';
  251. }
  252. if (detail.environment) {
  253. md += `## 建筑环境\n\n`;
  254. md += htmlToMarkdown(detail.environment as string) + '\n\n';
  255. }
  256. if (detail.价值评估) {
  257. md += `## 价值评估\n\n`;
  258. md += htmlToMarkdown(detail.价值评估 as string) + '\n\n';
  259. }
  260. }
  261. await writeFile(path.join(subDir, `${item.id}.md`), md);
  262. }
  263. }
  264. async function main() {
  265. const type = argv[2];
  266. function makeDir(nanme: string) {
  267. const dir = path.join(cwd(), `dist/${nanme}`);
  268. if (!fs.existsSync(dir))
  269. fs.mkdirSync(dir, { recursive: true });
  270. return dir;
  271. }
  272. switch (type) {
  273. case 'ich': {
  274. const dir = makeDir('ich');
  275. (await ProjectsContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => {
  276. data.push(item);
  277. });
  278. for (const item of data)
  279. item.detail = (await ProjectsContent.getContentDetail(item.id)) as GetContentDetailItem;
  280. generateMarkdownIch(dir, '非遗项目');
  281. break;
  282. }
  283. case 'seminar': {
  284. const dir = makeDir('seminar');
  285. (await SeminarContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => {
  286. data.push(item);
  287. });
  288. for (const item of data)
  289. item.detail = (await SeminarContent.getContentDetail(item.id)) as GetContentDetailItem;
  290. generateMarkdownIch(dir, '非遗传习所');
  291. break;
  292. }
  293. case 'old': {
  294. const dir = makeDir('old');
  295. (await CommonContent.getContentList(new GetContentListParams()
  296. .setModelId(17)
  297. .setMainBodyColumnId(312)
  298. , 1, 1000)).list.forEach(item => {
  299. data.push(item);
  300. });
  301. for (const item of data)
  302. item.detail = (await CommonContent.getContentDetail(item.id)) as GetContentDetailItem;
  303. generateMarkdownIch(dir, '老字号');
  304. break;
  305. }
  306. case 'unit': {
  307. const dir = makeDir('unit');
  308. (await UnitContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => {
  309. data.push(item);
  310. });
  311. for (const item of data)
  312. item.detail = (await UnitContent.getContentDetail(item.id)) as GetContentDetailItem;
  313. generateMarkdownIch(dir, '非遗保护单位');
  314. break;
  315. }
  316. case 'inheritor': {
  317. const dir = makeDir('inheritor');
  318. (await InheritorContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => {
  319. data.push(item);
  320. });
  321. for (const item of data)
  322. item.detail = (await InheritorContent.getContentDetail(item.id)) as GetContentDetailItem;
  323. generateMarkdownInheritor(dir);
  324. break;
  325. }
  326. case 'artifact': {
  327. const dir = makeDir('artifact');
  328. (await UnmoveableContent.getContentList(new GetContentListParams(), 1, 1000)).list.forEach(item => {
  329. data.push(item);
  330. });
  331. for (const item of data)
  332. item.detail = (await UnmoveableContent.getContentDetail(item.id)) as GetContentDetailItem;
  333. generateMarkdownArtifact(dir);
  334. break;
  335. }
  336. default:
  337. console.log('不支持的类型');
  338. break;
  339. }
  340. }
  341. main();