Add Obsidian Wrapped feature (inside-LMS, client-side vault parsing)

- WrappedRun Prisma model + migration (aggregates only, no note names/text)
- /wrapped authed route: FSA + webkitdirectory + drag-drop reader, Web Worker parser
- archetype + score heuristics, ДС-2 result card
- POST /api/wrapped/run (userId from session), public /share/wrapped/[token]
- next/og OG image (Satori+resvg-wasm, alpine-safe) + Fira Mono ttf
- middleware: /share/wrapped public

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-23 14:19:59 +05:00
parent 1cde567dbe
commit 06b8d9f64b
14 changed files with 790 additions and 1 deletions
@@ -0,0 +1,81 @@
// Web Worker: парсит .md-тексты в агрегаты (off-main, без фриза UI).
// Получает RawNote[], возвращает Stats. Тексты заметок дальше воркера не идут.
import type { RawNote, Stats, Period, Archetype } from "./types";
const WIKILINK = /\[\[([^\]|#]+)/g;
const TAG = /(^|\s)#([\p{L}\d_/-]+)/gu;
const DAY = 864e5;
function filterByPeriod(notes: RawNote[], period: Period): RawNote[] {
if (period === "all") return notes;
const cutoff = Date.now() - (period === "year" ? 365 * DAY : 90 * DAY);
return notes.filter((n) => n.mtime >= cutoff);
}
function buildStats(
notes: RawNote[],
degree: Map<string, number>,
tags: Map<string, number>,
linkCount: number,
period: Period,
): Stats {
const noteCount = notes.length;
const topTags = [...tags.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([tag, n]) => ({ tag, n }));
const topLinked = [...degree.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([name, d]) => ({ name, degree: d }));
// Сирота — заметка, на которую никто не ссылается (грубая оценка по имени).
const linkedTargets = new Set(degree.keys());
const orphans = notes.filter((n) => !linkedTargets.has(n.name.replace(/\.md$/i, ""))).length;
const months = new Map<string, number>();
for (const n of notes) {
const d = new Date(n.mtime);
const k = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}`;
months.set(k, (months.get(k) ?? 0) + 1);
}
const busiestMonth = [...months.entries()].sort((a, b) => b[1] - a[1])[0]?.[0] ?? "";
const linkDensity = noteCount ? linkCount / noteCount : 0;
const tagDensity = noteCount ? tags.size / noteCount : 0;
const orphanRatio = noteCount ? orphans / noteCount : 1;
let archetype: Archetype;
if (linkDensity >= 2) archetype = "architect";
else if (tagDensity >= 0.5) archetype = "librarian";
else if (orphanRatio >= 0.4) archetype = "gardener";
else archetype = "pragmatist";
const score = Math.max(0, Math.min(100, Math.round(
Math.min(linkDensity / 3, 1) * 45 + Math.min(tagDensity, 1) * 25 + Math.min(noteCount / 300, 1) * 30,
)));
const achievements: string[] = [];
if (noteCount >= 100) achievements.push(`${noteCount} заметок`);
if (linkCount >= 200) achievements.push(`${linkCount} связей`);
if (tags.size >= 30) achievements.push(`${tags.size} тегов`);
if (linkDensity >= 3) achievements.push("Плотная сеть знаний");
if (orphanRatio < 0.1 && noteCount >= 50) achievements.push("Почти нет сирот");
return { noteCount, linkCount, tagCount: tags.size, topTags, topLinked, orphans, busiestMonth, achievements, archetype, score, period };
}
self.onmessage = (e: MessageEvent<{ notes: RawNote[]; period: Period }>) => {
const { notes, period } = e.data;
const filtered = filterByPeriod(notes, period);
const degree = new Map<string, number>();
const tags = new Map<string, number>();
let linkCount = 0;
for (const n of filtered) {
for (const m of n.text.matchAll(WIKILINK)) {
linkCount++;
const t = m[1].trim();
if (t) degree.set(t, (degree.get(t) ?? 0) + 1);
}
for (const m of n.text.matchAll(TAG)) {
const t = m[2];
tags.set(t, (tags.get(t) ?? 0) + 1);
}
}
self.postMessage(buildStats(filtered, degree, tags, linkCount, period));
};