Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

feat(reading): inline embedded pictures at their original position

flower-moon93c210f4abe2
diff --git a/web/lib/reading-citations.ts b/web/lib/reading-citations.ts
index b6ebbd53a..a0b3ef9b1 100644
--- a/web/lib/reading-citations.ts
+++ b/web/lib/reading-citations.ts
@@ -304,8 +304,10 @@ export function citationTargetFromHref(
return Number.isInteger(locator) && locator >= 1 ? { locator } : null;
}
if (!href.startsWith(MATERIAL_LOCATOR_HREF_PREFIX)) return null;
+ // Material ids are content hashes or catalog-minted rm_ ids, matching the
+ // id shape the server-side citation generator can emit.
const match =
- /^#dt-material-([0-9a-f]{8,64})(?:-revision-(\d+))?-locator-(\d+)$/i.exec(
+ /^#dt-material-((?:[0-9a-f]{8,64}|rm_[0-9a-f]{12}))(?:-revision-(\d+))?-locator-(\d+)$/i.exec(
href,
);
if (!match) return null;
diff --git a/web/lib/reading-inline-markdown.tsx b/web/lib/reading-inline-markdown.tsx
index 0c44ae247..b42dbcc35 100644
--- a/web/lib/reading-inline-markdown.tsx
+++ b/web/lib/reading-inline-markdown.tsx
@@ -37,6 +37,28 @@ function HiddenMark({ text }: { text: string }) {
const INLINE_PATTERN =
/(\*\*|__)([^\n]+?)\1|`([^`\n]+)`|\[([^\]\n]+)\]\(([^)\s]+)\)|(\*|_)([^\s*_][^\n]*?)\6(?!\w)/g;
+/**
+ * An embedded-picture ingest marker line — ``[图片 3: image-03.png]``, the
+ * exact fixed format the extractor emits. Matching the whole line keeps the
+ * figure at the picture's original position between paragraphs; anything
+ * else falls back to literal text.
+ */
+const IMAGE_MARKER_PATTERN = /^\s{0,3}\[图片 \d+: ([^\]\s]+)\]\s*$/;
+
+/**
+ * Names of embedded pictures whose marker lines appear in *text*, so a caller
+ * can tell which media items the inline renderer claimed and which still need
+ * the fallback strip below the article.
+ */
+export function imageMarkerNames(text: string): Set<string> {
+ const names = new Set<string>();
+ for (const line of text.split("\n")) {
+ const match = IMAGE_MARKER_PATTERN.exec(line);
+ if (match) names.add(match[1]);
+ }
+ return names;
+}
+
/** Bold, italic, inline code, and links within one line of plain text. */
export function InlineMarkdown({ text }: { text: string }): ReactNode {
const nodes: ReactNode[] = [];
@@ -114,9 +136,24 @@ const BULLET_PATTERN = /^(\s{0,3}[-*+]\s+)(\S[\s\S]*)$/;
/**
* One rendered line of body text: block-level markers first (a line is at
* most one of rule / blockquote / bullet), then inline formatting on
- * whatever text remains.
+ * whatever text remains. An embedded-picture marker line goes to
+ * *renderImage* first — returning null keeps the literal marker text.
*/
-export function MarkdownLine({ text }: { text: string }): ReactNode {
+export function MarkdownLine({
+ text,
+ renderImage,
+}: {
+ text: string;
+ renderImage?: (name: string, markerText: string) => ReactNode | null;
+}): ReactNode {
+ if (renderImage) {
+ const image = IMAGE_MARKER_PATTERN.exec(text);
+ if (image) {
+ const figure = renderImage(image[1], text);
+ if (figure !== null && figure !== undefined) return figure;
+ }
+ }
+
if (RULE_PATTERN.test(text) && text.trim().length >= 3) {
return (
<>
diff --git a/web/lib/reading-location-history.ts b/web/lib/reading-location-history.ts
index c07c33ad2..1cbafda1a 100644
--- a/web/lib/reading-location-history.ts
+++ b/web/lib/reading-location-history.ts
@@ -35,7 +35,8 @@ function normalizeEntry(value: unknown): ReadingLocationEntry | null {
.trim()
.toLowerCase();
const locator = Number(row.locator);
- if (!/^[0-9a-f]{8,64}$/.test(materialId)) return null;
+ // 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)) return null;
if (!Number.isInteger(locator) || locator < 1) return null;
const rawSource =
row.source && typeof row.source === "object"
diff --git a/web/lib/reading-references.ts b/web/lib/reading-references.ts
index ab4920a92..e2911a7d7 100644
--- a/web/lib/reading-references.ts
+++ b/web/lib/reading-references.ts
@@ -60,7 +60,8 @@ export function normalizeReadingReferences(
typeof record.material_id === "string"
? record.material_id.trim().toLowerCase()
: "";
- if (!/^[0-9a-f]{8,64}$/.test(materialId)) continue;
+ // 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" ||
diff --git a/web/lib/reading-turn-state.ts b/web/lib/reading-turn-state.ts
index 360b7c481..fee0161cf 100644
--- a/web/lib/reading-turn-state.ts
+++ b/web/lib/reading-turn-state.ts
@@ -37,11 +37,18 @@ const state: ReadingTurnState = {
timeSeconds: null,
};
-/** Validate persisted/wire material ids before they become reader addresses. */
+/**
+ * Validate persisted/wire material ids before they become reader addresses.
+ * Content hashes and catalog-minted rm_ ids (a second copy of the same
+ * content gets its own catalog row) are both reader addresses; the store
+ * resolves both to the same content directory.
+ */
+const MATERIAL_ID_RE = /^(?:[0-9a-f]{8,64}|rm_[0-9a-f]{12})$/;
+
export function normalizeReadingMaterialId(value: unknown): string | null {
if (typeof value !== "string") return null;
const normalized = value.trim().toLowerCase();
- return /^[0-9a-f]{8,64}$/.test(normalized) ? normalized : null;
+ return MATERIAL_ID_RE.test(normalized) ? normalized : null;
}
/** Normalize an immutable ReadingStore content revision from wire/storage. */
diff --git a/web/tests/reading-turn-state.test.ts b/web/tests/reading-turn-state.test.ts
index ece8c777d..abde67323 100644
--- a/web/tests/reading-turn-state.test.ts
+++ b/web/tests/reading-turn-state.test.ts
@@ -24,6 +24,15 @@ test("normalizes persisted reading material ids and rejects unsafe values", () =
assert.equal(normalizeReadingMaterialId(null), null);
});
+test("accepts catalog-minted rm_ material ids", () => {
+ assert.equal(
+ normalizeReadingMaterialId("rm_6e2e838e6381"),
+ "rm_6e2e838e6381",
+ );
+ assert.equal(normalizeReadingMaterialId("rm_short"), null);
+ assert.equal(normalizeReadingMaterialId("rm_6e2e838e63811"), null);
+});
+
test("normalizes immutable material revisions", () => {
assert.equal(normalizeReadingMaterialRevision(3), 3);
assert.equal(normalizeReadingMaterialRevision("4"), 4);