@horizonwing/engine 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/README.md +32 -2
- package/drizzle/pg/schema.ts +100 -0
- package/drizzle/postgres/0000_smooth_princess_powerful.sql +34 -0
- package/drizzle/postgres/0001_colossal_morbius.sql +50 -0
- package/drizzle/postgres/README.md +23 -0
- package/drizzle/postgres/meta/0000_snapshot.json +244 -0
- package/drizzle/postgres/meta/0001_snapshot.json +545 -0
- package/drizzle/postgres/meta/_journal.json +20 -0
- package/drizzle/schema.ts +128 -0
- package/drizzle/sqlite/0000_cooing_jackpot.sql +34 -0
- package/drizzle/sqlite/0001_living_ben_urich.sql +49 -0
- package/drizzle/sqlite/meta/0000_snapshot.json +231 -0
- package/drizzle/sqlite/meta/0001_snapshot.json +554 -0
- package/drizzle/sqlite/meta/_journal.json +20 -0
- package/package.json +79 -4
- package/src/account-pages.tsx +151 -0
- package/src/auth.tsx +264 -0
- package/src/cache.ts +199 -0
- package/src/content.ts +448 -0
- package/src/db.ts +107 -0
- package/src/feeds.ts +376 -0
- package/src/guard.ts +126 -0
- package/src/html.ts +20 -0
- package/src/i18n.ts +304 -0
- package/src/index.ts +27 -0
- package/src/md.ts +278 -0
- package/src/media.ts +70 -0
- package/src/password.ts +59 -0
- package/src/platform.node.ts +75 -0
- package/src/platform.postgres.ts +61 -0
- package/src/platform.shared.ts +114 -0
- package/src/platform.ts +24 -0
- package/src/platform.workers.ts +94 -0
- package/src/ratelimit.ts +144 -0
- package/src/section.ts +123 -0
- package/src/sections/blog/index.tsx +92 -0
- package/src/sections/blog/schema.ts +12 -0
- package/src/sections/blog/views/pages.tsx +166 -0
- package/src/sections/docs/index.tsx +66 -0
- package/src/sections/docs/schema.ts +16 -0
- package/src/sections/docs/views/pages.tsx +180 -0
- package/src/sections/landing/index.tsx +41 -0
- package/src/sections/landing/views/pages.tsx +217 -0
- package/src/seo.ts +193 -0
- package/src/server.ts +122 -0
- package/src/services/content.ts +342 -0
- package/src/turnstile.ts +62 -0
- package/src/types.ts +192 -0
- package/src/views/error.tsx +35 -0
- package/src/views/index.tsx +12 -0
- package/src/views/layout.tsx +88 -0
- package/themes/warm/tokens.css +119 -0
package/src/section.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Hono } from 'hono';
|
|
2
|
+
import type { ZodType } from 'zod';
|
|
3
|
+
import type { Frontmatter } from './content';
|
|
4
|
+
import type { EngineContext } from './types';
|
|
5
|
+
|
|
6
|
+
export type { EngineContext };
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Section 协议(总设计 §6):栏目即插件。
|
|
10
|
+
*
|
|
11
|
+
* 一个 section = 路由挂载 + frontmatter schema + SEO 声明(+ 可选构建钩子)。
|
|
12
|
+
* 站点层 site.config.ts 以「一行一个工厂调用」启用:
|
|
13
|
+
*
|
|
14
|
+
* sections: [createBlogSection({ perPage: 12 }), createDocsSection(), createLandingSection()]
|
|
15
|
+
*
|
|
16
|
+
* 引擎 createApp 只经 registerSections 挂载;引擎自身不再保留任何内置内容路由。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/* ------------------------------------------------------------------ SEO 声明 */
|
|
20
|
+
|
|
21
|
+
export interface SectionListPageSeo {
|
|
22
|
+
/** 栏目页路径(blog = /blog、landing = /);sitemap 用 */
|
|
23
|
+
path: string;
|
|
24
|
+
/** 栏目页标题(sitemap / llms.txt 展示名) */
|
|
25
|
+
title: string;
|
|
26
|
+
changefreq: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';
|
|
27
|
+
priority: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SeoDeclaration {
|
|
31
|
+
listPage: SectionListPageSeo;
|
|
32
|
+
/** 栏目条目是否进 RSS/Atom(blog true;docs/landing 等参考型栏目 false) */
|
|
33
|
+
inFeeds?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* ------------------------------------------------------------------ Section */
|
|
37
|
+
|
|
38
|
+
export interface SectionHooks {
|
|
39
|
+
/** 同步/构建期:栏目全部条目(repo 解析产物),供静态派生物生成 */
|
|
40
|
+
onBuild?(items: unknown[]): Promise<void>;
|
|
41
|
+
/** 部署成功后(ship.mjs 调用;如 IndexNow 推送) */
|
|
42
|
+
onDeploy?(): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Section {
|
|
46
|
+
/** 栏目名(= content 表 section 列 + content/<name>/ 目录名) */
|
|
47
|
+
name: string;
|
|
48
|
+
/** 挂路由:app.get(`/${name}`...) 等;渲染依赖经 ctx.config(站点视图)取 */
|
|
49
|
+
routes(app: Hono, ctx: EngineContext): void;
|
|
50
|
+
seo: SeoDeclaration;
|
|
51
|
+
/** 栏目 frontmatter schema(同步管线按 section 名取用;shape 外的键进 meta) */
|
|
52
|
+
schema: ZodType<Frontmatter>;
|
|
53
|
+
hooks?: SectionHooks;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** defineSection:恒等包装,只为类型推导与可读性(协议入口) */
|
|
57
|
+
export function defineSection(s: Section): Section {
|
|
58
|
+
return s;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* ------------------------------------------------------------------ 注册 */
|
|
62
|
+
|
|
63
|
+
export interface SectionRegistration {
|
|
64
|
+
/** 已挂载栏目名(注册顺序) */
|
|
65
|
+
names: string[];
|
|
66
|
+
/** 收集到的 seo 声明(feeds/sitemap 消费) */
|
|
67
|
+
seo: SeoDeclaration[];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 按序注册 sections:挂路由 + 收集 seo 声明。
|
|
72
|
+
* 重复 name 或重复 listPage.path 视为配置错误,直接抛(fail-fast,M2.5 CLI 前的护栏)。
|
|
73
|
+
*/
|
|
74
|
+
export function registerSections(app: Hono, ctx: EngineContext, sections: Section[]): SectionRegistration {
|
|
75
|
+
const names: string[] = [];
|
|
76
|
+
const seo: SeoDeclaration[] = [];
|
|
77
|
+
const seenNames = new Set<string>();
|
|
78
|
+
const seenPaths = new Set<string>();
|
|
79
|
+
|
|
80
|
+
for (const section of sections) {
|
|
81
|
+
if (seenNames.has(section.name)) {
|
|
82
|
+
throw new Error(`section 重复注册:${section.name}`);
|
|
83
|
+
}
|
|
84
|
+
if (seenPaths.has(section.seo.listPage.path)) {
|
|
85
|
+
throw new Error(`section listPage.path 冲突:${section.seo.listPage.path}(${section.name})`);
|
|
86
|
+
}
|
|
87
|
+
seenNames.add(section.name);
|
|
88
|
+
seenPaths.add(section.seo.listPage.path);
|
|
89
|
+
section.routes(app, ctx);
|
|
90
|
+
names.push(section.name);
|
|
91
|
+
seo.push(section.seo);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return { names, seo };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** 某栏目是否认领了站点首页路径 '/'(server.ts 决定是否挂兜底首页用) */
|
|
98
|
+
export function homeClaimedBy(sections: Section[]): boolean {
|
|
99
|
+
return sections.some((s) => s.seo.listPage.path === '/');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/* ------------------------------------------------------------------ locale 变体挂载(M3) */
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 为全部 variant locale(locales[1..])各挂一个 <locale> 前缀子应用:
|
|
106
|
+
*
|
|
107
|
+
* app.route('/en', subApp) ← subApp 里 section.routes(sub, { ...ctx, locale: 'en' })
|
|
108
|
+
*
|
|
109
|
+
* 重用同一 section 实例的 routes(无状态,可安全多挂);ctx.locale 注入渲染语言,
|
|
110
|
+
* section 路由据此查询对应 locale 的内容并把 URL 前缀带上(redirect/canonical)。
|
|
111
|
+
* 单语言(i18n 关闭)不产生任何路由。
|
|
112
|
+
*/
|
|
113
|
+
export function mountLocaleVariants(app: Hono, ctx: EngineContext, sections: Section[], locales: string[]): void {
|
|
114
|
+
if (locales.length <= 1) return;
|
|
115
|
+
for (let i = 1; i < locales.length; i += 1) {
|
|
116
|
+
const locale = locales[i] as string;
|
|
117
|
+
const sub = new Hono();
|
|
118
|
+
for (const section of sections) {
|
|
119
|
+
section.routes(sub, { ...ctx, locale });
|
|
120
|
+
}
|
|
121
|
+
app.route(`/${locale}`, sub);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Hono } from 'hono';
|
|
2
|
+
import type { Context } from 'hono';
|
|
3
|
+
import { defineSection } from '../../section';
|
|
4
|
+
import type { EngineContext, Section } from '../../section';
|
|
5
|
+
import { getPublished, listPublished, resolveRedirect, toArticle } from '../../services/content';
|
|
6
|
+
import { notFoundHandler } from '../../guard';
|
|
7
|
+
import { siteLocales, baseLocaleOf, localePrefix } from '../../i18n';
|
|
8
|
+
import { blogSchema } from './schema';
|
|
9
|
+
import { BlogListPage, BlogArticlePage } from './views/pages';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* blog 栏目(总设计 §6 Section 协议):
|
|
13
|
+
*
|
|
14
|
+
* /blog 列表第 1 页(CollectionPage JSON-LD)
|
|
15
|
+
* /blog/page/:n 分页(n=1 → 301 回 /blog,避免重复内容)
|
|
16
|
+
* /blog/:slug 详情(Article/BlogPosting + BreadcrumbList);slug 变更
|
|
17
|
+
* 走 redirects 表 301 no-store;draft 永不出运行时读路径
|
|
18
|
+
* /en/blog/** M3 locale 变体:mountLocaleVariants 以 ctx.locale='en' 重挂同一路由
|
|
19
|
+
*
|
|
20
|
+
* 启用:site.config.ts `sections: [createBlogSection({ perPage: 12 }), ...]`。
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
function movedPermanently(c: Context, to: string): Response {
|
|
24
|
+
return new Response(null, {
|
|
25
|
+
status: 301,
|
|
26
|
+
headers: { location: to, 'cache-control': 'no-store' },
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function parsePage(raw: string | undefined): number | null {
|
|
31
|
+
if (!raw) return null;
|
|
32
|
+
const n = Number(raw);
|
|
33
|
+
if (!Number.isInteger(n) || n < 1) return null;
|
|
34
|
+
return n;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface BlogSectionOptions {
|
|
38
|
+
/** 列表分页大小,默认 12 */
|
|
39
|
+
perPage?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createBlogSection(options: BlogSectionOptions = {}): Section {
|
|
43
|
+
const perPage = options.perPage ?? 12;
|
|
44
|
+
|
|
45
|
+
return defineSection({
|
|
46
|
+
name: 'blog',
|
|
47
|
+
schema: blogSchema,
|
|
48
|
+
seo: {
|
|
49
|
+
listPage: { path: '/blog', title: 'Blog', changefreq: 'daily', priority: 0.9 },
|
|
50
|
+
inFeeds: true,
|
|
51
|
+
},
|
|
52
|
+
routes(app: Hono, ctx: EngineContext): void {
|
|
53
|
+
const config = ctx.config;
|
|
54
|
+
const db = ctx.platform.db;
|
|
55
|
+
const notFound = notFoundHandler(config);
|
|
56
|
+
const locales = siteLocales(config);
|
|
57
|
+
const locale = ctx.locale ?? baseLocaleOf(locales);
|
|
58
|
+
const prefix = localePrefix(locale, locales);
|
|
59
|
+
|
|
60
|
+
app.get('/blog', (c) =>
|
|
61
|
+
listPublished(db, 'blog', { page: 1, perPage, locale }).then((list) =>
|
|
62
|
+
c.html(<BlogListPage c={c} data={list} config={config} locale={locale} />),
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
app.get('/blog/page/:n', async (c) => {
|
|
67
|
+
const page = parsePage(c.req.param('n'));
|
|
68
|
+
if (page === null) return notFound(c);
|
|
69
|
+
if (page === 1) return movedPermanently(c, `${prefix}/blog`);
|
|
70
|
+
const list = await listPublished(db, 'blog', { page, perPage, locale });
|
|
71
|
+
if (page > list.pages) return notFound(c);
|
|
72
|
+
return c.html(<BlogListPage c={c} data={list} config={config} locale={locale} />);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
app.get('/blog/:slug', async (c) => {
|
|
76
|
+
const slug = c.req.param('slug');
|
|
77
|
+
const row = await getPublished(db, 'blog', slug, locale);
|
|
78
|
+
if (!row) {
|
|
79
|
+
// slug 变更:旧地址 301 到新地址(redirects 表,repo 同步产生;
|
|
80
|
+
// variant locale 的 from/to 都带 /<locale> 前缀)
|
|
81
|
+
const to = await resolveRedirect(db, `${prefix}/blog/${slug}`);
|
|
82
|
+
if (to) return movedPermanently(c, to);
|
|
83
|
+
return notFound(c);
|
|
84
|
+
}
|
|
85
|
+
const article = toArticle(row, locale);
|
|
86
|
+
const relatedPage = await listPublished(db, 'blog', { page: 1, perPage: perPage + 1, locale });
|
|
87
|
+
const related = relatedPage.items.filter((i) => i.slug !== slug).slice(0, perPage);
|
|
88
|
+
return c.html(<BlogArticlePage c={c} data={{ article, related }} config={config} locale={locale} />);
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { frontmatterSchema } from '../../content';
|
|
2
|
+
import type { Frontmatter } from '../../content';
|
|
3
|
+
import type { ZodType } from 'zod';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* blog 栏目 frontmatter schema(PRD:{title, description, date, updated?, tags?, draft?, author?})。
|
|
7
|
+
*
|
|
8
|
+
* 基础 schema 在 M2 前置小修中已把 date 定为必填(缺 date 会让 checksum 每次同步
|
|
9
|
+
* 都变化 → content_version 翻新 → 全站缓存键被隐形打翻);blog 的字段集与基础
|
|
10
|
+
* schema 完全一致,这里显式绑定一份,作为 Section 协议的栏目声明位。
|
|
11
|
+
*/
|
|
12
|
+
export const blogSchema: ZodType<Frontmatter> = frontmatterSchema;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { EngineConfig, ContentArticle, ContentListItem, PagedList } from '../../../types';
|
|
2
|
+
import { buildHead, articleJsonLd, breadcrumbJsonLd, collectionPageJsonLd, personJsonLd } from '../../../seo';
|
|
3
|
+
import { tr, localePrefix, hreflangAlternates, siteLocales, baseLocaleOf, localeSwitchProps } from '../../../i18n';
|
|
4
|
+
import { Layout } from '../../../views/layout';
|
|
5
|
+
import { raw } from 'hono/html';
|
|
6
|
+
import type { Context } from 'hono';
|
|
7
|
+
|
|
8
|
+
/** blog 视图(M1 模板样式,M2 起归位 sections/blog/views;M3 locale 变体 + paraglide 文案) */
|
|
9
|
+
|
|
10
|
+
/** 视图共用的语言切换器 props 构造(单语言返回 null 不渲染) */
|
|
11
|
+
function localeSwitchOf(c: Context, config: EngineConfig, locale: string) {
|
|
12
|
+
return localeSwitchProps(c, config, locale);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function fmtDate(d: Date, locale: string): string {
|
|
16
|
+
return new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'long', day: 'numeric' }).format(d);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function originOf(config: EngineConfig): string {
|
|
20
|
+
return config.site.url.replace(/\/+$/, '');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 列表/详情两页共用的 hreflang alternates(单语言返回 [],head 不输出) */
|
|
24
|
+
function alternatesOf(config: EngineConfig, path: string): { hreflang: string; href: string }[] {
|
|
25
|
+
const locales = siteLocales(config);
|
|
26
|
+
if (locales.length <= 1) return [];
|
|
27
|
+
return hreflangAlternates(originOf(config), path, locales);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* ------------------------------------------------------------ blog 列表 */
|
|
31
|
+
|
|
32
|
+
export function BlogListPage(props: { c: Context; data: PagedList<ContentListItem>; config: EngineConfig; locale?: string }) {
|
|
33
|
+
const { site } = props.config;
|
|
34
|
+
const locales = siteLocales(props.config);
|
|
35
|
+
const locale = props.locale ?? baseLocaleOf(locales);
|
|
36
|
+
const prefix = localePrefix(locale, locales);
|
|
37
|
+
const t = tr(locale);
|
|
38
|
+
const base = originOf(props.config);
|
|
39
|
+
const page = props.data.page;
|
|
40
|
+
const listPath = `${prefix}/blog`;
|
|
41
|
+
// M4(M3 遗留修正):分页页 canonical 自指(/blog/page/2 指自己,每页独立
|
|
42
|
+
// canonical,调研 §1.5);hreflang alternates 保持只算列表页 /blog
|
|
43
|
+
const canonicalPath = page > 1 ? `${listPath}/page/${page}` : listPath;
|
|
44
|
+
const head = buildHead({
|
|
45
|
+
title: `${t.blog_list_title()} - ${site.name}`,
|
|
46
|
+
description: `${t.blog_list_desc()}${page > 1 ? ` (${page}/${props.data.pages})` : ''}`,
|
|
47
|
+
canonical: `${base}${canonicalPath}`,
|
|
48
|
+
type: 'website',
|
|
49
|
+
locale,
|
|
50
|
+
hreflang: alternatesOf(props.config, '/blog'),
|
|
51
|
+
});
|
|
52
|
+
const jsonld = [
|
|
53
|
+
collectionPageJsonLd({
|
|
54
|
+
url: `${base}${canonicalPath}`,
|
|
55
|
+
name: `${t.blog_list_title()} - ${site.name}`,
|
|
56
|
+
description: t.blog_list_desc(),
|
|
57
|
+
items: props.data.items.map((i) => ({ name: i.title, url: `${base}${prefix}/blog/${i.slug}` })),
|
|
58
|
+
}),
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<Layout head={head} jsonld={jsonld} brand={site.name} lang={locale} localeSwitch={localeSwitchOf(props.c, props.config, locale)} footerNote={`${site.name} · ${t.footer_note()}`}>
|
|
63
|
+
<section class="section">
|
|
64
|
+
<div class="container">
|
|
65
|
+
<p class="hero-kicker">Blog</p>
|
|
66
|
+
<h1 class="hero-title">{t.blog_list_title()}</h1>
|
|
67
|
+
<ul class="post-list">
|
|
68
|
+
{props.data.items.map((item) => (
|
|
69
|
+
<li class="post-item">
|
|
70
|
+
<a class="post-link" href={`${prefix}/blog/${item.slug}`}>{item.title}</a>
|
|
71
|
+
<p class="post-desc">{item.description}</p>
|
|
72
|
+
<p class="post-meta">
|
|
73
|
+
<time datetime={item.publishedAt.toISOString()}>{fmtDate(item.publishedAt, locale)}</time>
|
|
74
|
+
{item.tags.length > 0 && <span> · {item.tags.join(' / ')}</span>}
|
|
75
|
+
</p>
|
|
76
|
+
</li>
|
|
77
|
+
))}
|
|
78
|
+
</ul>
|
|
79
|
+
{props.data.pages > 1 && (
|
|
80
|
+
<nav class="pagination" aria-label="pagination">
|
|
81
|
+
{page > 1 && <a class="button" href={`${prefix}/blog/page/${page - 1}`}>{t.prev_page()}</a>}
|
|
82
|
+
<span class="page-info">{t.page_info({ page, pages: props.data.pages })}</span>
|
|
83
|
+
{page < props.data.pages && <a class="button" href={`${prefix}/blog/page/${page + 1}`}>{t.next_page()}</a>}
|
|
84
|
+
</nav>
|
|
85
|
+
)}
|
|
86
|
+
</div>
|
|
87
|
+
</section>
|
|
88
|
+
</Layout>
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* ------------------------------------------------------------ blog 文章 */
|
|
93
|
+
|
|
94
|
+
export function BlogArticlePage(props: { c: Context; data: { article: ContentArticle; related: ContentListItem[] }; config: EngineConfig; locale?: string }) {
|
|
95
|
+
const { site } = props.config;
|
|
96
|
+
const locales = siteLocales(props.config);
|
|
97
|
+
const locale = props.locale ?? props.data.article.locale ?? baseLocaleOf(locales);
|
|
98
|
+
const prefix = localePrefix(locale, locales);
|
|
99
|
+
const t = tr(locale);
|
|
100
|
+
const { article, related } = props.data;
|
|
101
|
+
const base = originOf(props.config);
|
|
102
|
+
const url = `${base}${prefix}/blog/${article.slug}`;
|
|
103
|
+
const head = buildHead({
|
|
104
|
+
title: `${article.title} - ${site.name}`,
|
|
105
|
+
description: article.description,
|
|
106
|
+
canonical: url,
|
|
107
|
+
type: 'article',
|
|
108
|
+
locale: article.locale,
|
|
109
|
+
image: `${base}/favicon.svg`,
|
|
110
|
+
hreflang: alternatesOf(props.config, `/blog/${article.slug}`),
|
|
111
|
+
});
|
|
112
|
+
const jsonld = [
|
|
113
|
+
articleJsonLd({
|
|
114
|
+
url,
|
|
115
|
+
headline: article.title,
|
|
116
|
+
description: article.description,
|
|
117
|
+
datePublished: article.publishedAt,
|
|
118
|
+
dateModified: article.updatedAt,
|
|
119
|
+
author: { name: article.author ?? site.name, url: base },
|
|
120
|
+
image: `${base}/favicon.svg`,
|
|
121
|
+
section: article.section,
|
|
122
|
+
siteName: site.name,
|
|
123
|
+
}),
|
|
124
|
+
breadcrumbJsonLd([
|
|
125
|
+
{ name: site.name, url: `${base}${prefix}/` },
|
|
126
|
+
{ name: t.nav_blog(), url: `${base}${prefix}/blog` },
|
|
127
|
+
{ name: article.title, url },
|
|
128
|
+
]),
|
|
129
|
+
personJsonLd({ name: article.author, url: base }),
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<Layout head={head} jsonld={jsonld} brand={site.name} lang={locale} localeSwitch={localeSwitchOf(props.c, props.config, locale)} footerNote={`${site.name} · ${t.footer_note()}`}>
|
|
134
|
+
<article class="article">
|
|
135
|
+
<div class="container article-container">
|
|
136
|
+
<header class="article-header">
|
|
137
|
+
<h1 class="article-title">{article.title}</h1>
|
|
138
|
+
<p class="article-meta">
|
|
139
|
+
<time datetime={article.publishedAt.toISOString()}>{fmtDate(article.publishedAt, locale)}</time>
|
|
140
|
+
{article.author && <span> · {article.author}</span>}
|
|
141
|
+
{article.tags.length > 0 && <span> · {article.tags.join(' / ')}</span>}
|
|
142
|
+
</p>
|
|
143
|
+
</header>
|
|
144
|
+
{/* 正文由 md 管线 sanitize 后产出;chart 占位需 island 脚本 */}
|
|
145
|
+
<div class="article-body" dangerouslySetInnerHTML={{ __html: raw(article.html) }} />
|
|
146
|
+
{article.html.includes('data-chart') && <script type="module" src="/js/chart.js"></script>}
|
|
147
|
+
</div>
|
|
148
|
+
</article>
|
|
149
|
+
{related.length > 0 && (
|
|
150
|
+
<section class="section" aria-label={t.more_posts()}>
|
|
151
|
+
<div class="container">
|
|
152
|
+
<h2 class="feature-title">{t.more_posts()}</h2>
|
|
153
|
+
<ul class="post-list">
|
|
154
|
+
{related.map((item) => (
|
|
155
|
+
<li class="post-item">
|
|
156
|
+
<a class="post-link" href={`${prefix}/blog/${item.slug}`}>{item.title}</a>
|
|
157
|
+
<p class="post-meta"><time datetime={item.publishedAt.toISOString()}>{fmtDate(item.publishedAt, locale)}</time></p>
|
|
158
|
+
</li>
|
|
159
|
+
))}
|
|
160
|
+
</ul>
|
|
161
|
+
</div>
|
|
162
|
+
</section>
|
|
163
|
+
)}
|
|
164
|
+
</Layout>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Hono } from 'hono';
|
|
2
|
+
import { defineSection } from '../../section';
|
|
3
|
+
import type { EngineContext, Section } from '../../section';
|
|
4
|
+
import { getPublished, listSectionWithMeta, resolveRedirect, toArticle } from '../../services/content';
|
|
5
|
+
import { notFoundHandler } from '../../guard';
|
|
6
|
+
import { siteLocales, baseLocaleOf, localePrefix } from '../../i18n';
|
|
7
|
+
import { docsSchema } from './schema';
|
|
8
|
+
import { DocsListPage, DocsPage } from './views/pages';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* docs 栏目(总设计 §6 Section 协议):
|
|
12
|
+
*
|
|
13
|
+
* /docs 列表 + 侧栏树导航(meta.order 排序 / meta.group 分组)
|
|
14
|
+
* /docs/[[...path]] 文档页(嵌套 slug)+ 侧栏 + Article + BreadcrumbList
|
|
15
|
+
* /en/docs/** M3 locale 变体(mountLocaleVariants 重挂,ctx.locale 注入)
|
|
16
|
+
*
|
|
17
|
+
* 侧栏数据两页共用(buildDocsTree);slug 变更经 redirects 表 301 no-store。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export function createDocsSection(): Section {
|
|
21
|
+
return defineSection({
|
|
22
|
+
name: 'docs',
|
|
23
|
+
schema: docsSchema,
|
|
24
|
+
seo: {
|
|
25
|
+
listPage: { path: '/docs', title: 'Docs', changefreq: 'weekly', priority: 0.8 },
|
|
26
|
+
inFeeds: false,
|
|
27
|
+
},
|
|
28
|
+
routes(app: Hono, ctx: EngineContext): void {
|
|
29
|
+
const config = ctx.config;
|
|
30
|
+
const db = ctx.platform.db;
|
|
31
|
+
const notFound = notFoundHandler(config);
|
|
32
|
+
const locales = siteLocales(config);
|
|
33
|
+
const locale = ctx.locale ?? baseLocaleOf(locales);
|
|
34
|
+
const prefix = localePrefix(locale, locales);
|
|
35
|
+
|
|
36
|
+
const loadTree = () => listSectionWithMeta(db, 'docs', { locale });
|
|
37
|
+
|
|
38
|
+
app.get('/docs', (c) =>
|
|
39
|
+
loadTree().then((items) => c.html(<DocsListPage c={c} data={items} config={config} locale={locale} />)),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
app.get('/docs/:slug{.+}', async (c) => {
|
|
43
|
+
const slug = c.req.param('slug');
|
|
44
|
+
const row = await getPublished(db, 'docs', slug, locale);
|
|
45
|
+
if (!row) {
|
|
46
|
+
const to = await resolveRedirect(db, `${prefix}/docs/${slug}`);
|
|
47
|
+
if (to) return new Response(null, { status: 301, headers: { location: to, 'cache-control': 'no-store' } });
|
|
48
|
+
return notFound(c);
|
|
49
|
+
}
|
|
50
|
+
const article = toArticle(row, locale);
|
|
51
|
+
const base = config.site.url.replace(/\/+$/, '');
|
|
52
|
+
const trail = [
|
|
53
|
+
{ name: config.site.name, url: `${base}${prefix}/` },
|
|
54
|
+
{ name: 'Docs', url: `${base}${prefix}/docs` },
|
|
55
|
+
];
|
|
56
|
+
const parts = slug.split('/');
|
|
57
|
+
for (let i = 0; i < parts.length - 1; i += 1) {
|
|
58
|
+
trail.push({ name: parts[i] ?? '', url: `${base}${prefix}/docs/${parts.slice(0, i + 1).join('/')}` });
|
|
59
|
+
}
|
|
60
|
+
trail.push({ name: article.title, url: `${base}${prefix}/docs/${slug}` });
|
|
61
|
+
const tree = await loadTree();
|
|
62
|
+
return c.html(<DocsPage c={c} data={{ article, trail, tree }} config={config} locale={locale} />);
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { z, type ZodType } from 'zod';
|
|
2
|
+
import { frontmatterSchema } from '../../content';
|
|
3
|
+
import type { Frontmatter } from '../../content';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* docs 栏目 frontmatter schema(PRD:{title, description, order, group?})。
|
|
7
|
+
*
|
|
8
|
+
* order 必填:侧栏树导航的排序键;group 可选:侧栏分组标题。
|
|
9
|
+
* order/group 属于栏目扩展字段,随 meta JSON 进 content 表(shape 外键不进基础列)。
|
|
10
|
+
*/
|
|
11
|
+
export const docsSchema: ZodType<Frontmatter & { order: number; group?: string }> = frontmatterSchema.extend({
|
|
12
|
+
order: z.number().int().min(0).max(100000),
|
|
13
|
+
group: z.string().trim().min(1).max(60).optional(),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export type DocsFrontmatter = z.infer<typeof docsSchema>;
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { EngineConfig, ContentArticle, ContentItemWithMeta } from '../../../types';
|
|
2
|
+
import { buildHead, collectionPageJsonLd, breadcrumbJsonLd, articleJsonLd } from '../../../seo';
|
|
3
|
+
import { tr, localePrefix, hreflangAlternates, siteLocales, baseLocaleOf, localeSwitchProps } from '../../../i18n';
|
|
4
|
+
import { Layout } from '../../../views/layout';
|
|
5
|
+
import { raw } from 'hono/html';
|
|
6
|
+
import type { Context } from 'hono';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* docs 视图:侧栏树导航(order/group)+ 文档页。
|
|
10
|
+
* 侧栏在列表页与文档页共用(同一数据形状:分组后的条目列表)。
|
|
11
|
+
* M3:locale 变体(链接/标题/canonical 带 /<locale> 前缀,paraglide 文案)。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export interface DocsSidebarGroup {
|
|
15
|
+
/** 分组标题(meta.group;未分组条目归入 '' 组,标题显示「文档」) */
|
|
16
|
+
title: string;
|
|
17
|
+
items: ContentItemWithMeta[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 按 meta.order 升序(缺省排最后)、slug 次序稳定排序,并按 group 分组 */
|
|
21
|
+
export function buildDocsTree(items: ContentItemWithMeta[], defaultGroupTitle = '文档'): DocsSidebarGroup[] {
|
|
22
|
+
const sorted = [...items].sort((a, b) => {
|
|
23
|
+
const oa = typeof a.meta.order === 'number' ? a.meta.order : Number.MAX_SAFE_INTEGER;
|
|
24
|
+
const ob = typeof b.meta.order === 'number' ? b.meta.order : Number.MAX_SAFE_INTEGER;
|
|
25
|
+
return oa - ob || a.slug.localeCompare(b.slug);
|
|
26
|
+
});
|
|
27
|
+
const groups = new Map<string, ContentItemWithMeta[]>();
|
|
28
|
+
for (const item of sorted) {
|
|
29
|
+
const g = typeof item.meta.group === 'string' && item.meta.group.trim() ? item.meta.group.trim() : '';
|
|
30
|
+
const list = groups.get(g) ?? [];
|
|
31
|
+
list.push(item);
|
|
32
|
+
groups.set(g, list);
|
|
33
|
+
}
|
|
34
|
+
return [...groups.entries()].map(([title, groupItems]) => ({ title: title || defaultGroupTitle, items: groupItems }));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function Sidebar(props: { groups: DocsSidebarGroup[]; prefix: string; currentSlug?: string }) {
|
|
38
|
+
return (
|
|
39
|
+
<aside class="docs-sidebar" aria-label="文档目录">
|
|
40
|
+
{props.groups.map((group) => (
|
|
41
|
+
<div class="docs-sidebar-group">
|
|
42
|
+
<p class="docs-sidebar-title">{group.title}</p>
|
|
43
|
+
<ul class="docs-sidebar-list">
|
|
44
|
+
{group.items.map((item) => (
|
|
45
|
+
<li>
|
|
46
|
+
<a
|
|
47
|
+
class="docs-sidebar-link"
|
|
48
|
+
href={`${props.prefix}/docs/${item.slug}`}
|
|
49
|
+
aria-current={props.currentSlug === item.slug ? 'page' : undefined}
|
|
50
|
+
>
|
|
51
|
+
{item.title}
|
|
52
|
+
</a>
|
|
53
|
+
</li>
|
|
54
|
+
))}
|
|
55
|
+
</ul>
|
|
56
|
+
</div>
|
|
57
|
+
))}
|
|
58
|
+
</aside>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* ------------------------------------------------------------ docs 列表 */
|
|
63
|
+
|
|
64
|
+
export function DocsListPage(props: { c: Context; data: ContentItemWithMeta[]; config: EngineConfig; locale?: string }) {
|
|
65
|
+
const { site } = props.config;
|
|
66
|
+
const locales = siteLocales(props.config);
|
|
67
|
+
const locale = props.locale ?? baseLocaleOf(locales);
|
|
68
|
+
const prefix = localePrefix(locale, locales);
|
|
69
|
+
const t = tr(locale);
|
|
70
|
+
const base = props.config.site.url.replace(/\/+$/, '');
|
|
71
|
+
const groups = buildDocsTree(props.data, t.docs_default_group());
|
|
72
|
+
const flat = groups.flatMap((g) => g.items);
|
|
73
|
+
const head = buildHead({
|
|
74
|
+
title: `${t.nav_docs()} - ${site.name}`,
|
|
75
|
+
description: t.docs_list_desc(),
|
|
76
|
+
canonical: `${base}${prefix}/docs`,
|
|
77
|
+
type: 'website',
|
|
78
|
+
locale,
|
|
79
|
+
hreflang: locales.length > 1 ? hreflangAlternates(base, '/docs', locales) : [],
|
|
80
|
+
});
|
|
81
|
+
const jsonld = [
|
|
82
|
+
collectionPageJsonLd({
|
|
83
|
+
url: `${base}${prefix}/docs`,
|
|
84
|
+
name: `${t.nav_docs()} - ${site.name}`,
|
|
85
|
+
description: t.docs_list_desc(),
|
|
86
|
+
items: flat.map((i) => ({ name: i.title, url: `${base}${prefix}/docs/${i.slug}` })),
|
|
87
|
+
}),
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<Layout head={head} jsonld={jsonld} brand={site.name} lang={locale} localeSwitch={localeSwitchProps(props.c, props.config, locale)} footerNote={`${site.name} · ${t.footer_note()}`}>
|
|
92
|
+
<section class="section">
|
|
93
|
+
<div class="container">
|
|
94
|
+
<p class="hero-kicker">Docs</p>
|
|
95
|
+
<h1 class="hero-title">{t.nav_docs()}</h1>
|
|
96
|
+
<div class="docs-layout">
|
|
97
|
+
<Sidebar groups={groups} prefix={prefix} />
|
|
98
|
+
<div>
|
|
99
|
+
<ul class="post-list">
|
|
100
|
+
{flat.map((item) => (
|
|
101
|
+
<li class="post-item">
|
|
102
|
+
<a class="post-link" href={`${prefix}/docs/${item.slug}`}>{item.title}</a>
|
|
103
|
+
<p class="post-desc">{item.description}</p>
|
|
104
|
+
</li>
|
|
105
|
+
))}
|
|
106
|
+
</ul>
|
|
107
|
+
</div>
|
|
108
|
+
</div>
|
|
109
|
+
</div>
|
|
110
|
+
</section>
|
|
111
|
+
</Layout>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/* ------------------------------------------------------------ docs 文档页 */
|
|
116
|
+
|
|
117
|
+
export function DocsPage(props: {
|
|
118
|
+
c: Context;
|
|
119
|
+
data: { article: ContentArticle; trail: { name: string; url: string }[]; tree: ContentItemWithMeta[] };
|
|
120
|
+
config: EngineConfig;
|
|
121
|
+
locale?: string;
|
|
122
|
+
}) {
|
|
123
|
+
const { site } = props.config;
|
|
124
|
+
const locales = siteLocales(props.config);
|
|
125
|
+
const locale = props.locale ?? props.data.article.locale ?? baseLocaleOf(locales);
|
|
126
|
+
const prefix = localePrefix(locale, locales);
|
|
127
|
+
const t = tr(locale);
|
|
128
|
+
const { article, trail } = props.data;
|
|
129
|
+
const base = props.config.site.url.replace(/\/+$/, '');
|
|
130
|
+
const url = `${base}${prefix}/docs/${article.slug}`;
|
|
131
|
+
const head = buildHead({
|
|
132
|
+
title: `${article.title} - ${site.name}`,
|
|
133
|
+
description: article.description,
|
|
134
|
+
canonical: url,
|
|
135
|
+
type: 'article',
|
|
136
|
+
locale: article.locale,
|
|
137
|
+
hreflang: locales.length > 1 ? hreflangAlternates(base, `/docs/${article.slug}`, locales) : [],
|
|
138
|
+
});
|
|
139
|
+
const jsonld = [
|
|
140
|
+
articleJsonLd({
|
|
141
|
+
url,
|
|
142
|
+
headline: article.title,
|
|
143
|
+
description: article.description,
|
|
144
|
+
datePublished: article.publishedAt,
|
|
145
|
+
dateModified: article.updatedAt,
|
|
146
|
+
author: { name: article.author ?? site.name },
|
|
147
|
+
siteName: site.name,
|
|
148
|
+
}),
|
|
149
|
+
breadcrumbJsonLd(trail),
|
|
150
|
+
];
|
|
151
|
+
|
|
152
|
+
return (
|
|
153
|
+
<Layout head={head} jsonld={jsonld} brand={site.name} lang={locale} localeSwitch={localeSwitchProps(props.c, props.config, locale)} footerNote={`${site.name} · ${t.footer_note()}`}>
|
|
154
|
+
<section class="section">
|
|
155
|
+
<div class="container">
|
|
156
|
+
<div class="docs-layout">
|
|
157
|
+
<Sidebar groups={buildDocsTree(props.data.tree, t.docs_default_group())} prefix={prefix} currentSlug={article.slug} />
|
|
158
|
+
<article class="article">
|
|
159
|
+
<nav class="breadcrumb" aria-label="面包屑">
|
|
160
|
+
{trail.map((tr2, i) => (
|
|
161
|
+
<span key={`${tr2.url}-${i}`}>
|
|
162
|
+
{i > 0 && <span class="crumb-sep"> / </span>}
|
|
163
|
+
{i < trail.length - 1 ? <a href={tr2.url.replace(base, '')}>{tr2.name}</a> : <span>{tr2.name}</span>}
|
|
164
|
+
</span>
|
|
165
|
+
))}
|
|
166
|
+
</nav>
|
|
167
|
+
<header class="article-header">
|
|
168
|
+
<h1 class="article-title">{article.title}</h1>
|
|
169
|
+
<p class="article-meta">
|
|
170
|
+
<time datetime={article.updatedAt.toISOString()}>更新于 {new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'long', day: 'numeric' }).format(article.updatedAt)}</time>
|
|
171
|
+
</p>
|
|
172
|
+
</header>
|
|
173
|
+
<div class="article-body" dangerouslySetInnerHTML={{ __html: raw(article.html) }} />
|
|
174
|
+
</article>
|
|
175
|
+
</div>
|
|
176
|
+
</div>
|
|
177
|
+
</section>
|
|
178
|
+
</Layout>
|
|
179
|
+
);
|
|
180
|
+
}
|