@insapp-dev/inslog 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +26 -0
- package/README.md +66 -2
- package/config/database.example.json +5 -0
- package/config/locale.example.json +3 -0
- package/locales/en.json +476 -0
- package/locales/zh-CN.json +1 -0
- package/next-env.d.ts +7 -0
- package/next.config.ts +10 -0
- package/package.json +62 -4
- package/public/apple-touch-icon.png +0 -0
- package/public/favicon.ico +0 -0
- package/public/icon-color.png +0 -0
- package/public/icon-color.svg +5 -0
- package/public/icon-dark.svg +5 -0
- package/public/icon.svg +5 -0
- package/scripts/_db.mjs +67 -0
- package/scripts/export-backup.mjs +104 -0
- package/scripts/release.sh +70 -0
- package/scripts/seed-demo.mjs +184 -0
- package/scripts/test-connection.mjs +26 -0
- package/src/app/api/auth/logout/route.ts +9 -0
- package/src/app/api/auth/passkey/add-request/route.ts +35 -0
- package/src/app/api/auth/passkey/login/options/route.ts +40 -0
- package/src/app/api/auth/passkey/login/verify/route.ts +58 -0
- package/src/app/api/auth/passkey/register/options/route.ts +46 -0
- package/src/app/api/auth/passkey/register/verify/route.ts +63 -0
- package/src/app/api/auth/passkey/remove-request/route.ts +44 -0
- package/src/app/api/auth/recovery/route.ts +45 -0
- package/src/app/api/auth/totp/disable/route.ts +42 -0
- package/src/app/api/auth/totp/enable/route.ts +75 -0
- package/src/app/api/auth/totp/login/route.ts +47 -0
- package/src/app/api/auth/totp/setup/route.ts +20 -0
- package/src/app/api/setup/complete/route.ts +27 -0
- package/src/app/api/setup/email/route.ts +41 -0
- package/src/app/api/upload/local/route.ts +73 -0
- package/src/app/api/upload/route.ts +73 -0
- package/src/app/api/upload/s3/route.ts +73 -0
- package/src/app/confirm/[token]/page.tsx +181 -0
- package/src/app/globals.css +952 -0
- package/src/app/layout.tsx +107 -0
- package/src/app/login/login-form.tsx +325 -0
- package/src/app/login/page.tsx +76 -0
- package/src/app/not-found.tsx +26 -0
- package/src/app/page.tsx +179 -0
- package/src/app/posts/[slug]/page.tsx +100 -0
- package/src/app/robots.txt/route.ts +25 -0
- package/src/app/rss.xml/route.ts +60 -0
- package/src/app/search/page.tsx +94 -0
- package/src/app/settings/actions.ts +92 -0
- package/src/app/settings/image-field.tsx +143 -0
- package/src/app/settings/language-actions.ts +28 -0
- package/src/app/settings/language-select.tsx +56 -0
- package/src/app/settings/page.tsx +97 -0
- package/src/app/settings/passkey-manager.tsx +194 -0
- package/src/app/settings/site-form.tsx +323 -0
- package/src/app/settings/theme-toggle.tsx +51 -0
- package/src/app/settings/totp-manager.tsx +239 -0
- package/src/app/setup/auth-config-panel.tsx +132 -0
- package/src/app/setup/config-actions.ts +90 -0
- package/src/app/setup/page.tsx +80 -0
- package/src/app/setup/secrets-actions.ts +49 -0
- package/src/app/setup/setup-data.ts +133 -0
- package/src/app/setup/setup-wizard.tsx +338 -0
- package/src/app/sitemap.xml/route.ts +46 -0
- package/src/app/uploads/[...path]/route.ts +64 -0
- package/src/app/write/actions.ts +85 -0
- package/src/app/write/page.tsx +48 -0
- package/src/app/write/write-client.tsx +254 -0
- package/src/components/BlockEditor.tsx +102 -0
- package/src/components/PostBody.tsx +51 -0
- package/src/components/SiteFooter.tsx +41 -0
- package/src/components/WriteFab.tsx +38 -0
- package/src/components/i18n-provider.tsx +22 -0
- package/src/components/install-hint.tsx +31 -0
- package/src/components/service-config-fields.tsx +110 -0
- package/src/lib/auth.ts +41 -0
- package/src/lib/change-request.ts +184 -0
- package/src/lib/config-keys.ts +300 -0
- package/src/lib/credentials.ts +171 -0
- package/src/lib/env-config.ts +39 -0
- package/src/lib/env-store.ts +72 -0
- package/src/lib/i18n-core.ts +27 -0
- package/src/lib/i18n.ts +72 -0
- package/src/lib/image-compress.ts +59 -0
- package/src/lib/mail.ts +121 -0
- package/src/lib/mongodb.ts +236 -0
- package/src/lib/passkey.ts +73 -0
- package/src/lib/pending.ts +142 -0
- package/src/lib/post-utils.ts +56 -0
- package/src/lib/posts.ts +283 -0
- package/src/lib/rate-limit.ts +91 -0
- package/src/lib/recovery-hints.ts +16 -0
- package/src/lib/safe-path.ts +0 -0
- package/src/lib/secret-box.ts +46 -0
- package/src/lib/service-config.ts +25 -0
- package/src/lib/service-features.ts +72 -0
- package/src/lib/session-constants.ts +5 -0
- package/src/lib/session.ts +51 -0
- package/src/lib/settings-file.ts +120 -0
- package/src/lib/settings.ts +199 -0
- package/src/lib/site-url.ts +15 -0
- package/src/lib/slug.ts +53 -0
- package/src/lib/storage.ts +226 -0
- package/src/lib/time.ts +15 -0
- package/src/lib/totp.ts +88 -0
- package/src/lib/types.ts +72 -0
- package/src/lib/upload-client.ts +83 -0
- package/src/lib/xml.ts +12 -0
- package/src/proxy.ts +24 -0
- package/tsconfig.json +41 -0
package/src/app/page.tsx
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// 首页:站点信息 + 搜索 + 标签 + 文章时间线(全部来自数据库)
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
import { listPosts, listTags } from "@/lib/posts";
|
|
4
|
+
import { getSettings } from "@/lib/settings";
|
|
5
|
+
import { isLoggedIn } from "@/lib/auth";
|
|
6
|
+
import SiteFooter from "@/components/SiteFooter";
|
|
7
|
+
import InstallHint from "@/components/install-hint";
|
|
8
|
+
import { isDbConfigured } from "@/lib/mongodb";
|
|
9
|
+
import { formatDate, postDisplayDate, postFlags } from "@/lib/post-utils";
|
|
10
|
+
import { getT } from "@/lib/i18n";
|
|
11
|
+
|
|
12
|
+
// new 标签依赖当前时间,文章也随时会变,必须动态渲染
|
|
13
|
+
export const dynamic = "force-dynamic";
|
|
14
|
+
|
|
15
|
+
type Props = {
|
|
16
|
+
searchParams: Promise<{ page?: string; tag?: string }>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 首页的规范地址。
|
|
21
|
+
*
|
|
22
|
+
* 要跟实际 URL 完全一致(分页和标签都用查询参数),否则搜索引擎会以为
|
|
23
|
+
* 分页页、标签页都是首页的重复,权重就分散了。
|
|
24
|
+
* 分页链接和这个函数共用一份逻辑,不会写岔。
|
|
25
|
+
*/
|
|
26
|
+
function canonicalHref(page: number, tag?: string): string {
|
|
27
|
+
const params = new URLSearchParams();
|
|
28
|
+
if (tag) params.set("tag", tag);
|
|
29
|
+
if (page > 1) params.set("page", String(page));
|
|
30
|
+
const qs = params.toString();
|
|
31
|
+
return qs ? `/?${qs}` : "/";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 首页的元信息。
|
|
36
|
+
*
|
|
37
|
+
* 规范地址要区分三种情况,不然搜索引擎会以为分页页和标签页都是首页的重复:
|
|
38
|
+
* - 第 1 页且没标签:/ 就是它自己
|
|
39
|
+
* - 第 N 页:/?page=N
|
|
40
|
+
* - 带标签:/?tag=xxx(翻页时 /?tag=xxx&page=N)
|
|
41
|
+
*/
|
|
42
|
+
export async function generateMetadata({
|
|
43
|
+
searchParams,
|
|
44
|
+
}: Props): Promise<Metadata> {
|
|
45
|
+
const t = await getT();
|
|
46
|
+
const { page: pageParam, tag } = await searchParams;
|
|
47
|
+
const page = Math.max(1, Number(pageParam) || 1);
|
|
48
|
+
const url = canonicalHref(page, tag);
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
// 带标签时给「标签:xxx」,不带时**整个 title 字段都不写**,
|
|
52
|
+
// 让它继承 layout 里的标题(没设置标题时才用站点名)。
|
|
53
|
+
//
|
|
54
|
+
// 别写成 title: undefined——显式给 undefined 会被当成「本页明确不要标题」,
|
|
55
|
+
// 把继承来的 default 覆盖掉,结果 <title> 和 og:title 整个消失,
|
|
56
|
+
// 浏览器标签页只剩网址。
|
|
57
|
+
...(tag ? { title: t("标签:{tag}", { tag }) } : {}),
|
|
58
|
+
alternates: { canonical: url },
|
|
59
|
+
openGraph: { url },
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export default async function Home({ searchParams }: Props) {
|
|
64
|
+
const t = await getT();
|
|
65
|
+
const { page: pageParam, tag } = await searchParams;
|
|
66
|
+
const settings = await getSettings();
|
|
67
|
+
const loggedIn = await isLoggedIn();
|
|
68
|
+
const page = Math.max(1, Number(pageParam) || 1);
|
|
69
|
+
|
|
70
|
+
// 数据库还没配好(全新部署的第一步)时,给安装入口而不是 500
|
|
71
|
+
if (!(await isDbConfigured())) return <InstallHint />;
|
|
72
|
+
|
|
73
|
+
// 访客只看 public;登录后 private / draft 也出现在同一条时间线里,靠标题前的标签区分
|
|
74
|
+
const [{ items, total }, tags] = await Promise.all([
|
|
75
|
+
listPosts({
|
|
76
|
+
page,
|
|
77
|
+
limit: settings.postsPerPage,
|
|
78
|
+
tag,
|
|
79
|
+
includeHidden: loggedIn,
|
|
80
|
+
}),
|
|
81
|
+
listTags(),
|
|
82
|
+
]);
|
|
83
|
+
const totalPages = Math.max(1, Math.ceil(total / settings.postsPerPage));
|
|
84
|
+
|
|
85
|
+
// 和 canonical 用同一份逻辑,链接和搜索引擎看到的地址永远一致
|
|
86
|
+
const pageHref = (target: number) => canonicalHref(target, tag);
|
|
87
|
+
|
|
88
|
+
const socialLinks = settings.social.filter((link) => link.title && link.url);
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<main className="site mx-auto">
|
|
92
|
+
<header className="site-head">
|
|
93
|
+
<h1 className="site-title">
|
|
94
|
+
<a href="/">{settings.siteName}</a>
|
|
95
|
+
</h1>
|
|
96
|
+
{settings.description && <p className="site-desc">{settings.description}</p>}
|
|
97
|
+
</header>
|
|
98
|
+
|
|
99
|
+
<section className="section">
|
|
100
|
+
<form className="search-form d-flex" action="/search" method="get">
|
|
101
|
+
<input
|
|
102
|
+
className="search-input"
|
|
103
|
+
type="search"
|
|
104
|
+
name="q"
|
|
105
|
+
placeholder={t("搜索文章")}
|
|
106
|
+
aria-label={t("搜索文章")}
|
|
107
|
+
/>
|
|
108
|
+
<button className="btn btn-outline-light btn-plain" type="submit">
|
|
109
|
+
|
|
110
|
+
{t("搜索")}
|
|
111
|
+
</button>
|
|
112
|
+
</form>
|
|
113
|
+
{tags.length > 0 && (
|
|
114
|
+
<div className="tag-list d-flex flex-wrap">
|
|
115
|
+
{tags.map((name) => (
|
|
116
|
+
<a href={canonicalHref(1, name)} key={name}>
|
|
117
|
+
{name}
|
|
118
|
+
</a>
|
|
119
|
+
))}
|
|
120
|
+
</div>
|
|
121
|
+
)}
|
|
122
|
+
</section>
|
|
123
|
+
|
|
124
|
+
<section className="section">
|
|
125
|
+
<h2 className="section-title">{tag ? t("标签:{tag}", { tag }) : t("更新")}</h2>
|
|
126
|
+
{items.length === 0 ? (
|
|
127
|
+
<p className="notice">
|
|
128
|
+
{tag ? t("这个标签下还没有文章。") : t("还没有文章。")}
|
|
129
|
+
{tag && (
|
|
130
|
+
<>
|
|
131
|
+
{" "}
|
|
132
|
+
<a href="/">{t("查看全部")}</a>
|
|
133
|
+
</>
|
|
134
|
+
)}
|
|
135
|
+
</p>
|
|
136
|
+
) : (
|
|
137
|
+
<ul className="post-list list-unstyled m-0 p-0">
|
|
138
|
+
{items.map((post) => (
|
|
139
|
+
<li className="post-item d-flex align-items-baseline" key={post.slug}>
|
|
140
|
+
<time className="post-date flex-shrink-0" dateTime={formatDate(postDisplayDate(post))}>
|
|
141
|
+
{formatDate(postDisplayDate(post))}
|
|
142
|
+
</time>
|
|
143
|
+
<span className="post-title">
|
|
144
|
+
<a href={`/posts/${post.slug}`}>{post.title}</a>
|
|
145
|
+
{postFlags(post).map((flag) => (
|
|
146
|
+
<span className="badge post-badge" key={flag}>
|
|
147
|
+
{flag}
|
|
148
|
+
</span>
|
|
149
|
+
))}
|
|
150
|
+
</span>
|
|
151
|
+
</li>
|
|
152
|
+
))}
|
|
153
|
+
</ul>
|
|
154
|
+
)}
|
|
155
|
+
{totalPages > 1 && (
|
|
156
|
+
<nav className="pager d-flex align-items-baseline">
|
|
157
|
+
{page > 1 ? <a href={pageHref(page - 1)}>{t("上一页")}</a> : <span className="pager-off">{t("上一页")}</span>}
|
|
158
|
+
<span className="pager-index">
|
|
159
|
+
{page} / {totalPages}
|
|
160
|
+
</span>
|
|
161
|
+
{page < totalPages ? <a href={pageHref(page + 1)}>{t("下一页")}</a> : <span className="pager-off">{t("下一页")}</span>}
|
|
162
|
+
</nav>
|
|
163
|
+
)}
|
|
164
|
+
</section>
|
|
165
|
+
|
|
166
|
+
{socialLinks.length > 0 && (
|
|
167
|
+
<nav className="site-links d-flex flex-wrap">
|
|
168
|
+
{socialLinks.map((link) => (
|
|
169
|
+
<a href={link.url} key={`${link.title}-${link.url}`} rel="noopener noreferrer">
|
|
170
|
+
{link.title}
|
|
171
|
+
</a>
|
|
172
|
+
))}
|
|
173
|
+
</nav>
|
|
174
|
+
)}
|
|
175
|
+
|
|
176
|
+
<SiteFooter />
|
|
177
|
+
</main>
|
|
178
|
+
);
|
|
179
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// 文章详情页:按 slug 从数据库读取;非 public 的文章目前一律当不存在(登录鉴权接入后再放开)
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
import { notFound } from "next/navigation";
|
|
4
|
+
import PostBody from "@/components/PostBody";
|
|
5
|
+
import SiteFooter from "@/components/SiteFooter";
|
|
6
|
+
import InstallHint from "@/components/install-hint";
|
|
7
|
+
import { getPostBySlug } from "@/lib/posts";
|
|
8
|
+
import { isDbConfigured } from "@/lib/mongodb";
|
|
9
|
+
import { isLoggedIn } from "@/lib/auth";
|
|
10
|
+
import { formatDate, postDisplayDate } from "@/lib/post-utils";
|
|
11
|
+
import type { PostDoc } from "@/lib/types";
|
|
12
|
+
import { getT } from "@/lib/i18n";
|
|
13
|
+
|
|
14
|
+
export const dynamic = "force-dynamic";
|
|
15
|
+
|
|
16
|
+
type Props = {
|
|
17
|
+
params: Promise<{ slug: string }>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
|
21
|
+
const t = await getT();
|
|
22
|
+
const { slug } = await params;
|
|
23
|
+
// 与页面一致:私有/草稿对访客当作不存在,别把标题漏进 meta
|
|
24
|
+
// 数据库还没配好时(新部署)也别报错,标题给个默认的
|
|
25
|
+
let post: PostDoc | null = null;
|
|
26
|
+
try {
|
|
27
|
+
post = await getPostBySlug(slug, await isLoggedIn());
|
|
28
|
+
} catch {
|
|
29
|
+
post = null;
|
|
30
|
+
}
|
|
31
|
+
if (!post) return { title: t("文章不存在") };
|
|
32
|
+
|
|
33
|
+
const excerpt = post.plainText.replace(/\s+/g, " ").trim();
|
|
34
|
+
const description =
|
|
35
|
+
excerpt.length > 120 ? `${excerpt.slice(0, 120)}…` : excerpt;
|
|
36
|
+
const url = `/posts/${post.slug}`;
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
title: post.title,
|
|
40
|
+
description,
|
|
41
|
+
keywords: post.tags.length > 0 ? post.tags : undefined,
|
|
42
|
+
alternates: { canonical: url },
|
|
43
|
+
openGraph: {
|
|
44
|
+
type: "article",
|
|
45
|
+
title: post.title,
|
|
46
|
+
description,
|
|
47
|
+
url,
|
|
48
|
+
publishedTime: post.publishedAt?.toISOString(),
|
|
49
|
+
tags: post.tags,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default async function PostPage({ params }: Props) {
|
|
55
|
+
const t = await getT();
|
|
56
|
+
const { slug } = await params;
|
|
57
|
+
// 登录后私有/草稿也能打开;访客只认 public
|
|
58
|
+
const loggedIn = await isLoggedIn();
|
|
59
|
+
// 数据库还没配好时给安装入口
|
|
60
|
+
if (!(await isDbConfigured())) return <InstallHint />;
|
|
61
|
+
const post = await getPostBySlug(slug, loggedIn);
|
|
62
|
+
if (!post) notFound();
|
|
63
|
+
|
|
64
|
+
const date = formatDate(postDisplayDate(post));
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<main className="site mx-auto">
|
|
68
|
+
<article>
|
|
69
|
+
<header className="post-head">
|
|
70
|
+
<h1 className="post-head-title">{post.title}</h1>
|
|
71
|
+
<div className="post-meta-line d-flex flex-wrap align-items-baseline">
|
|
72
|
+
<time className="post-date" dateTime={date}>
|
|
73
|
+
{date}
|
|
74
|
+
</time>
|
|
75
|
+
<span className="post-meta-links d-flex">
|
|
76
|
+
<a href="/">{t("返回首页")}</a>
|
|
77
|
+
{post.visibility !== "public" && (
|
|
78
|
+
<span className="post-state">{post.visibility}</span>
|
|
79
|
+
)}
|
|
80
|
+
{loggedIn && <a href={`/write?slug=${post.slug}`}>{t("编辑")}</a>}
|
|
81
|
+
</span>
|
|
82
|
+
</div>
|
|
83
|
+
{post.tags.length > 0 && (
|
|
84
|
+
<div className="post-head-tags d-flex flex-wrap">
|
|
85
|
+
{post.tags.map((tag) => (
|
|
86
|
+
<a href={`/?tag=${encodeURIComponent(tag)}`} key={tag}>
|
|
87
|
+
{`#${tag}`}
|
|
88
|
+
</a>
|
|
89
|
+
))}
|
|
90
|
+
</div>
|
|
91
|
+
)}
|
|
92
|
+
</header>
|
|
93
|
+
|
|
94
|
+
<PostBody blocks={post.blocks} />
|
|
95
|
+
</article>
|
|
96
|
+
|
|
97
|
+
<SiteFooter />
|
|
98
|
+
</main>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { siteOrigin } from "@/lib/site-url";
|
|
2
|
+
|
|
3
|
+
export const dynamic = "force-dynamic";
|
|
4
|
+
|
|
5
|
+
export async function GET() {
|
|
6
|
+
const body = [
|
|
7
|
+
"User-agent: *",
|
|
8
|
+
"Allow: /",
|
|
9
|
+
// 写作、设置、确认页不该被索引
|
|
10
|
+
"Disallow: /write",
|
|
11
|
+
"Disallow: /settings",
|
|
12
|
+
"Disallow: /setup",
|
|
13
|
+
"Disallow: /confirm/",
|
|
14
|
+
"",
|
|
15
|
+
`Sitemap: ${siteOrigin()}/sitemap.xml`,
|
|
16
|
+
"",
|
|
17
|
+
].join("\n");
|
|
18
|
+
|
|
19
|
+
return new Response(body, {
|
|
20
|
+
headers: {
|
|
21
|
+
"content-type": "text/plain; charset=utf-8",
|
|
22
|
+
"cache-control": "public, max-age=3600",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// RSS 2.0:只输出 public 文章,最多 20 篇
|
|
2
|
+
import { currentLocale } from "@/lib/i18n";
|
|
3
|
+
import { isDbConfigured } from "@/lib/mongodb";
|
|
4
|
+
import { listPosts } from "@/lib/posts";
|
|
5
|
+
import { getSettings } from "@/lib/settings";
|
|
6
|
+
import { siteOrigin } from "@/lib/site-url";
|
|
7
|
+
import { xmlEscape } from "@/lib/xml";
|
|
8
|
+
|
|
9
|
+
export const dynamic = "force-dynamic";
|
|
10
|
+
|
|
11
|
+
export async function GET() {
|
|
12
|
+
// 数据库还没配好时(新部署)返回 503,而不是 500
|
|
13
|
+
if (!(await isDbConfigured())) {
|
|
14
|
+
return new Response("Site is not configured yet.", {
|
|
15
|
+
status: 503,
|
|
16
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
const [{ items }, settings] = await Promise.all([
|
|
20
|
+
listPosts({ limit: 20 }),
|
|
21
|
+
getSettings(),
|
|
22
|
+
]);
|
|
23
|
+
const origin = siteOrigin();
|
|
24
|
+
const title = settings.seoTitle || settings.siteName;
|
|
25
|
+
const description = settings.seoDescription || settings.description;
|
|
26
|
+
|
|
27
|
+
const itemsXml = items
|
|
28
|
+
.map((post) => {
|
|
29
|
+
const link = `${origin}/posts/${post.slug}`;
|
|
30
|
+
const date = post.publishedAt ?? post.createdAt;
|
|
31
|
+
const summary = post.plainText.slice(0, 200);
|
|
32
|
+
return ` <item>
|
|
33
|
+
<title>${xmlEscape(post.title)}</title>
|
|
34
|
+
<link>${xmlEscape(link)}</link>
|
|
35
|
+
<guid isPermaLink="true">${xmlEscape(link)}</guid>
|
|
36
|
+
<pubDate>${date.toUTCString()}</pubDate>
|
|
37
|
+
<description>${xmlEscape(summary)}</description>
|
|
38
|
+
</item>`;
|
|
39
|
+
})
|
|
40
|
+
.join("\n");
|
|
41
|
+
|
|
42
|
+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
43
|
+
<rss version="2.0">
|
|
44
|
+
<channel>
|
|
45
|
+
<title>${xmlEscape(title)}</title>
|
|
46
|
+
<link>${xmlEscape(origin)}</link>
|
|
47
|
+
<description>${xmlEscape(description)}</description>
|
|
48
|
+
<language>${await currentLocale()}</language>
|
|
49
|
+
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
|
|
50
|
+
${itemsXml}
|
|
51
|
+
</channel>
|
|
52
|
+
</rss>`;
|
|
53
|
+
|
|
54
|
+
return new Response(xml, {
|
|
55
|
+
headers: {
|
|
56
|
+
"content-type": "application/rss+xml; charset=utf-8",
|
|
57
|
+
"cache-control": "public, max-age=600",
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// 站内搜索:标题、正文、标签都匹配(数据层 listPosts 的 query)
|
|
2
|
+
import { isLoggedIn } from "@/lib/auth";
|
|
3
|
+
import { listPosts } from "@/lib/posts";
|
|
4
|
+
import SiteFooter from "@/components/SiteFooter";
|
|
5
|
+
import InstallHint from "@/components/install-hint";
|
|
6
|
+
import { isDbConfigured } from "@/lib/mongodb";
|
|
7
|
+
import { formatDate, postDisplayDate, postFlags } from "@/lib/post-utils";
|
|
8
|
+
import { getT } from "@/lib/i18n";
|
|
9
|
+
|
|
10
|
+
export const dynamic = "force-dynamic";
|
|
11
|
+
|
|
12
|
+
const MAX_RESULTS = 50;
|
|
13
|
+
|
|
14
|
+
type Props = {
|
|
15
|
+
searchParams: Promise<{ q?: string }>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default async function SearchPage({ searchParams }: Props) {
|
|
19
|
+
const t = await getT();
|
|
20
|
+
const { q } = await searchParams;
|
|
21
|
+
const query = (q ?? "").trim();
|
|
22
|
+
const loggedIn = await isLoggedIn();
|
|
23
|
+
// 数据库还没配好时给安装入口
|
|
24
|
+
if (!(await isDbConfigured())) return <InstallHint />;
|
|
25
|
+
const { items, total } = query
|
|
26
|
+
? await listPosts({ query, limit: MAX_RESULTS, includeHidden: loggedIn })
|
|
27
|
+
: { items: [], total: 0 };
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<main className="site mx-auto">
|
|
31
|
+
<header className="site-head">
|
|
32
|
+
<h1 className="site-title">
|
|
33
|
+
<a href="/">{t("搜索")}</a>
|
|
34
|
+
</h1>
|
|
35
|
+
</header>
|
|
36
|
+
|
|
37
|
+
<section className="section">
|
|
38
|
+
<form className="search-form d-flex" action="/search" method="get">
|
|
39
|
+
<input
|
|
40
|
+
className="search-input"
|
|
41
|
+
type="search"
|
|
42
|
+
name="q"
|
|
43
|
+
defaultValue={query}
|
|
44
|
+
placeholder={t("搜索文章")}
|
|
45
|
+
aria-label={t("搜索文章")}
|
|
46
|
+
/>
|
|
47
|
+
<button className="btn btn-outline-light btn-plain" type="submit">
|
|
48
|
+
|
|
49
|
+
{t("搜索")}
|
|
50
|
+
</button>
|
|
51
|
+
</form>
|
|
52
|
+
</section>
|
|
53
|
+
|
|
54
|
+
<section className="section">
|
|
55
|
+
<h2 className="section-title">
|
|
56
|
+
{query ? t("“{q}” 的结果", { q: query }) : t("输入关键词")}
|
|
57
|
+
</h2>
|
|
58
|
+
{!query ? (
|
|
59
|
+
<p className="notice">{t("搜标题、正文或标签都可以。")}</p>
|
|
60
|
+
) : items.length === 0 ? (
|
|
61
|
+
<p className="notice">{t("没有匹配的文章。")}</p>
|
|
62
|
+
) : (
|
|
63
|
+
<>
|
|
64
|
+
<ul className="post-list list-unstyled m-0 p-0">
|
|
65
|
+
{items.map((post) => (
|
|
66
|
+
<li className="post-item d-flex align-items-baseline" key={post.slug}>
|
|
67
|
+
<time
|
|
68
|
+
className="post-date flex-shrink-0"
|
|
69
|
+
dateTime={formatDate(postDisplayDate(post))}
|
|
70
|
+
>
|
|
71
|
+
{formatDate(postDisplayDate(post))}
|
|
72
|
+
</time>
|
|
73
|
+
<span className="post-title">
|
|
74
|
+
<a href={`/posts/${post.slug}`}>{post.title}</a>
|
|
75
|
+
{postFlags(post).map((flag) => (
|
|
76
|
+
<span className="badge post-badge" key={flag}>
|
|
77
|
+
{flag}
|
|
78
|
+
</span>
|
|
79
|
+
))}
|
|
80
|
+
</span>
|
|
81
|
+
</li>
|
|
82
|
+
))}
|
|
83
|
+
</ul>
|
|
84
|
+
{total > MAX_RESULTS && (
|
|
85
|
+
<p className="notice">{t("共")} {total} {t("篇,只显示前")} {MAX_RESULTS} {t("篇。")}</p>
|
|
86
|
+
)}
|
|
87
|
+
</>
|
|
88
|
+
)}
|
|
89
|
+
</section>
|
|
90
|
+
|
|
91
|
+
<SiteFooter />
|
|
92
|
+
</main>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
// 站点设置的保存:只处理站点展示信息,不碰任何数据库连接配置
|
|
4
|
+
import { isLoggedIn } from "@/lib/auth";
|
|
5
|
+
import { saveSettings } from "@/lib/settings";
|
|
6
|
+
import type { SocialLink } from "@/lib/types";
|
|
7
|
+
import { getT } from "@/lib/i18n";
|
|
8
|
+
|
|
9
|
+
export type SettingsState = { status: "idle" | "ok" | "error"; message: string };
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 保存站点设置。前端只提交改过的字段,没出现的字段原样不动。
|
|
13
|
+
* 登录校验在这里再做一遍(纵深防御),不依赖 proxy 的粗筛。
|
|
14
|
+
*/
|
|
15
|
+
export async function saveSiteSettings(
|
|
16
|
+
_prev: SettingsState,
|
|
17
|
+
form: FormData
|
|
18
|
+
): Promise<SettingsState> {
|
|
19
|
+
const t = await getT();
|
|
20
|
+
if (!(await isLoggedIn())) {
|
|
21
|
+
return { status: "error", message: t("登录已过期,请重新登录后再试") };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const has = (key: string) => form.has(key);
|
|
25
|
+
const value = (key: string) => String(form.get(key) ?? "").trim();
|
|
26
|
+
const changes: Record<string, unknown> = {};
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
if (has("siteName")) {
|
|
30
|
+
const siteName = value("siteName");
|
|
31
|
+
if (!siteName) return { status: "error", message: t("站点名不能为空") };
|
|
32
|
+
// 上限跟安装向导那边一致:两条保存路径规则不一样的话,
|
|
33
|
+
// 用户能存进一个安装向导不接受的站点名。
|
|
34
|
+
if (siteName.length > 80) {
|
|
35
|
+
return { status: "error", message: t("站点名太长(最多 80 字)") };
|
|
36
|
+
}
|
|
37
|
+
changes.siteName = siteName;
|
|
38
|
+
}
|
|
39
|
+
const textKeys = [
|
|
40
|
+
"description", "author", "footerText", "seoTitle",
|
|
41
|
+
"seoDescription", "seoKeywords", "notifyEmail", "favicon", "ogImage",
|
|
42
|
+
] as const;
|
|
43
|
+
for (const key of textKeys) {
|
|
44
|
+
if (!has(key)) continue;
|
|
45
|
+
const v = value(key);
|
|
46
|
+
// 通知邮箱格式跟安装向导那边一样校验,
|
|
47
|
+
// 不然能存进一个发不出信的地址,还没有任何提示。
|
|
48
|
+
if (key === "notifyEmail" && v && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v)) {
|
|
49
|
+
return { status: "error", message: t("通知邮箱格式不对") };
|
|
50
|
+
}
|
|
51
|
+
changes[key] = v;
|
|
52
|
+
}
|
|
53
|
+
if (has("postsPerPage")) {
|
|
54
|
+
const num = Number(value("postsPerPage"));
|
|
55
|
+
changes.postsPerPage = Number.isFinite(num)
|
|
56
|
+
? Math.min(50, Math.max(1, Math.round(num)))
|
|
57
|
+
: 20;
|
|
58
|
+
}
|
|
59
|
+
if (has("social")) {
|
|
60
|
+
let links: unknown;
|
|
61
|
+
try {
|
|
62
|
+
links = JSON.parse(value("social"));
|
|
63
|
+
} catch {
|
|
64
|
+
return { status: "error", message: t("社交链接格式不对") };
|
|
65
|
+
}
|
|
66
|
+
if (!Array.isArray(links)) {
|
|
67
|
+
return { status: "error", message: t("社交链接格式不对") };
|
|
68
|
+
}
|
|
69
|
+
const clean: SocialLink[] = links
|
|
70
|
+
.filter((item): item is Record<string, unknown> =>
|
|
71
|
+
Boolean(item && typeof item === "object")
|
|
72
|
+
)
|
|
73
|
+
.map((item) => ({
|
|
74
|
+
title: String(item.title ?? "").trim(),
|
|
75
|
+
url: String(item.url ?? "").trim(),
|
|
76
|
+
}))
|
|
77
|
+
.filter((item) => item.title && item.url);
|
|
78
|
+
changes.social = clean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (Object.keys(changes).length === 0) return { status: "idle", message: "" };
|
|
82
|
+
await saveSettings(changes);
|
|
83
|
+
return { status: "ok", message: t("已保存") };
|
|
84
|
+
} catch (error) {
|
|
85
|
+
// 后端可能是 MongoDB 也可能是配置文件,报错要说清是哪种
|
|
86
|
+
const message = error instanceof Error ? error.message : "";
|
|
87
|
+
return {
|
|
88
|
+
status: "error",
|
|
89
|
+
message: message || t("保存失败:数据库连接异常"),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|