From 89c92e739553413c2f13b1d76bbe89f6e6c35b3a Mon Sep 17 00:00:00 2001 From: riccardo-algorime Date: Tue, 10 Nov 2026 13:44:05 +0000 Subject: [PATCH] fix(book): render math in callouts, key takeaways and flash cards These blocks printed their text as is, so the LaTeX the book writer puts in them showed raw (`$q^n \to \infty$`). They now go through MarkdownRenderer like section bodies, the way #1364 fixed Mastery question cards. Co-Authored-By: Claude Opus 5.5 --- .../books/components/blocks/CalloutBlock.tsx | 10 +++- .../components/blocks/FlashCardsBlock.tsx | 23 ++++++--- .../books/components/blocks/SectionBlock.tsx | 10 ++-- web/tests/book-blocks-math.spec.tsx | 47 +++++++++++++++++++ 4 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 web/tests/book-blocks-math.spec.tsx diff --git a/web/app/(workspace)/learning/books/components/blocks/CalloutBlock.tsx b/web/app/(workspace)/learning/books/components/blocks/CalloutBlock.tsx index ef2e8f7a7..cc547eb90 100644 --- a/web/app/(workspace)/learning/books/components/blocks/CalloutBlock.tsx +++ b/web/app/(workspace)/learning/books/components/blocks/CalloutBlock.tsx @@ -6,6 +6,7 @@ import { BookmarkCheck, Sparkles, } from "lucide-react"; +import MarkdownRenderer from "@/components/common/MarkdownRenderer"; import type { Block } from "@/lib/book-types"; const VARIANT_STYLES: Record< @@ -59,7 +60,14 @@ export default function CalloutBlock({ block }: CalloutBlockProps) { > {label} -
{body}
+
+ +
); diff --git a/web/app/(workspace)/learning/books/components/blocks/FlashCardsBlock.tsx b/web/app/(workspace)/learning/books/components/blocks/FlashCardsBlock.tsx index 2b698cb85..943c77cef 100644 --- a/web/app/(workspace)/learning/books/components/blocks/FlashCardsBlock.tsx +++ b/web/app/(workspace)/learning/books/components/blocks/FlashCardsBlock.tsx @@ -3,6 +3,7 @@ import { useState } from "react"; import { ChevronLeft, ChevronRight, RotateCcw } from "lucide-react"; import { useTranslation } from "react-i18next"; +import MarkdownRenderer from "@/components/common/MarkdownRenderer"; import type { Block } from "@/lib/book-types"; interface Card { @@ -40,13 +41,23 @@ export default function FlashCardsBlock({ block }: FlashCardsBlockProps) { {showBack ? t("Answer") : t("Question")} - - {showBack ? card.back : card.front} - +
+ +
{!showBack && card.hint && ( - - {t("Hint")}: {card.hint} - +
+ +
)}
diff --git a/web/app/(workspace)/learning/books/components/blocks/SectionBlock.tsx b/web/app/(workspace)/learning/books/components/blocks/SectionBlock.tsx index 0244e4d98..c00f52d45 100644 --- a/web/app/(workspace)/learning/books/components/blocks/SectionBlock.tsx +++ b/web/app/(workspace)/learning/books/components/blocks/SectionBlock.tsx @@ -55,9 +55,13 @@ export default function SectionBlock({ block }: SectionBlockProps) { {keyTakeaway && (
-
- {t("Key takeaway:")} - {keyTakeaway} +
+
)} diff --git a/web/tests/book-blocks-math.spec.tsx b/web/tests/book-blocks-math.spec.tsx new file mode 100644 index 000000000..3c7defaad --- /dev/null +++ b/web/tests/book-blocks-math.spec.tsx @@ -0,0 +1,47 @@ +import { fireEvent, render, waitFor } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; + +import CalloutBlock from "@/app/(workspace)/learning/books/components/blocks/CalloutBlock"; +import FlashCardsBlock from "@/app/(workspace)/learning/books/components/blocks/FlashCardsBlock"; +import SectionBlock from "@/app/(workspace)/learning/books/components/blocks/SectionBlock"; +import type { Block } from "@/lib/book-types"; + +vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) })); + +function block(type: string, payload: Record): Block { + return { id: "blk-1", type, payload } as unknown as Block; +} + +async function expectMath(container: HTMLElement, count: number) { + await waitFor(() => expect(container.querySelectorAll(".katex")).toHaveLength(count)); + expect(container.textContent).not.toContain("$"); +} + +describe("book blocks render math", () => { + it("in a callout body", async () => { + const { container } = render( + 1$ then $q^n \\to \\infty$." })} />, + ); + await expectMath(container, 2); + }); + + it("in a section's key takeaway", async () => { + const { container } = render( + , + ); + await expectMath(container, 1); + }); + + it("on both sides of a flash card and in its hint", async () => { + const { container, getAllByRole } = render( + , + ); + await expectMath(container, 2); + fireEvent.click(getAllByRole("button")[0]); + await expectMath(container, 1); + }); +});