Skip to content

Commit

Permalink
perf(theme): use art-template, kill fucking fastify-ejs
Browse files Browse the repository at this point in the history
  • Loading branch information
wibus-wee committed Aug 14, 2022
1 parent b0e57c9 commit 1f73193
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function bootstrap() {
});
app.setViewEngine({
engine: {
ejs: require("ejs"),
"art-template": require("art-template"),
},
templates: join(THEME_DIR), // 模板目录,模板名字应在 Render 中指定
viewExt: "ejs", // 模板文件的后缀名
Expand Down
34 changes: 17 additions & 17 deletions src/modules/theme/theme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class ThemeController {
@ApiOperation({ summary: '首页' })
async renderIndex(@Res() res, @Query() pager: PagerDto) {
return await res.view(
`${(await this.themeService.currentTheme())!.name}/index.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/index` as string,
{
...(await this.basicProps()),
path: '/',
Expand All @@ -124,26 +124,26 @@ export class ThemeController {
async renderPage(@Res() res, @Param("path") path: string) {
const page = await this.pageService.model.findOne({ path })
if (!page) {
const filePath = join(THEME_DIR, (await this.themeService.currentTheme())!.name, `page-${path}.ejs`);
const filePath = join(THEME_DIR, (await this.themeService.currentTheme())!.name, `page-${path}`);
if (!fs.existsSync(filePath)) {
return res.view(
`${(await this.themeService.currentTheme())!.name}/page-${path}.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/page-${path}` as string,
{
...(await this.basicProps()),
path,
} as ThemeBasicInterface,
)
}
return res.view(
`${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/404` as string,
{
...(await this.basicProps()),
path,
} as ThemeBasicInterface,
)
}
return await res.view(
`${(await this.themeService.currentTheme())!.name}/page.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/page` as string,
{
...(await this.basicProps()),
path,
Expand All @@ -166,7 +166,7 @@ export class ThemeController {
if (password === undefined || !password) password = null;
if (!categoryDocument) {
res.view(
`${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/404` as string,
{
...(await this.basicProps()),
path: `/${category}/${slug}`,
Expand All @@ -184,7 +184,7 @@ export class ThemeController {
.then(async (postDocument) => {
if (!postDocument) {
res.view(
`${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/404` as string,
{
...(await this.basicProps()),
path: `/${category}/${slug}`,
Expand All @@ -210,7 +210,7 @@ export class ThemeController {

if (!postDocument || postDocument.hide) {
return res.view(
`${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/404` as string,
{
...(await this.basicProps()),
path: `/${category}/${slug}`,
Expand All @@ -228,7 +228,7 @@ export class ThemeController {
}),
};
return res.view(
`${(await this.themeService.currentTheme())!.name}/post.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/post` as string,
data,
);
}
Expand All @@ -237,7 +237,7 @@ export class ThemeController {
@ApiOperation({ summary: '归档' })
async renderArchive(@Res() res) {
return await res.view(
`${(await this.themeService.currentTheme())!.name}/archive.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/archive` as string,
{
...(await this.basicProps()),
path: '/archive',
Expand All @@ -251,7 +251,7 @@ export class ThemeController {
const pageDoc = await this.categoryService.model.findOne({ slug }).sort({ created: -1 }).lean()
if (!pageDoc) {
return res.view(
`${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/404` as string,
{
...(await this.basicProps()),
path: `/category/${slug}`,
Expand All @@ -271,7 +271,7 @@ export class ThemeController {
page,
}
return res.view(
`${(await this.themeService.currentTheme())!.name}/category.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/category` as string,
data,
);
}
Expand All @@ -285,7 +285,7 @@ export class ThemeController {
}
if (!page.data.length) {
return res.view(
`${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/404` as string,
{
...(await this.basicProps()),
path: `/tag/${slug}`,
Expand All @@ -298,7 +298,7 @@ export class ThemeController {
page,
}
return res.view(
`${(await this.themeService.currentTheme())!.name}/tag.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/tag` as string,
data,
);
}
Expand All @@ -322,7 +322,7 @@ export class ThemeController {
links: linksData,
}
return res.view(
`${(await this.themeService.currentTheme())!.name}/links.ejs` as string,
`${(await this.themeService.currentTheme())!.name}/links` as string,
data,
);
}
Expand All @@ -335,10 +335,10 @@ export class ThemeController {
// @Param("props") props: string,
// ) {
// // 检查是否存在该路径对应的文件
// const filePath = join(THEME_DIR, (await this.themeService.currentTheme())!.name, `page-${path}.ejs`);
// const filePath = join(THEME_DIR, (await this.themeService.currentTheme())!.name, `page-${path}`);
// if (!fs.existsSync(filePath)) {
// return res.view(
// `${(await this.themeService.currentTheme())!.name}/404.ejs` as string,
// `${(await this.themeService.currentTheme())!.name}/404` as string,
// {
// ...(await this.basicProps()),
// path: `/${path}`,
Expand Down

0 comments on commit 1f73193

Please sign in to comment.