Files
DeepTutor/web/lib/reading-references.ts
flower-moon 93c210f4ab feat(reading): inline embedded pictures at their original position
The marker lines the extractor embeds ([图片 N: image-NN.png]) rendered
as literal text in the body with the pictures collected in a separate
strip below the article, each captioned with its filename. Render the
picture itself at the marker's position instead — the marker text stays
in the DOM under the same HiddenMark pattern every other collapsed
marker uses, so Recogito's char-level selectors still resolve against
the exact text an annotation was saved against. A picture whose media
row is missing keeps its literal marker, and anything the inline
renderer cannot claim falls back to the strip.

Also accept catalog-minted rm_ material ids in the reader's wire
validators (turn state, references, location history, citation
anchors), matching the id shape the store already accepts.
2026-09-17 18:46:28 +08:00

107 lines
2.8 KiB
TypeScript

"use client";
import type { UnitKind } from "@/lib/reading-api";
export const MAX_READING_REFERENCE_MATERIALS = 8;
export const MAX_READING_REFERENCE_UNITS = 24;
export interface SelectedReadingUnit {
locator: number;
title: string;
}
export interface SelectedReadingReference {
materialId: string;
revision: number;
materialTitle: string;
unit: UnitKind;
units: SelectedReadingUnit[];
}
export interface ReadingReferencePayload {
material_id: string;
revision: number;
locators: number[];
}
export function selectedReadingsToPayload(
references: SelectedReadingReference[],
): ReadingReferencePayload[] {
return normalizeReadingReferences(
references.map((reference) => ({
material_id: reference.materialId,
revision: reference.revision,
locators: reference.units.map((unit) => unit.locator),
})),
);
}
export function countSelectedReadingUnits(
references: SelectedReadingReference[],
): number {
return references.reduce(
(total, reference) => total + reference.units.length,
0,
);
}
export function normalizeReadingReferences(
value: unknown,
): ReadingReferencePayload[] {
if (!Array.isArray(value)) return [];
const byReference = new Map<string, ReadingReferencePayload>();
const materialIds = new Set<string>();
let total = 0;
for (const item of value) {
if (!item || typeof item !== "object") continue;
const record = item as Record<string, unknown>;
const materialId =
typeof record.material_id === "string"
? record.material_id.trim().toLowerCase()
: "";
// Same id shape the store accepts: content hashes and catalog rm_ ids.
if (!/^(?:[0-9a-f]{8,64}|rm_[0-9a-f]{12})$/.test(materialId)) continue;
const revision = record.revision;
if (
typeof revision !== "number" ||
!Number.isSafeInteger(revision) ||
revision <= 0
) {
continue;
}
if (!Array.isArray(record.locators)) continue;
const key = `${materialId}:${revision}`;
let reference = byReference.get(key);
if (
!reference &&
!materialIds.has(materialId) &&
materialIds.size >= MAX_READING_REFERENCE_MATERIALS
) {
continue;
}
for (const raw of record.locators) {
if (
typeof raw !== "number" ||
!Number.isSafeInteger(raw) ||
raw <= 0 ||
reference?.locators.includes(raw)
) {
continue;
}
if (total >= MAX_READING_REFERENCE_UNITS) break;
if (!reference) {
reference = { material_id: materialId, revision, locators: [] };
byReference.set(key, reference);
materialIds.add(materialId);
}
reference.locators.push(raw);
total += 1;
}
}
return [...byReference.values()].filter(
(reference) => reference.locators.length > 0,
);
}