From c6b263b862783c0129dbbdab008636161fa7c6ed Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Sun, 1 Nov 2026 09:07:38 -0500 Subject: [PATCH] fix(notes): drop fenced-code info string from Anki export body markdown_to_anki_html took the whole span between the opening and closing backtick runs as code, so a fenced block's info string (its language) became the first line of the rendered code. ```python\nprint(1)\n``` exported as
python
print(1)
, putting a stray "python" line inside every language-tagged code card. Start the code body after the opening fence line's newline, matching how a fenced code block is defined. Fences without an info string, and single-line spans with no newline, are unchanged. Co-Authored-By: Claude Opus 4.8 --- deeptutor/notes/anki_export.py | 7 ++++++- tests/notes/test_anki_export.py | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/deeptutor/notes/anki_export.py b/deeptutor/notes/anki_export.py index 6fab45a18..d5942a64d 100644 --- a/deeptutor/notes/anki_export.py +++ b/deeptutor/notes/anki_export.py @@ -92,7 +92,12 @@ def markdown_to_anki_html(text: str) -> str: def protect(match: re.Match[str]) -> str: raw = match.group() if match.lastgroup == "fence": - body = html.escape(raw[3:-3].strip("\n")).replace("\n", "
") + inner = raw[3:-3] + # The opening fence line holds the optional info string (language), + # which is not code; the body starts after that line's newline. + newline = inner.find("\n") + code = inner[newline + 1 :] if newline != -1 else inner + body = html.escape(code.strip("\n")).replace("\n", "
") return stash(f"
{body}
") if match.lastgroup == "code": return stash(f"{html.escape(raw[1:-1])}") diff --git a/tests/notes/test_anki_export.py b/tests/notes/test_anki_export.py index 5380fbef5..269019db0 100644 --- a/tests/notes/test_anki_export.py +++ b/tests/notes/test_anki_export.py @@ -182,6 +182,12 @@ def test_latex_becomes_anki_mathjax_and_markdown_inline_is_html(): assert "this" in italic +def test_fenced_code_language_is_not_part_of_the_code_body(): + html = markdown_to_anki_html("```python\nprint(1)\n```") + assert "
print(1)
" in html + assert "python" not in html + + def test_deck_title_sanitises_hierarchy_and_tags_slug_the_title(): assert deck_name("Child::Name") == "DeepTutor::Child — Name" assert deck_name("") == "DeepTutor::Untitled"