Skip to content

Commit 6c4ead2

Browse files
committed
支持字体大小调节 (close #13)
1 parent 2851743 commit 6c4ead2

File tree

7 files changed

+134
-17
lines changed

7 files changed

+134
-17
lines changed

docs/content/setup/font-size.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: 字体设置
3+
icon: type
4+
status:
5+
type: new
6+
---
7+
8+
PageForge 支持设置字体大小。默认是禁用的,需要在 `pageforge.yaml` 配置文件中启用。
9+
10+
## 基本用法
11+
12+
---
13+
14+
```yaml
15+
feature:
16+
fontSize:
17+
enable: true
18+
```
19+
20+
启用后会在页面标题右侧出现一个字体调整按钮,可以调整字体大小。

docs/content/setup/i18n.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ icon: globe
55

66
PageForge 支持用户自定义的国际化设置,需要启用后才能使用国际化。
77

8+
### 启用国际化
9+
10+
---
11+
812
```yaml
913
i18n:
1014
enable: true
@@ -37,6 +41,10 @@ i18n:
3741

3842
系统会通过 `pageforge.yaml` 中配置的语言来自动加载对应的翻译,如果没有找到翻译,则会返回原 key
3943

44+
### 使用国际化
45+
46+
---
47+
4048
在 `pageforge.yaml` 中配置国际化后,可以在 `nav` 中使用以下方式来获取翻译的结果。
4149

4250
```yaml

docs/pageforge.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ feature:
6767
weibo: true
6868
sitemap:
6969
enable: true
70+
fontSize:
71+
enable: true
7072

7173
i18n:
7274
default: zh-CN
@@ -144,6 +146,7 @@ nav:
144146
- /setup/share
145147
- /setup/sitemap
146148
- /setup/banner
149+
- /setup/font-size
147150
- InlineTemplate:
148151
- /setup/template/home
149152
- /setup/template/enterprise

templates/assets/js/pageforge.js

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ const DOMUtils = {
88
// GitHub Stats 模块
99
const GitHubStats = {
1010
formatNumber(num) {
11-
if (num >= 1000000) return (num / 1000000).toFixed(1) + 'm';
12-
if (num >= 1000) return (num / 1000).toFixed(1) + 'k';
11+
if (num >= 1000000) {
12+
return (num / 1000000).toFixed(1) + 'm';
13+
}
14+
if (num >= 1000) {
15+
return (num / 1000).toFixed(1) + 'k';
16+
}
1317
return num.toString();
1418
},
1519

@@ -65,7 +69,9 @@ const GitHubStats = {
6569
fetch(`https://api.github.com/repos/${owner}/${repo}/tags`, {headers})
6670
]);
6771

68-
if (!repoResponse.ok || !tagsResponse.ok) throw new Error('GitHub API request failed');
72+
if (!repoResponse.ok || !tagsResponse.ok) {
73+
throw new Error('GitHub API request failed');
74+
}
6975

7076
const [repoData, tagsData] = await Promise.all([
7177
repoResponse.json(),
@@ -83,18 +89,21 @@ const GitHubStats = {
8389

8490
this.updateUI(stats);
8591
return stats;
86-
} catch (error) {
92+
}
93+
catch (error) {
8794
console.error('🤯 获取 GitHub Stats 失败', error);
88-
const fallbackStats = { stars: '-', forks: '-', tag: '-' };
95+
const fallbackStats = {stars: '-', forks: '-', tag: '-'};
8996
this.updateUI(fallbackStats);
9097
return fallbackStats;
9198
}
9299
},
93100

94101
updateUI(stats) {
95-
if (!stats) return;
102+
if (!stats) {
103+
return;
104+
}
96105

97-
const { findAll } = DOMUtils;
106+
const {findAll} = DOMUtils;
98107
const containers = {
99108
tag: findAll('.tag-container'),
100109
stars: findAll('.stars-container'),
@@ -115,7 +124,9 @@ const GitHubStats = {
115124
},
116125

117126
init(owner, repo) {
118-
if (!owner || !repo) return;
127+
if (!owner || !repo) {
128+
return;
129+
}
119130
this.showLoading();
120131
const updateStats = async () => await this.fetchStats(owner, repo);
121132
updateStats();
@@ -126,9 +137,11 @@ const GitHubStats = {
126137
// 代码复制模块
127138
const CodeCopy = {
128139
copy(button) {
129-
const { find, toggleClass } = DOMUtils;
140+
const {find, toggleClass} = DOMUtils;
130141
const codeBlock = find('code', button.closest('div'));
131-
if (!codeBlock) return;
142+
if (!codeBlock) {
143+
return;
144+
}
132145

133146
navigator.clipboard.writeText(codeBlock.textContent)
134147
.then(() => {
@@ -153,7 +166,9 @@ const Header = {
153166
observer: null,
154167

155168
initObserver() {
156-
if (this.observer) return;
169+
if (this.observer) {
170+
return;
171+
}
157172

158173
this.observer = new MutationObserver(mutations => {
159174
mutations.forEach(mutation => {
@@ -180,7 +195,8 @@ const Header = {
180195
if (isDark) {
181196
html.classList.add('dark');
182197
html.setAttribute('data-theme', 'dark');
183-
} else {
198+
}
199+
else {
184200
html.classList.remove('dark');
185201
html.setAttribute('data-theme', 'light');
186202
}
@@ -191,7 +207,8 @@ const Header = {
191207
if (e.matches) {
192208
html.classList.add('dark');
193209
html.setAttribute('data-theme', 'dark');
194-
} else {
210+
}
211+
else {
195212
html.classList.remove('dark');
196213
html.setAttribute('data-theme', 'light');
197214
}
@@ -275,16 +292,67 @@ const Header = {
275292
}
276293
};
277294

295+
// 字体大小控制
296+
const FontSizeControl = {
297+
init() {
298+
const proseContent = document.querySelectorAll('.prose *:not(h1):not(.text-3xl)');
299+
if (!proseContent.length) {
300+
return;
301+
}
302+
303+
const sizes = ['text-sm', 'text-base', 'text-lg', 'text-xl'];
304+
let currentSizeIndex = 1;
305+
306+
const savedSize = localStorage.getItem('content-font-size');
307+
if (savedSize) {
308+
proseContent.forEach(el => {
309+
el.classList.remove(...sizes);
310+
el.classList.add(savedSize);
311+
});
312+
currentSizeIndex = sizes.indexOf(savedSize);
313+
}
314+
315+
const updateFontSize = (newIndex) => {
316+
if (newIndex >= 0 && newIndex < sizes.length) {
317+
proseContent.forEach(el => {
318+
el.classList.remove(...sizes);
319+
el.classList.add(sizes[newIndex]);
320+
});
321+
localStorage.setItem('content-font-size', sizes[newIndex]);
322+
currentSizeIndex = newIndex;
323+
}
324+
};
325+
326+
document.getElementById('increase-font')?.addEventListener('click', () => {
327+
updateFontSize(currentSizeIndex + 1);
328+
});
329+
330+
document.getElementById('decrease-font')?.addEventListener('click', () => {
331+
updateFontSize(currentSizeIndex - 1);
332+
});
333+
334+
document.getElementById('reset-font')?.addEventListener('click', () => {
335+
updateFontSize(1);
336+
});
337+
}
338+
};
339+
278340
// 暴露到全局
279341
window.PageForge = {
280342
GitHubStats,
281343
CodeCopy,
282-
Header
344+
Header,
345+
FontSizeControl
283346
};
284347

285348
// DOM 加载完成后初始化
286349
if (document.readyState === 'loading') {
287-
document.addEventListener('DOMContentLoaded', () => Header.init());
288-
} else {
350+
document.addEventListener('DOMContentLoaded', () => {
351+
Header.init();
352+
FontSizeControl.init();
353+
});
354+
}
355+
else {
289356
Header.init();
357+
FontSizeControl.init();
290358
}

templates/includes/page-font.ejs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<% if (locals.siteData.feature?.fontSize?.enable) { %>
2+
<div class="flex items-center gap-2 p-0.5 bg-white dark:bg-gray-800 rounded-lg dark:border-gray-700">
3+
<button id="decrease-font" class="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded" title="减小字体">
4+
<svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
5+
<path fill-rule="evenodd" d="M5 10a1 1 0 011-1h8a1 1 0 110 2H6a1 1 0 01-1-1z" clip-rule="evenodd"/>
6+
</svg>
7+
</button>
8+
<button id="reset-font" class="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded text-sm" title="重置字体">A</button>
9+
<button id="increase-font" class="p-1 hover:bg-gray-100 dark:hover:bg-gray-700 rounded" title="增大字体">
10+
<svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
11+
<path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd"/>
12+
</svg>
13+
</button>
14+
</div>
15+
<% } %>

templates/includes/page-title.ejs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
</h1>
66
77
<div class="flex items-center gap-4">
8+
<!-- 字体大小控制按钮 -->
9+
<%- include('./page-font') %>
10+
811
<!-- 分享按钮 -->
912
<%- include('./page-share') %>
1013

templates/layouts/content.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</div>
99

1010
<!-- 主要内容部分 -->
11-
<div class="prose prose-gray dark:prose-invert max-w-none">
11+
<div class="prose prose-gray dark:prose-invert max-w-none text-base">
1212
<%- pageData.content %>
1313
</div>
1414

0 commit comments

Comments
 (0)