Skip to content

Commit 0e7398d

Browse files
committed
fix: hide encrypted post summaries
1 parent 1d37d64 commit 0e7398d

14 files changed

Lines changed: 185 additions & 17 deletions

File tree

src/components/features/posts/PostCard.astro

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
2+
import { getPostHomeContent } from "@utils/post-card-content";
23
import { render } from "astro:content";
34
import { Icon } from "astro-icon/components";
45
@@ -22,6 +23,7 @@ const {
2223
category,
2324
image,
2425
description,
26+
hideHomeContent,
2527
pinned,
2628
encrypted,
2729
password,
@@ -32,6 +34,10 @@ const hasCover = image !== undefined && image !== null && image !== "";
3234
const coverWidth = "28%";
3335
3436
const { remarkPluginFrontmatter } = await render(entry);
37+
const homeContent = getPostHomeContent(
38+
{ description, hideHomeContent, password },
39+
remarkPluginFrontmatter.excerpt,
40+
);
3541
---
3642

3743
<div
@@ -106,7 +112,7 @@ const { remarkPluginFrontmatter } = await render(entry);
106112
{ "line-clamp-2 md:line-clamp-1": !description },
107113
]}
108114
>
109-
{description || remarkPluginFrontmatter.excerpt}
115+
{homeContent}
110116
</div>
111117

112118
<div class="flex flex-wrap gap-2 mt-2">

src/components/features/posts/PostListItem.astro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Icon } from "astro-icon/components";
33
44
import type { PostForList } from "@/utils/content-utils";
55
import { formatDateToYYYYMMDD } from "@/utils/date-utils";
6+
import { getPostPublicDescription } from "@/utils/post-card-content";
67
import { getPostUrlBySlug } from "@/utils/url-utils";
78
89
interface Props {
@@ -24,7 +25,10 @@ const {
2425
const displayDate =
2526
dateFormat === "iso"
2627
? post.data.published.toISOString().substring(0, 10)
27-
: post.data.description || formatDateToYYYYMMDD(post.data.published);
28+
: getPostPublicDescription(
29+
post.data,
30+
formatDateToYYYYMMDD(post.data.published),
31+
);
2832
---
2933

3034
<a

src/components/widgets/feed/FeedInfo.astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { i18n } from "@i18n/translation";
44
import MainGridLayout from "@layouts/MainGridLayout.astro";
55
import { getSortedPosts } from "@utils/content-utils";
66
import { formatDateToYYYYMMDD } from "@utils/date-utils";
7+
import { getPostPublicDescription } from "@utils/post-card-content";
78
import { Icon } from "astro-icon/components";
89
910
interface Props {
@@ -13,6 +14,8 @@ interface Props {
1314
const { type } = Astro.props;
1415
const posts = (await getSortedPosts()).filter((post) => !post.data.encrypted);
1516
const recentPosts = posts.slice(0, 6);
17+
const getDescription = (post: (typeof recentPosts)[number]) =>
18+
getPostPublicDescription(post.data);
1619
1720
const feedUrl = type === "rss" ? "rss.xml" : "atom.xml";
1821
const prefix = type === "rss" ? "rss" : "atom";
@@ -103,9 +106,9 @@ const t = (key: string) => i18n(key as I18nKey);
103106
{post.data.title}
104107
</a>
105108
</h3>
106-
{post.data.description && (
109+
{getDescription(post) && (
107110
<p class="text-75 mb-3 line-clamp-2">
108-
{post.data.description}
111+
{getDescription(post)}
109112
</p>
110113
)}
111114
<div class="flex items-center gap-4 text-sm text-60">

src/content.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const postsCollection = defineCollection({
2626
encrypted: z.boolean().optional().default(false),
2727
password: z.string().optional().default(""),
2828
passwordHint: z.string().optional().default(""),
29+
hideHomeContent: z.boolean().optional(),
2930

3031
/* Posts alias */
3132
alias: z.string().optional(),

src/content/posts/encrypted-post.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ encrypted: true
66
pinned: true
77
password: "123456"
88
passwordHint: "123456"
9+
hideHomeContent: true
910
alias: "encrypted-example"
1011
tags: ["Test", "Encryption"]
1112
category: "Technology"
@@ -48,6 +49,7 @@ draft: false
4849
| `encrypted` | Whether this post is password protected. |
4950
| `password` | The password to unlock the encrypted post. |
5051
| `passwordHint`| A hint to help users remember the password. Displayed below the password input. |
52+
| `hideHomeContent` | Whether to hide public post summaries, including the home page, meta tags, feed/API summaries, and share previews. Defaults to `true` when `password` is set. |
5153

5254
## Where to Place the Post Files
5355

@@ -112,6 +114,7 @@ published: 2024-01-15
112114
encrypted: true
113115
password: "my-secret-password"
114116
passwordHint: "Hint: The password is my dog's name"
117+
hideHomeContent: true
115118
---
116119
```
117120

@@ -122,6 +125,7 @@ passwordHint: "Hint: The password is my dog's name"
122125
| `encrypted` | Yes | Set to `true` to enable password protection |
123126
| `password` | Yes | The password to unlock the post |
124127
| `passwordHint` | No | A hint displayed below the password input to help users |
128+
| `hideHomeContent` | No | Hide public summaries as `该文章已加密`. Defaults to `true` when `password` is set. Set to `false` to show the normal summary. |
125129

126130
### How the Unlock Box Looks
127131

src/pages/[...permalink].astro

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
hasCustomPermalink,
2626
initPostIdMap,
2727
} from "@utils/permalink-utils";
28+
import { getPostPublicDescription } from "@utils/post-card-content";
2829
import { getFileDirFromPath } from "@utils/url-utils";
2930
import type { CollectionEntry } from "astro:content";
3031
import { render } from "astro:content";
@@ -82,6 +83,10 @@ const { remarkPluginFrontmatter } = await render(entry);
8283
// 处理加密逻辑
8384
let encryptedContent = "";
8485
const isEncrypted = entry.data.encrypted && entry.data.password;
86+
const publicDescription = getPostPublicDescription(
87+
entry.data,
88+
entry.data.title,
89+
);
8590
8691
if (isEncrypted) {
8792
const contentToEncrypt = "MIZUKI-VERIFY:" + entry.body;
@@ -95,7 +100,7 @@ const jsonLd = {
95100
"@context": "https://schema.org",
96101
"@type": "BlogPosting",
97102
headline: entry.data.title,
98-
description: entry.data.description || entry.data.title,
103+
description: publicDescription,
99104
keywords: entry.data.tags,
100105
author: {
101106
"@type": "Person",
@@ -112,7 +117,7 @@ const jsonLd = {
112117
<MainGridLayout
113118
banner={entry.data.image}
114119
title={entry.data.title}
115-
description={entry.data.description}
120+
description={publicDescription}
116121
lang={entry.data.lang}
117122
setOGTypeArticle={true}
118123
postSlug={entry.id}

src/pages/api/allPostMeta.json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getSortedPosts } from "@/utils/content-utils";
2+
import { getPostPublicDescription } from "@/utils/post-card-content";
23

34
export async function GET() {
45
const posts = await getSortedPosts();
@@ -7,7 +8,7 @@ export async function GET() {
78
.map((post) => ({
89
id: post.id,
910
title: post.data.title,
10-
description: post.data.description,
11+
description: getPostPublicDescription(post.data),
1112
published: post.data.published.getTime(),
1213
category: post.data.category || "",
1314
password: !!post.data.password,

src/pages/archive.astro

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ const showCategoryBar = siteConfig.postListLayout.categoryBar?.enable ?? false;
1414
1515
// 1. 修复类型兼容性:将 category 的 null 转换为 undefined
1616
const posts = sortedPostsList.map((post) => ({
17-
...post,
17+
id: post.id,
18+
url: post.url,
1819
data: {
19-
...post.data,
20+
title: post.data.title,
21+
tags: post.data.tags,
2022
category: post.data.category === null ? undefined : post.data.category,
23+
published: post.data.published,
24+
alias: post.data.alias,
25+
permalink: post.data.permalink,
2126
},
2227
}));
2328

src/pages/atom.xml.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import sanitizeHtml from "sanitize-html";
88
import { profileConfig, siteConfig } from "@/config";
99
import { getSortedPosts } from "@/utils/content-utils";
1010
import { initPostIdMap } from "@/utils/permalink-utils";
11+
import { getPostPublicDescription } from "@/utils/post-card-content";
1112
import { getPostUrl } from "@/utils/url-utils";
1213

1314
const markdownParser = new MarkdownIt();
@@ -134,7 +135,7 @@ export async function GET(context: APIContext) {
134135
<id>${postUrl}</id>
135136
<published>${post.data.published.toISOString()}</published>
136137
<updated>${post.data.updated?.toISOString() || post.data.published.toISOString()}</updated>
137-
<summary>${post.data.description || ""}</summary>
138+
<summary>${getPostPublicDescription(post.data)}</summary>
138139
<content type="html"><![CDATA[${content}]]></content>
139140
<author>
140141
<name>${profileConfig.name}</name>

src/pages/og/[...slug].png.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getCollection } from "astro:content";
66
import satori from "satori";
77
import sharp from "sharp";
88

9+
import { getPostPublicDescription } from "@/utils/post-card-content";
910
import { removeFileExtension } from "@/utils/url-utils";
1011

1112
import { profileConfig, siteConfig } from "../../config";
@@ -136,7 +137,7 @@ export async function GET({
136137
day: "numeric",
137138
});
138139

139-
const description = post.data.description;
140+
const description = getPostPublicDescription(post.data);
140141

141142
const template = {
142143
type: "div",

0 commit comments

Comments
 (0)