| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- 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<GetContentListItem & { detail?: GetContentDetailItem }>;
- // HTML转Markdown的简单实现
- function htmlToMarkdown(html: string): string {
- if (!html) return '';
-
- // 处理标题
- html = html.replace(/<h1[^>]*>(.*?)<\/h1>/gi, '# $1\n\n');
- html = html.replace(/<h2[^>]*>(.*?)<\/h2>/gi, '## $1\n\n');
- html = html.replace(/<h3[^>]*>(.*?)<\/h3>/gi, '### $1\n\n');
-
- // 处理段落
- html = html.replace(/<p[^>]*>(.*?)<\/p>/gi, '$1\n\n');
-
- // 处理加粗
- html = html.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '**$1**');
- html = html.replace(/<b[^>]*>(.*?)<\/b>/gi, '**$1**');
-
- // 处理斜体
- html = html.replace(/<em[^>]*>(.*?)<\/em>/gi, '*$1*');
- html = html.replace(/<i[^>]*>(.*?)<\/i>/gi, '*$1*');
-
- // 处理列表
- html = html.replace(/<ul[^>]*>([\s\S]*?)<\/ul>/gi, (match, content) => {
- return content.replace(/<li[^>]*>(.*?)<\/li>/gi, '- $1\n') + '\n';
- });
-
- html = html.replace(/<ol[^>]*>([\s\S]*?)<\/ol>/gi, (match, content, index) => {
- let count = 1;
- return content.replace(/<li[^>]*>(.*?)<\/li>/gi, () => {
- return `${count++}. $1\n`;
- }) + '\n';
- });
-
- // 处理图片
- html = html.replace(/<img[^>]*src="([^"]*)"[^>]*alt="([^"]*)"[^>]*>/gi, '');
-
- // 处理链接
- html = html.replace(/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)');
-
- // 处理换行
- html = html.replace(/<br[^>]*>/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 += `\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();
|