Skip to content

Commit 9cd4677

Browse files
committed
1
1 parent 90f11f3 commit 9cd4677

File tree

10 files changed

+903
-2
lines changed

10 files changed

+903
-2
lines changed

ENCRYPT_USAGE.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Front-matter 加密功能使用说明
2+
3+
## 功能说明
4+
5+
现在你可以在 MDX 文件的 front-matter 中添加 `password` 字段来自动加密页面内容。当用户访问页面时,需要输入正确的密码才能查看内容。
6+
7+
## 使用方法
8+
9+
### 1. 在 front-matter 中添加密码字段
10+
11+
在需要加密的 MDX 文件的 front-matter 中添加 `password` 字段:
12+
13+
```mdx
14+
---
15+
title: 加密文章示例
16+
password: "your-password-here"
17+
passwordSubtitle: "请输入密码以查看内容" # 可选
18+
---
19+
20+
这里是需要加密的内容。
21+
```
22+
23+
### 2. 使用 EncryptedContent 组件(推荐)
24+
25+
在 MDX 文件中,使用 `EncryptedContent` 组件来包装需要加密的内容:
26+
27+
```mdx
28+
---
29+
title: 加密文章示例
30+
password: "your-password-here"
31+
passwordSubtitle: "请输入密码以查看内容" # 可选
32+
---
33+
34+
import { getEntry } from 'astro:content';
35+
import EncryptedContent from '@components/EncryptedContent.astro';
36+
37+
const entry = await getEntry('docs', 'your-page-slug');
38+
39+
<EncryptedContent entry={entry}>
40+
# 这里是需要加密的内容
41+
42+
这些内容只有在输入正确密码后才能查看。
43+
</EncryptedContent>
44+
```
45+
46+
### 3. 直接使用 PasswordWrapper 组件
47+
48+
你也可以直接在 MDX 文件中使用 `PasswordWrapper` 组件:
49+
50+
```mdx
51+
---
52+
title: 加密文章示例
53+
---
54+
55+
import PasswordWrapper from '@components/PasswordWrapper.astro';
56+
57+
<PasswordWrapper password="your-password-here" passwordSubtitle="请输入密码以查看内容">
58+
# 这里是需要加密的内容
59+
60+
这些内容只有在输入正确密码后才能查看。
61+
</PasswordWrapper>
62+
```
63+
64+
## 功能特性
65+
66+
- ✅ 支持在 front-matter 中定义密码
67+
- ✅ 客户端加密/解密(使用 AES-CBC 算法)
68+
- ✅ 密码输入框支持显示/隐藏切换
69+
- ✅ 支持回车键快速提交
70+
- ✅ 自动保存密码到 localStorage(24小时有效期)
71+
- ✅ 密码错误提示
72+
- ✅ 响应式设计,适配移动端
73+
74+
## 注意事项
75+
76+
1. 密码存储在 front-matter 中,是明文的。如果需要更高的安全性,建议使用服务器端加密。
77+
2. 加密是在客户端进行的,有经验的用户仍然可以通过查看源代码或控制台获取加密内容。
78+
3. 这种方法适用于对安全性要求不高的场景,主要用于防止非技术用户直接查看内容。
79+
80+
## 文件结构
81+
82+
- `src/utils/encrypt.ts` - 加密工具函数
83+
- `src/components/Encrypt.astro` - 加密组件(核心加密逻辑)
84+
- `src/components/PasswordWrapper.astro` - 密码包装组件
85+
- `src/components/EncryptedContent.astro` - 从 front-matter 读取密码的组件
86+
- `src/styles/password.css` - 密码相关样式
87+
- `src/content.config.ts` - 扩展了 schema 以支持 password 字段
88+

astro.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default defineConfig({
2929
resolve: {
3030
alias: {
3131
'@components': new URL('./src/components', import.meta.url).pathname,
32+
'@': new URL('./src', import.meta.url).pathname,
3233
},
3334
},
3435
ssr: {
@@ -115,6 +116,7 @@ export default defineConfig({
115116
{ label: 'Astro-Starlight 简要配置', slug: 'astro-starlight/astro-starlight-example' },
116117
{ label: 'Astro-Starlight 扩展', slug: 'astro-starlight/starlight-writing-extensions'},
117118
{ label: 'Astro-Starlight 写作脚本', slug: 'astro-starlight/astro-starlight-scripts' },
119+
{ label: 'Astro-Starlight 加密内容示例', slug: 'astro-starlight/astro-starlight-encrypted' },
118120
],
119121
},
120122
{

0 commit comments

Comments
 (0)