mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 11:39:34 +08:00
Merge PR #2597: fix(book): render math in callouts, key takeaways and flash cards
This commit is contained in:
@@ -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}
|
||||
</div>
|
||||
<div className="text-[14.5px] leading-relaxed">{body}</div>
|
||||
<div className="text-[14.5px] leading-relaxed">
|
||||
<MarkdownRenderer
|
||||
content={body}
|
||||
variant="compact"
|
||||
className="font-sans text-[14.5px] [&_p:first-child]:mt-0 [&_p:last-child]:mb-0 [&_p]:my-0"
|
||||
allowHtml={false}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
|
||||
@@ -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) {
|
||||
<span className="text-[10px] uppercase tracking-wider text-[var(--muted-foreground)]">
|
||||
{showBack ? t("Answer") : t("Question")}
|
||||
</span>
|
||||
<span className="text-base font-medium text-[var(--foreground)]">
|
||||
{showBack ? card.back : card.front}
|
||||
</span>
|
||||
<div className="text-base font-medium text-[var(--foreground)]">
|
||||
<MarkdownRenderer
|
||||
content={String((showBack ? card.back : card.front) ?? "")}
|
||||
variant="compact"
|
||||
className="font-sans text-base font-medium [&_p:first-child]:mt-0 [&_p:last-child]:mb-0 [&_p]:my-0"
|
||||
allowHtml={false}
|
||||
/>
|
||||
</div>
|
||||
{!showBack && card.hint && (
|
||||
<span className="text-xs italic text-[var(--muted-foreground)]">
|
||||
{t("Hint")}: {card.hint}
|
||||
</span>
|
||||
<div className="text-xs italic text-[var(--muted-foreground)]">
|
||||
<MarkdownRenderer
|
||||
content={`${t("Hint")}: ${card.hint}`}
|
||||
variant="compact"
|
||||
className="font-sans text-xs italic [&_p:first-child]:mt-0 [&_p:last-child]:mb-0 [&_p]:my-0"
|
||||
allowHtml={false}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</button>
|
||||
<div className="mt-3 flex items-center justify-between">
|
||||
|
||||
@@ -55,9 +55,13 @@ export default function SectionBlock({ block }: SectionBlockProps) {
|
||||
{keyTakeaway && (
|
||||
<div className="mt-6 flex items-start gap-2 rounded-xl border border-[var(--border)] bg-[var(--muted)]/30 px-3 py-2 text-sm">
|
||||
<Sparkles className="mt-0.5 h-4 w-4 flex-shrink-0 text-[var(--muted-foreground)]" />
|
||||
<div className="flex-1">
|
||||
<span className="mr-1 font-medium">{t("Key takeaway:")}</span>
|
||||
<span className="text-[var(--foreground)]">{keyTakeaway}</span>
|
||||
<div className="flex-1 text-[var(--foreground)]">
|
||||
<MarkdownRenderer
|
||||
content={`**${t("Key takeaway:")}** ${keyTakeaway}`}
|
||||
variant="compact"
|
||||
className="font-sans text-sm [&_p:first-child]:mt-0 [&_p:last-child]:mb-0 [&_p]:my-0"
|
||||
allowHtml={false}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
47
web/tests/book-blocks-math.spec.tsx
Normal file
47
web/tests/book-blocks-math.spec.tsx
Normal file
@@ -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<string, unknown>): 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(
|
||||
<CalloutBlock block={block("callout", { variant: "key_idea", body: "If $q > 1$ then $q^n \\to \\infty$." })} />,
|
||||
);
|
||||
await expectMath(container, 2);
|
||||
});
|
||||
|
||||
it("in a section's key takeaway", async () => {
|
||||
const { container } = render(
|
||||
<SectionBlock block={block("section", { key_takeaway: "Divide by $n^k$." })} />,
|
||||
);
|
||||
await expectMath(container, 1);
|
||||
});
|
||||
|
||||
it("on both sides of a flash card and in its hint", async () => {
|
||||
const { container, getAllByRole } = render(
|
||||
<FlashCardsBlock
|
||||
block={block("flash_cards", {
|
||||
cards: [{ front: "$\\lim \\frac{1}{n}$?", back: "$0$", hint: "Compare with $n$." }],
|
||||
})}
|
||||
/>,
|
||||
);
|
||||
await expectMath(container, 2);
|
||||
fireEvent.click(getAllByRole("button")[0]);
|
||||
await expectMath(container, 1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user