Skip to content

Commit

Permalink
feat(markdown)!: import markdown service
Browse files Browse the repository at this point in the history
  • Loading branch information
wibus-wee committed Jul 19, 2022
1 parent 6bd4079 commit 0cd2f99
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/modules/markdown/markdown.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { Auth } from '~/common/decorator/auth.decorator';
import { HTTPDecorators } from '~/common/decorator/http.decorator';
import { ApiName } from '~/common/decorator/openapi.decorator';
import { CategoryModel } from '../category/category.model';
import { ExportMarkdownDto } from './markdown.dto';
import { ExportMarkdownDto, ImportMarkdownDto } from './markdown.dto';
import { MarkdownService } from './markdown.service';
import JSZip from 'jszip';
import { join } from 'path';
import { ArticleTypeEnum } from './markdown.interface';
@Controller('markdown')
@ApiName
export class MarkdownController {
Expand Down Expand Up @@ -147,4 +148,20 @@ export class MarkdownController {
// return readable

}

@Post('/import')
@Auth()
@ApiProperty({ description: '导入 Markdown YAML 数据' })
async importMarkdownData(@Body() body: ImportMarkdownDto) {
const type = body.type

switch (type) {
case ArticleTypeEnum.Post: {
return await this.markdownService.importPostMarkdownData(body.data)
}
case ArticleTypeEnum.Page: {
return await this.markdownService.importPageMarkdownData(body.data)
}
}
}
}
28 changes: 26 additions & 2 deletions src/modules/markdown/markdown.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,56 @@
* @author: Wibus
* @Date: 2022-07-18 21:25:29
* @LastEditors: Wibus
* @LastEditTime: 2022-07-19 11:36:22
* @LastEditTime: 2022-07-19 13:00:23
* Coding With IU
*/

import { ApiProperty } from "@nestjs/swagger";
import { Transform, Type } from "class-transformer";
import { IsBoolean, IsDate, IsOptional, IsString, ValidateNested } from "class-validator";
import { IsBoolean, IsDate, IsEnum, IsOptional, IsString, ValidateNested } from "class-validator";
import { ArticleTypeEnum } from "./markdown.interface";


export class MarkdownMetaDto {
@IsString()
@ApiProperty({ description: '文章标题' })
title: string;

@Transform(({ value }) => new Date(value))
@IsDate()
@ApiProperty({ description: '文章创建时间' })
date: Date

@Transform(({ value }) => new Date(value))
@IsDate()
@IsOptional()
@ApiProperty({ description: '文章修改时间' })
updated?: Date

@IsString({ each: true })
@IsOptional()
@ApiProperty({ description: '文章分类' })
categories?: string[]

@IsString({ each: true })
@IsOptional()
@ApiProperty({ description: '文章标签' })
tags?: string[]

@IsString()
@ApiProperty({ description: '文章路径' })
slug: string;
}

export class DataDto {
@ValidateNested()
@IsOptional()
@Type(() => MarkdownMetaDto)
@ApiProperty({ description: '文章元数据' })
meta?: MarkdownMetaDto;

@IsString()
@ApiProperty({ description: '文章内容' })
text: string;
}

Expand All @@ -66,3 +75,18 @@ export class ExportMarkdownDto {
@ApiProperty({ description: 'Markdown 文件第一行是否显示标题' })
showTitle?: boolean;
}


export class ImportMarkdownDto {
@IsEnum(ArticleTypeEnum)
@Transform(({ value }) =>
typeof value === 'string' ? value.toLowerCase() : value,
)
@ApiProperty({ description: '文章类型' })
type: ArticleTypeEnum

@ValidateNested({ each: true })
@Type(() => DataDto)
@ApiProperty({ description: '文章数据' })
data: DataDto[]
}
31 changes: 31 additions & 0 deletions src/modules/markdown/markdown.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,35 @@ ${text.trim()}
})

}

/**
* 导入页面
* @param data 数据
*/
async importPageMarkdownData(data: DataDto[]) {
let count = 1
const models = [] as PageModel[]
for await (const item of data) {
if (item.meta) {
models.push({
title: item.meta.title,
slug: item.meta.slug || item.meta.title.replace(/\s/g, '-'),
text: item.text,
...this.genDate(item),
} as PageModel)
} else {
models.push({
title: `未命名-${count++}`,
slug: new Date().getTime(),
text: item.text,
...this.genDate(item),
} as any as PageModel)
}
}
return await this.pageModel.insertMany(models, { ordered: false }).catch(err => {
Logger.warn(`一个或多个页面导入失败:${err.message}`, MarkdownService.name)
})
}


}

0 comments on commit 0cd2f99

Please sign in to comment.