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
<pre><code>python<br>print(1)</code></pre>, 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 <noreply@anthropic.com>
This commit is contained in:
Rudy Celekli
2026-11-01 09:07:38 -05:00
parent f7aed0744c
commit c6b263b862
2 changed files with 12 additions and 1 deletions

View File

@@ -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", "<br>")
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", "<br>")
return stash(f"<pre><code>{body}</code></pre>")
if match.lastgroup == "code":
return stash(f"<code>{html.escape(raw[1:-1])}</code>")

View File

@@ -182,6 +182,12 @@ def test_latex_becomes_anki_mathjax_and_markdown_inline_is_html():
assert "<i>this</i>" in italic
def test_fenced_code_language_is_not_part_of_the_code_body():
html = markdown_to_anki_html("```python\nprint(1)\n```")
assert "<pre><code>print(1)</code></pre>" 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"