From 83a0c7aceb3fbb3dfac32d978b5765ba036b35ef Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sun, 1 Nov 2026 23:54:51 -0500 Subject: [PATCH] fix(book): keep flash-card newlines inside the Markdown table row _render_flash_cards escaped pipes but not newlines, so a card whose front/back contained a line break split the GFM table row and corrupted the rest of the table on "export book as Markdown". Replace interior newlines with
, matching the pipe escaping and the notebook table renderer's convention. Co-Authored-By: Claude Opus 4.8 --- deeptutor/book/export.py | 4 ++-- tests/book/test_export.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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) == ""