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"