Skip to content

Commit

Permalink
feat: 1、新增修改「关于」信息的 api
Browse files Browse the repository at this point in the history
2、修改「获取mdx文件数据」api,可获取作者的mdx文件
  • Loading branch information
kangood committed Dec 11, 2023
1 parent 7289b5c commit a881d66
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
16 changes: 13 additions & 3 deletions src/modules/article/controller/article.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Query } from '@nestjs/common';
import { Body, Controller, Get, Post, Query } from '@nestjs/common';

import { BaseController } from '@/modules/restful/base';
import { Crud } from '@/modules/restful/decorators';
Expand All @@ -25,8 +25,8 @@ export class ArticleController extends BaseController<ArticleService> {
* 获取md文件数据
*/
@Get('getMdFileData')
getMdFileData(@Query('titleEng') titleEng: string) {
return this.service.getMdFileData(titleEng);
getMdFileData(@Query('titleEng') titleEng: string, @Query('author') author: string) {
return this.service.getMdFileData(titleEng, author);
}

/**
Expand All @@ -44,4 +44,14 @@ export class ArticleController extends BaseController<ArticleService> {
countListArticleTag() {
return this.service.countListArticleTag();
}

/**
* 修改关于信息,mdx 文件中的
*/
@Post('updateAboutInfo')
async updateAboutInfo(
@Body() data: { aboutContent: string; mdxContent: string; isMe: boolean },
) {
return this.service.updateAboutInfo(data);
}
}
42 changes: 35 additions & 7 deletions src/modules/article/service/article.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ export class ArticleService extends BaseService<ArticleEntity, ArticleRepository
}

/**
* 获取md文件数据
* 获取mdx文件数据
*/
async getMdFileData(titleEng: string) {
async getMdFileData(titleEng: string, author: string) {
let mdFileData = '';
let filePath = '';
// 加载并读取已上传的文件数据
const filePath = join(process.env.MD_FILE_PATH, `${titleEng}.mdx`);
if (titleEng) {
filePath = join(process.env.MD_FILE_PATH, `/blog/${titleEng}.mdx`);
} else if (author === 'kangod') {
filePath = join(process.env.MD_FILE_PATH, `/authors/kangod.mdx`);
}
if (filePath === '') {
return '';
}
await readFile(filePath).then(async (data) => {
mdFileData = data.toString();
});
Expand Down Expand Up @@ -77,8 +85,8 @@ export class ArticleService extends BaseService<ArticleEntity, ArticleRepository
* @param data
*/
async create(data: CreateArticleDto) {
// 文章内容需要写入md文件
const filePath = join(process.env.MD_FILE_PATH, `${data.titleEng}.mdx`);
// 文章内容需要写入mdx文件
const filePath = join(process.env.MD_FILE_PATH, `/blog/${data.titleEng}.mdx`);
writeFile(filePath, data.content);
// 获取通用参数
data.id = getSnowflakeId();
Expand All @@ -93,14 +101,34 @@ export class ArticleService extends BaseService<ArticleEntity, ArticleRepository
* @param data
*/
async update(data: UpdateArticleDto) {
// 文章内容需要写入md文件
const filePath = join(process.env.MD_FILE_PATH, `${data.titleEng}.mdx`);
// 文章内容需要写入mdx文件
const filePath = join(process.env.MD_FILE_PATH, `/blog/${data.titleEng}.mdx`);
writeFile(filePath, data.content);
// 执行更新
await this.repository.update(data.id, omit(data, ['id', 'content']));
return this.detail(data.id);
}

/**
* 修改关于信息
*/
async updateAboutInfo(data: { aboutContent: string; mdxContent: string; isMe: boolean }) {
// 关于信息,写入mdx文件
const filePath = join(process.env.MD_FILE_PATH, `/authors/kangod.mdx`);
// 使用正则表达式匹配关于数据
let match;
if (data.isMe) {
match = data.mdxContent.match(/##\s*关于我\s*👨‍💻([\s\S]*?)(?=##|$)/);
} else {
match = data.mdxContent.match(/##\s*关于本站\s*🌊([\s\S]*?)(?=$)/);
}
// 如果有匹配,替换匹配的内容
if (match) {
data.mdxContent = data.mdxContent.replace(match[1], `\n\n${data.aboutContent}\n\n`);
}
writeFile(filePath, data.mdxContent);
}

/**
* 构建博客列表查询器
* @param queryBuilder 初始查询构造器
Expand Down

0 comments on commit a881d66

Please sign in to comment.