feat: 友链/书库/移动端导航/排行榜等多模块功能
新增模块: - 友链(friendlink)后端处理 + 前端管理页与友链板 - 书库(library):导入、章节、封面、阅读页、横竖版自适应 AdaptiveCoverSlot - 顶栏导航(header_nav)配置与 MobileTabBar/MobileRailDrawers 移动端抽屉 - 排行榜 service 测试、站点页面测试、时间线发布(timeline_release) 其它改动: - 后端 handlers/services 全量小幅调整 - 前端组件、库函数、URL/品牌/站点 URL 工具更新 - Lightbox 图片、MdEntries 条目卡、coverColor 派生色相等前端能力
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { TocItem } from "@/components/PostToc";
|
||||
import type { ContentSegment } from "@/lib/directiveBlocks";
|
||||
|
||||
const TIMELINE_DAY_RE = /^##\s+(\d{4}-\d{2}-\d{2})\s+(.+)$/;
|
||||
const HEADING_RE = /^(#{2,3})\s+(.+?)\s*#*\s*$/;
|
||||
@@ -14,13 +15,14 @@ export function timelineDayId(date: string): string {
|
||||
* 从正文提取目录:
|
||||
* - 块外 ## / ### → h-{n}(供 headingRenderer 按序挂 id)
|
||||
* - [timeline] 内按日期去重 → tl-YYYY-MM-DD(日分组锚点)
|
||||
* - 代码围栏 / [hide] 块内标题不计入
|
||||
* - 代码围栏 / [hide] / [entries] 块内标题不计入
|
||||
*/
|
||||
export function extractToc(content: string): TocItem[] {
|
||||
const items: TocItem[] = [];
|
||||
let inCode = false;
|
||||
let inTimeline = false;
|
||||
let inHide = false;
|
||||
let inEntries = false;
|
||||
let headingCount = 0;
|
||||
/** 当前 timeline 块内出现过的日期(保持文件顺序,收块时再按日倒序发出) */
|
||||
let timelineDates: string[] = [];
|
||||
@@ -65,6 +67,17 @@ export function extractToc(content: string): TocItem[] {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!inHide && !inTimeline && !inEntries && lower === "[entries]") {
|
||||
inEntries = true;
|
||||
continue;
|
||||
}
|
||||
if (inEntries && lower === "[/entries]") {
|
||||
inEntries = false;
|
||||
continue;
|
||||
}
|
||||
// 条目卡块内标题不进目录(渲染时也不挂 h-{n} 锚点,避免错位)
|
||||
if (inEntries) continue;
|
||||
|
||||
if (!inHide && HIDE_OPEN_RE.test(line)) {
|
||||
inHide = true;
|
||||
continue;
|
||||
@@ -95,6 +108,60 @@ export function markdownHeadingToc(toc: TocItem[]): TocItem[] {
|
||||
return toc.filter((t) => /^h-\d+$/.test(t.id));
|
||||
}
|
||||
|
||||
/** buildTocFromSegments 的产物:目录项 + 供正文挂锚点的三组 id */
|
||||
export interface SegmentsToc {
|
||||
/** 按渲染顺序的目录项(md 标题 / 条目标题 / 时间线日),id 已由 nextAnchor 分配 */
|
||||
items: TocItem[];
|
||||
/** md 段标题 id 序列(供 headingComponentsFor 按序挂到 h2/h3) */
|
||||
mdHeadIds: string[];
|
||||
/** 按段索引组织的条目标题 id(传给 MdEntries 挂到卡内标题) */
|
||||
entriesTitleIds: (string[] | undefined)[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 基于「渲染分段」构建章内目录(条目卡模式下的目录来源):
|
||||
* - md 段标题 → h2/h3 级目录项(顺序与 headingRenderer 一致)
|
||||
* - entries 段条目标题 → 二级目录项(与卡内标题共享锚点,目录可跳转)
|
||||
* - timeline 段 → 日分组锚点(与 extractToc 同为日倒序);hide 段内容不进目录
|
||||
* nextAnchor 与阅读页章内锚点计数器共享,保证目录、正文、卡内标题同一份 id
|
||||
*/
|
||||
export function buildTocFromSegments(
|
||||
segs: ContentSegment[],
|
||||
nextAnchor: () => string
|
||||
): SegmentsToc {
|
||||
const items: TocItem[] = [];
|
||||
const mdHeadIds: string[] = [];
|
||||
const entriesTitleIds: (string[] | undefined)[] = [];
|
||||
|
||||
for (let si = 0; si < segs.length; si++) {
|
||||
const seg = segs[si];
|
||||
if (seg.type === "md") {
|
||||
for (const raw of seg.text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n")) {
|
||||
const m = HEADING_RE.exec(raw.trim());
|
||||
if (!m) continue;
|
||||
const id = nextAnchor();
|
||||
items.push({ level: m[1].length as 2 | 3, text: m[2], id });
|
||||
mdHeadIds.push(id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (seg.type === "entries") {
|
||||
const ids = seg.items.map(() => nextAnchor());
|
||||
seg.items.forEach((it, i) => items.push({ level: 2, text: it.title, id: ids[i] }));
|
||||
entriesTitleIds[si] = ids;
|
||||
continue;
|
||||
}
|
||||
if (seg.type === "timeline") {
|
||||
const dates = [...new Set(seg.items.map((t) => t.date))].sort((a, b) => (a < b ? 1 : a > b ? -1 : 0));
|
||||
for (const date of dates) {
|
||||
items.push({ level: 2, text: date, id: timelineDayId(date) });
|
||||
}
|
||||
}
|
||||
// hide 段内容(未解锁/未登录不可见)不进目录
|
||||
}
|
||||
return { items, mdHeadIds, entriesTitleIds };
|
||||
}
|
||||
|
||||
/** 粗略阅读时长:约 400 字/分钟,至少 1 分钟 */
|
||||
export function estimateReadingMinutes(text: string, fallbackLen = 0): number {
|
||||
const len = text.trim().length > 0 ? text.trim().length : fallbackLen;
|
||||
|
||||
Reference in New Issue
Block a user