Skip to content

Commit d10a56b

Browse files
committed
支持页面分享功能 (close #17)
1 parent 0abd234 commit d10a56b

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

docs/content/setup/share.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: 分享设置
3+
icon: share
4+
---
5+
6+
PageForge 的分享设置包含了一些用于在网站上分享文档的选项。您可以在文档的顶部设置是否启用分享,以及选择要分享的平台。
7+
8+
PageForge 的分享功能是使用 [social-share.js](https://github.com/overtrue/share.js "social-share.js" "_blank") 进行实现的。
9+
10+
## 启用分享
11+
12+
---
13+
14+
```yaml
15+
feature:
16+
share:
17+
enable: true
18+
```
19+
20+
- `enable`: 是否启用分享
21+
22+
## 分享设置
23+
24+
---
25+
26+
可以配置分享平台的选项,可以参考它来设置怎么分享
27+
28+
```yaml
29+
feature:
30+
share:
31+
enable: true
32+
options:
33+
facebook: true
34+
weibo: true
35+
linkedin: true
36+
```
37+
38+
支持的分享平台可参考 [social-share.js](https://github.com/overtrue/share.js "social-share.js" "_blank")
39+
40+
## 配置 CDN
41+
42+
---
43+
44+
```yaml
45+
feature:
46+
share:
47+
enable: true
48+
cdn: https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/
49+
```
50+
51+
- `cdn`: CDN 链接,可以根据自己的需求进行配置,这里要填写完整的 CDN 链接,例如 `https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16`,这里要填写前面部分,会自动拼接后面的 js 和 css。

docs/pageforge.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ feature:
4444
enable: true
4545
button:
4646
enable: true
47+
share:
48+
enable: true
49+
options:
50+
qq: true
51+
wechat: true
52+
weibo: true
4753

4854
i18n:
4955
default: zh-CN
@@ -118,6 +124,7 @@ nav:
118124
- /setup/feature
119125
- /setup/i18n
120126
- /setup/compress
127+
- /setup/share
121128
- InlineTemplate:
122129
- /setup/template/home
123130
- /setup/template/enterprise

templates/includes/page-share.ejs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<% if (locals.siteData.feature?.share?.enable) { %>
2+
<button type="button" id="share-btn" class="text-gray-400 hover:text-blue-700 dark:text-gray-400 dark:hover:text-gray-300">
3+
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
4+
stroke-linejoin="round" class="w-4 h-4">
5+
<path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8"/>
6+
<polyline points="16 6 12 2 8 6"/>
7+
<line x1="12" y1="2" x2="12" y2="15"/>
8+
</svg>
9+
</button>
10+
11+
<div id="share-popup" class="hidden fixed inset-0 z-50" onclick="handleClickOutside(event)">
12+
<div class="absolute inset-0 bg-gray-500 bg-opacity-75 dark:bg-gray-900 dark:bg-opacity-75"></div>
13+
<div class="relative z-10 flex items-end sm:items-center justify-center min-h-full p-4 text-center sm:p-0">
14+
<div class="relative transform overflow-visible rounded-lg bg-white dark:bg-gray-800 px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 w-fit">
15+
<div class="social-share text-center"
16+
data-sites="<%= Object.entries(locals.siteData.feature?.share?.options || {})
17+
.filter(([_, enabled]) => enabled)
18+
.map(([platform]) => platform)
19+
.join(',') %>"
20+
data-mobile-sites="<%= Object.entries(locals.siteData.feature?.share?.options || {})
21+
.filter(([_, enabled]) => enabled)
22+
.map(([platform]) => platform)
23+
.join(',') %>"
24+
data-wechat-qrcode-title="微信扫一扫:分享"
25+
data-wechat-qrcode-helper="微信扫一扫后点击右上角分享">
26+
</div>
27+
</div>
28+
</div>
29+
</div>
30+
31+
<link rel="stylesheet" href="<%= locals.siteData.feature?.share?.cdn || 'https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16' %>/css/share.min.css">
32+
<script src="<%= locals.siteData.feature?.share?.cdn || 'https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16' %>/js/social-share.min.js"></script>
33+
34+
<style>
35+
/* 修复微信二维码显示被遮挡的问题 */
36+
.social-share .wechat-qrcode {
37+
z-index: 9999;
38+
width: fit-content;
39+
}
40+
</style>
41+
42+
<script>
43+
document.getElementById('share-btn')?.addEventListener('click', function () {
44+
document.getElementById('share-popup')?.classList.remove('hidden');
45+
document.body.style.overflow = 'hidden';
46+
});
47+
48+
function handleClickOutside(event) {
49+
const sharePopup = document.getElementById('share-popup');
50+
const shareContent = sharePopup?.querySelector('.transform');
51+
52+
// 如果点击的是弹窗内容之外的区域
53+
if (event.target === sharePopup || !shareContent?.contains(event.target)) {
54+
hideSharePopup();
55+
}
56+
}
57+
58+
function hideSharePopup() {
59+
document.getElementById('share-popup')?.classList.add('hidden');
60+
document.body.style.overflow = '';
61+
}
62+
63+
// 处理 ESC 键关闭
64+
document.addEventListener('keydown', function (e) {
65+
if (e.key === 'Escape') {
66+
hideSharePopup();
67+
}
68+
});
69+
70+
document.addEventListener('DOMContentLoaded', function () {
71+
// 初始化 social share
72+
socialShare('.social-share', {
73+
wechatQrcodeTitle: '微信扫一扫:分享',
74+
wechatQrcodeHelper: '微信扫一扫后点击右上角分享',
75+
url: window.location.href,
76+
source: document.title,
77+
wechat: function (elem, data) {
78+
// 自定义微信点击行为
79+
if (elem.classList.contains('icon-wechat')) {
80+
return false; // 阻止默认跳转
81+
}
82+
}
83+
});
84+
85+
// 防止微信图标点击跳转
86+
document.querySelector('.icon-wechat')?.addEventListener('click', function (e) {
87+
e.preventDefault();
88+
e.stopPropagation();
89+
return false;
90+
}, true);
91+
});
92+
</script>
93+
<% } %>

templates/includes/page-title.ejs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
</h1>
66
77
<div class="flex items-center gap-4">
8+
<!-- 分享按钮 -->
9+
<%- include('./page-share') %>
10+
811
<!-- 编辑源码 -->
912
<% if (locals.siteData.feature?.edit?.enable) { %>
1013
<a href="<%= `${locals.siteData?.repo?.url}/edit/${locals.siteData?.repo?.branch || 'main'}/docs/content/${pageData.relativePath}` %>"
1114
target="_blank"
1215
rel="noopener noreferrer"
13-
class="ml-4 text-gray-400 hover:text-blue-700 dark:text-gray-400 dark:hover:text-gray-300">
16+
class="text-gray-400 hover:text-blue-700 dark:text-gray-400 dark:hover:text-gray-300">
1417
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
1518
stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-edit w-4 h-4">
1619
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>

0 commit comments

Comments
 (0)