From 8601a6014fbcea3c9a5c1ece894133bf394ac545 Mon Sep 17 00:00:00 2001 From: Tiel Coder Date: Sat, 21 Nov 2026 00:45:32 +0000 Subject: [PATCH] feat(training): captions under lesson images MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Authors can now attach a caption to an image in the lesson editor; it is sanitized as figure text and follows the image's alignment in the reader. 🤖 Generated with Codebuff Co-Authored-By: Codebuff --- e2e/training.spec.ts | 6 ++ src/app/(app)/training/guides/admin/page.tsx | 6 +- src/app/globals.css | 16 ++++ .../app/training/rich-text-editor.tsx | 94 +++++++++++++++++-- src/lib/rich-text.test.ts | 11 +++ src/lib/rich-text.ts | 8 +- 6 files changed, 130 insertions(+), 11 deletions(-) diff --git a/e2e/training.spec.ts b/e2e/training.spec.ts index 9e20b9c..cd132d5 100644 --- a/e2e/training.spec.ts +++ b/e2e/training.spec.ts @@ -91,11 +91,16 @@ test.describe("training", () => { await page .getByRole("button", { name: "Set image width to 50%" }) .click(); + page.once("dialog", (dialog) => dialog.accept("Front-of-house console")); + await page.getByRole("button", { name: "Add image caption" }).click(); await page.getByRole("button", { name: "Save lesson" }).click(); await expect( lesson.locator('img[data-align="right"][width="50%"]'), ).toBeVisible(); + await expect(lesson.locator("figcaption")).toHaveText( + "Front-of-house console", + ); const pdf = Buffer.from("%PDF-1.7\n% E2E manual\n"); await lesson @@ -127,6 +132,7 @@ test.describe("training", () => { ).toHaveCount(0); await expect(page.getByRole("link", { name: "guide.txt" })).toBeVisible(); await expect(page.getByRole("img", { name: "console.png" })).toBeVisible(); + await expect(page.getByText("Front-of-house console")).toBeVisible(); await expect( page.getByRole("link", { name: "audio-manual.pdf" }), ).toBeVisible(); diff --git a/src/app/(app)/training/guides/admin/page.tsx b/src/app/(app)/training/guides/admin/page.tsx index 3c01615..dc67e45 100644 --- a/src/app/(app)/training/guides/admin/page.tsx +++ b/src/app/(app)/training/guides/admin/page.tsx @@ -683,9 +683,9 @@ export default function AdminGuidePage() { Press the image button to upload a screenshot. Select the image to - align it left, center, or right, choose a width, edit its - description, or move it up and down. You can also drag the image to a - new position in the lesson. + align it left, center, or right, choose a width, add a caption shown + beneath it, edit its description, or move it up and down. You can + also drag the image to a new position in the lesson. Set a sort order to control the list position. Lower numbers appear diff --git a/src/app/globals.css b/src/app/globals.css index 1023c6a..0c18477 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -245,6 +245,22 @@ margin-left: auto; } +.training-prose figcaption { + margin-top: 0.4rem; + color: var(--muted-foreground); + font-size: 0.78rem; + line-height: 1.5; + text-align: center; +} + +.training-prose figcaption[data-align="left"] { + text-align: left; +} + +.training-prose figcaption[data-align="right"] { + text-align: right; +} + .training-prose img.ProseMirror-selectednode { outline: 3px solid var(--ring); outline-offset: 2px; diff --git a/src/components/app/training/rich-text-editor.tsx b/src/components/app/training/rich-text-editor.tsx index bc385f9..f2dc3da 100644 --- a/src/components/app/training/rich-text-editor.tsx +++ b/src/components/app/training/rich-text-editor.tsx @@ -12,6 +12,7 @@ import { ArrowDown, ArrowUp, Bold, + Captions, Code, Heading2, Heading3, @@ -31,24 +32,81 @@ import { } from "lucide-react"; import { Button } from "@/components/ui/button"; +/** The for an image node, whether the node parsed from an or a
. */ +function imageElement(element: HTMLElement): HTMLElement { + return element.tagName === "IMG" + ? element + : element.querySelector("img") ?? element; +} + /** - * The image node adds alignment to the stock image extension. Width is already - * a native attribute, so the toolbar can offer percentage presets without any - * inline styles (which the server sanitizer intentionally strips). + * The image node adds alignment and a caption to the stock image extension. + * Width is already a native attribute and the caption is emitted as the + * figcaption text, so the toolbar needs no inline styles (which the server + * sanitizer intentionally strips). */ const TrainingImage = Image.extend({ addAttributes() { return { - ...this.parent?.(), + src: { + default: null, + parseHTML: (element: HTMLElement) => + imageElement(element).getAttribute("src"), + }, + alt: { + default: null, + parseHTML: (element: HTMLElement) => + imageElement(element).getAttribute("alt"), + }, + title: { + default: null, + parseHTML: (element: HTMLElement) => + imageElement(element).getAttribute("title"), + }, + width: { + default: null, + parseHTML: (element: HTMLElement) => + imageElement(element).getAttribute("width"), + }, align: { default: "center", - parseHTML: (element) => element.getAttribute("data-align") ?? "center", - renderHTML: (attributes) => ({ + parseHTML: (element: HTMLElement) => + imageElement(element).getAttribute("data-align") ?? "center", + renderHTML: (attributes: Record) => ({ "data-align": attributes.align ?? "center", }), }, + caption: { + default: null, + parseHTML: (element: HTMLElement) => + element.tagName === "FIGURE" + ? element.querySelector("figcaption")?.textContent?.trim() || null + : null, + }, }; }, + parseHTML() { + return [ + { tag: "figure[data-lesson-image]" }, + { tag: 'img[src]:not([src^="data:"])' }, + ]; + }, + renderHTML({ HTMLAttributes }) { + const { caption: captionAttribute, ...attributes } = HTMLAttributes; + const caption = + typeof captionAttribute === "string" ? captionAttribute.trim() : ""; + const align = attributes["data-align"] ?? "center"; + + if (!caption) { + return ["figure", { "data-lesson-image": "true" }, ["img", attributes]]; + } + return [ + "figure", + { "data-lesson-image": "true" }, + ["img", attributes], + ["figcaption", { "data-align": align }, caption], + ]; + }, }); const IMAGE_WIDTHS = ["25%", "50%", "75%", "100%"] as const; @@ -192,6 +250,7 @@ export function RichTextEditor({ ? String(imageAttributes.width) : ""; const imageAlt = (imageAttributes.alt as string | undefined) ?? ""; + const imageCaption = (imageAttributes.caption as string | undefined) ?? ""; function promptLink() { if (!editor) return; @@ -224,6 +283,20 @@ export function RichTextEditor({ .run(); } + function promptImageCaption() { + if (!editor) return; + const entered = window.prompt( + "Caption shown under the image (leave empty to remove it)", + imageCaption, + ); + if (entered === null) return; + editor + .chain() + .focus() + .updateAttributes("image", { caption: entered.trim() || null }) + .run(); + } + async function uploadImage(file: File) { if (!editor) return; if (!file.type.startsWith("image/")) { @@ -489,6 +562,13 @@ export function RichTextEditor({ ALT + + editor && moveImage(editor, -1)} @@ -515,7 +595,7 @@ export function RichTextEditor({
Insert images with the image button. Select an image to align, resize, - move, or describe it; you can also drag it to a new position. + caption, move, or describe it; you can also drag it to a new position. {uploading && ( diff --git a/src/lib/rich-text.test.ts b/src/lib/rich-text.test.ts index 8af00aa..ef3da74 100644 --- a/src/lib/rich-text.test.ts +++ b/src/lib/rich-text.test.ts @@ -30,6 +30,17 @@ describe("sanitizeTrainingHtml", () => { expect(dataUrl).not.toContain(" { + const html = sanitizeTrainingHtml( + '
Console
Console ready
', + ); + + expect(html).toContain('
'); + expect(html).toContain('
'); + expect(html).toContain("Console ready"); + expect(html).not.toContain(" { const external = sanitizeTrainingHtml( 'Manual', diff --git a/src/lib/rich-text.ts b/src/lib/rich-text.ts index 05f8e73..3fb49a5 100644 --- a/src/lib/rich-text.ts +++ b/src/lib/rich-text.ts @@ -32,12 +32,16 @@ const ALLOWED_TAGS = [ "code", "a", "img", + "figure", + "figcaption", "hr", ]; const ALLOWED_ATTRIBUTES: sanitizeHtml.IOptions["allowedAttributes"] = { a: ["href", "target", "rel", "title"], img: ["src", "alt", "title", "width", "height", "data-align"], + figure: ["data-lesson-image"], + figcaption: ["data-align"], }; function escapeHtml(value: string): string { @@ -57,7 +61,9 @@ function escapeHtml(value: string): string { function looksLikeHtml(value: string): boolean { return ( /<\/[a-z][^>]*>/i.test(value) || - /<(p|h[1-6]|ul|ol|li|blockquote|pre|img|hr|br|div)\b[^>]*>/i.test(value) + /<(p|h[1-6]|ul|ol|li|blockquote|pre|figure|img|hr|br|div)\b[^>]*>/i.test( + value, + ) ); }