feat: add author and created to markdown render

This commit is contained in:
Innei
2021-09-27 11:46:22 +08:00
parent db8db822cc
commit af9034424c
2 changed files with 28 additions and 17 deletions

View File

@@ -3,11 +3,14 @@ import {
InternalServerErrorException,
Logger,
} from '@nestjs/common'
import { ReturnModelType } from '@typegoose/typegoose'
import { DocumentType, ReturnModelType } from '@typegoose/typegoose'
import { BeAnObject } from '@typegoose/typegoose/lib/types'
import { cloneDeep } from 'lodash'
import { LeanDocument } from 'mongoose'
import { InjectModel } from 'nestjs-typegoose'
import { API_VERSION } from '~/app.config'
import { sleep } from '~/utils/index.util'
import { UserModel } from '../user/user.model'
import { UserService } from '../user/user.service'
import { BackupOptionsDto, MailOptionsDto } from './configs.dto'
import { IConfig } from './configs.interface'
@@ -131,6 +134,8 @@ export class ConfigsService {
get getMaster() {
// HINT: 需要注入 this 的指向
return this.userService.getMaster.bind(this.userService)
return this.userService.getMaster.bind(this.userService) as () => Promise<
LeanDocument<DocumentType<UserModel, BeAnObject>>
>
}
}

View File

@@ -21,7 +21,6 @@ import { Auth } from '~/common/decorator/auth.decorator'
import { HTTPDecorators } from '~/common/decorator/http.decorator'
import { ApiName } from '~/common/decorator/openapi.decorator'
import { ArticleTypeEnum } from '~/constants/article.constant'
import { AssetService } from '~/processors/helper/hepler.asset.service'
import { MongoIdDto } from '~/shared/dto/id.dto'
import { CategoryModel } from '../category/category.model'
import { ConfigsService } from '../configs/configs.service'
@@ -38,7 +37,6 @@ export class MarkdownController {
constructor(
private readonly service: MarkdownService,
private readonly assetService: AssetService,
private readonly configs: ConfigsService,
) {}
@@ -163,11 +161,17 @@ export class MarkdownController {
) {
const { id } = params
const now = performance.now()
const {
html: markdown,
document,
type,
} = await this.service.renderArticle(id)
const [
{ html: markdown, document, type },
{
url: { webUrl },
},
{ name: username },
] = await Promise.all([
this.service.renderArticle(id),
this.configs.waitForConfigReady(),
this.configs.getMaster(),
])
const relativePath = (() => {
switch (type.toLowerCase()) {
@@ -181,9 +185,7 @@ export class MarkdownController {
return `/${(document as PageModel).slug}`
}
})()
const {
url: { webUrl },
} = await this.configs.waitForConfigReady()
const url = new URL(relativePath, webUrl)
const { style, link, script, extraScripts, body } =
@@ -213,11 +215,15 @@ export class MarkdownController {
<body>
${body.join('\n')}
</body>
<footer style="text-align: right; padding: 2em 0;">
<p>本文渲染于 ${dayjs().format('llll')},用时 ${
performance.now() - now
}ms</p>
<p>原文地址:<a href="${url}">${decodeURIComponent(
<footer style="text-align: right; padding: 2em 0; font-size: .8em">
<p>本文渲染于 ${dayjs().format(
'MM/DD/YY H:mm:ss',
)},由 marked.js 解析生成,用时 ${(performance.now() - now).toFixed(
2,
)}ms</p>
<p>作者:${username},撰写于${dayjs(document.created).format(
'llll',
)},原文地址:<a href="${url}">${decodeURIComponent(
url.toString(),
)}</a></p>
</footer>