diff --git a/deeptutor/book/export.py b/deeptutor/book/export.py
index 7342d0870..a15a05770 100644
--- a/deeptutor/book/export.py
+++ b/deeptutor/book/export.py
@@ -177,8 +177,8 @@ def _render_flash_cards(payload: dict[str, Any], labels: dict[str, str]) -> str:
for card in cards:
if not isinstance(card, dict):
continue
- front = _payload_text(card, "front").replace("|", "\\|")
- back = _payload_text(card, "back").replace("|", "\\|")
+ front = _payload_text(card, "front").replace("|", "\\|").replace("\n", "
")
+ back = _payload_text(card, "back").replace("|", "\\|").replace("\n", "
")
rows.append(f"| {front} | {back} |")
return "\n".join(rows) if len(rows) > 2 else ""
diff --git a/tests/book/test_export.py b/tests/book/test_export.py
index 3c95dc4ba..bd5ea48bb 100644
--- a/tests/book/test_export.py
+++ b/tests/book/test_export.py
@@ -64,6 +64,20 @@ def test_flash_cards_become_a_table_with_escaped_pipes() -> None:
assert r"a\|b" in md
+def test_flash_card_newlines_stay_inside_the_table_row() -> None:
+ md = render_block(
+ _ready(
+ BlockType.FLASH_CARDS,
+ {"cards": [{"front": "Define X", "back": "First line\nSecond line"}]},
+ )
+ )
+ table_rows = [line for line in md.splitlines() if line.startswith("|")]
+ # The front/back of every card must stay on a single table row; a raw
+ # newline would split the GFM row and corrupt the rest of the table.
+ assert any("First line" in row and "Second line" in row for row in table_rows)
+ assert "First line
Second line" in md
+
+
def test_unrendered_blocks_contribute_nothing() -> None:
pending = Block(type=BlockType.TEXT, status=BlockStatus.PENDING, payload={"body": "x"})
assert render_block(pending) == ""