新增模块: - 友链(friendlink)后端处理 + 前端管理页与友链板 - 书库(library):导入、章节、封面、阅读页、横竖版自适应 AdaptiveCoverSlot - 顶栏导航(header_nav)配置与 MobileTabBar/MobileRailDrawers 移动端抽屉 - 排行榜 service 测试、站点页面测试、时间线发布(timeline_release) 其它改动: - 后端 handlers/services 全量小幅调整 - 前端组件、库函数、URL/品牌/站点 URL 工具更新 - Lightbox 图片、MdEntries 条目卡、coverColor 派生色相等前端能力
105 lines
3.1 KiB
TypeScript
105 lines
3.1 KiB
TypeScript
import type { ReactNode } from "react";
|
||
import type { Components } from "react-markdown";
|
||
import { renderMarkdownToReact } from "@/components/markdown/renderMarkdown";
|
||
import { splitContentSegments } from "@/lib/directiveBlocks";
|
||
import { expandDyEmojiShortcodes } from "@/lib/dyEmoji";
|
||
import type { HideKind } from "@/lib/hideBlocks";
|
||
import MdHideBlock from "@/components/markdown/MdHideBlock";
|
||
import MdTimeline from "@/components/markdown/MdTimeline";
|
||
import MdEntries from "@/components/markdown/MdEntries";
|
||
|
||
type MarkdownBodyProps = {
|
||
content: string;
|
||
className?: string;
|
||
components?: Components;
|
||
/** 详情页解锁门禁需要 */
|
||
postId?: number;
|
||
/** 登录跳转(公告等无 postId 时) */
|
||
loginHref?: string;
|
||
loggedIn?: boolean;
|
||
/** 书级「全章节条目卡模式」:章节内容免写 [entries] 壳 */
|
||
entriesAuto?: boolean;
|
||
/** 按分段索引的条目标题锚点 id(与章内目录共享;阅读页传入,其他场景缺省) */
|
||
entriesTitleIds?: (string[] | undefined)[];
|
||
};
|
||
|
||
/**
|
||
* 站点统一 Markdown 渲染(服务端):GFM + Shiki;支持 [hide] / [timeline] / [entries]。
|
||
*/
|
||
export default async function MarkdownBody({
|
||
content,
|
||
className = "prose-content",
|
||
components,
|
||
postId,
|
||
loginHref,
|
||
loggedIn = false,
|
||
entriesAuto = false,
|
||
entriesTitleIds,
|
||
}: MarkdownBodyProps) {
|
||
const segs = splitContentSegments(expandDyEmojiShortcodes(content || ""), {
|
||
entriesAuto,
|
||
});
|
||
const nodes = [];
|
||
for (let i = 0; i < segs.length; i++) {
|
||
const seg = segs[i];
|
||
if (seg.type === "md") {
|
||
if (!seg.text.trim()) continue;
|
||
nodes.push(
|
||
<div key={`md-${i}`}>{await renderMarkdownToReact(seg.text, components)}</div>
|
||
);
|
||
continue;
|
||
}
|
||
if (seg.type === "timeline") {
|
||
const bodies: ReactNode[] = [];
|
||
for (let j = 0; j < seg.items.length; j++) {
|
||
const it = seg.items[j];
|
||
if (!it.body.trim()) {
|
||
bodies.push(null);
|
||
continue;
|
||
}
|
||
// 条目正文不挂目录 id,避免与块外 headingRenderer 错位
|
||
bodies.push(await renderMarkdownToReact(expandDyEmojiShortcodes(it.body)));
|
||
}
|
||
nodes.push(
|
||
<MdTimeline
|
||
key={`tl-${i}`}
|
||
items={seg.items}
|
||
renderBody={(_it, idx) => bodies[idx]}
|
||
/>
|
||
);
|
||
continue;
|
||
}
|
||
if (seg.type === "entries") {
|
||
nodes.push(
|
||
<MdEntries key={`entries-${i}`} items={seg.items} titleIds={entriesTitleIds?.[i]} />
|
||
);
|
||
continue;
|
||
}
|
||
const kind = seg.kind as HideKind;
|
||
if (seg.locked) {
|
||
nodes.push(
|
||
<MdHideBlock
|
||
key={`hide-${i}`}
|
||
kind={kind}
|
||
points={seg.points}
|
||
locked
|
||
postId={postId}
|
||
loginHref={loginHref}
|
||
loggedIn={loggedIn}
|
||
/>
|
||
);
|
||
continue;
|
||
}
|
||
const inner = seg.body.trim()
|
||
? await renderMarkdownToReact(expandDyEmojiShortcodes(seg.body))
|
||
: null;
|
||
nodes.push(
|
||
<MdHideBlock key={`hide-${i}`} kind={kind} points={seg.points}>
|
||
{inner}
|
||
</MdHideBlock>
|
||
);
|
||
}
|
||
|
||
return <div className={className}>{nodes}</div>;
|
||
}
|