mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 05:51:24 +08:00
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:
@@ -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>")
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user