Skip to content

Commit 7b705f9

Browse files
authored
Merge pull request #27 from devlive-community/dev
优化导航获取方式
2 parents d68049f + 0abd234 commit 7b705f9

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

docs/content/setup/compress.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: 压缩设置
3+
icon: combine
4+
---
5+
6+
PageForge 支持压缩文件,可以帮助你快速构建和部署你的静态网站。使用的是 [html-minifier](https://github.com/kangax/html-minifier "html-minifier") 模块,默认是禁用的,需要在 `pageforge.yaml` 配置文件中启用
7+
8+
## 启用压缩
9+
10+
---
11+
12+
```yaml
13+
feature:
14+
compress:
15+
enable: true
16+
```
17+
18+
- `enable`: 是否启用压缩
19+
20+
## 压缩设置
21+
22+
---
23+
24+
PageForge 支持使用 [html-minifier](https://github.com/kangax/html-minifier "html-minifier" "_blank") 模块的压缩配置,可以参考它来设置怎么压缩
25+
26+
```yaml
27+
feature:
28+
compress:
29+
enable: true
30+
options:
31+
collapseWhitespace: true
32+
removeComments: true
33+
removeEmptyAttributes: true
34+
```

docs/pageforge.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ nav:
117117
- /setup/page
118118
- /setup/feature
119119
- /setup/i18n
120+
- /setup/compress
120121
- InlineTemplate:
121122
- /setup/template/home
122123
- /setup/template/enterprise

lib/file-processor.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ class FileProcessor {
2626
this.pages = pages;
2727
this.sourcePath = sourcePath;
2828
this.outputPath = outputPath;
29+
this.navigationCache = new Map();
2930

3031
this.templateEngine = new TemplateEngine(config.templatePath);
3132

32-
this.minifyOptions = {
33+
// 设置基础压缩选项
34+
const baseOptions = {
3335
collapseWhitespace: true,
3436
removeComments: true,
3537
removeEmptyAttributes: true,
@@ -43,6 +45,20 @@ class FileProcessor {
4345
removeAttributeQuotes: true,
4446
removeOptionalTags: true
4547
};
48+
49+
// 合并用户配置
50+
this.minifyOptions = config.feature?.compress?.enable ?
51+
{ ...baseOptions, ...config.feature.compress.options } :
52+
false;
53+
}
54+
55+
// 获取缓存的导航数据
56+
getCachedNavigation(locale) {
57+
if (!this.navigationCache.has(locale)) {
58+
const navData = transformNavigation(this.config.nav, this.pages, this.config, locale);
59+
this.navigationCache.set(locale, navData);
60+
}
61+
return this.navigationCache.get(locale);
4662
}
4763

4864
// 解析元数据中的 EJS 模板
@@ -282,13 +298,13 @@ class FileProcessor {
282298
...this.config
283299
}
284300
}
285-
pageData.siteData.nav = transformNavigation(this.config.nav, this.pages, this.config, locale);
301+
pageData.siteData.nav = this.getCachedNavigation(locale);
286302

287303
// 渲染页面
288304
let html = await this.templateEngine.renderWithLayout('layouts/page', pageData);
289305

290306
// 压缩 HTML
291-
if (this.config.minify !== false) {
307+
if (this.config.feature?.compress?.enable !== false) {
292308
try {
293309
html = minify(html, this.minifyOptions);
294310
console.log(`🗜️ 压缩 ${relativePath} HTML 完成`);
@@ -328,7 +344,7 @@ class FileProcessor {
328344
...this.config
329345
}
330346
}
331-
pageData.siteData.nav = transformNavigation(this.config.nav, this.pages, this.config, locale);
347+
pageData.siteData.nav = this.getCachedNavigation(locale);
332348

333349
let html = await this.templateEngine.renderWithLayout('layouts/page', pageData);
334350

0 commit comments

Comments
 (0)