From 3503cc22653b4e4d2f5cb194aa85ec94c0696ae1 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Tue, 17 Nov 2026 13:36:34 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Drop=20continuation-line?= =?UTF-8?q?=20indentation=20inside=20code=20spans?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Leading spaces or tabs of paragraph continuation lines are not part of the paragraph's raw content (CommonMark 4.8), but the backticks rule only turned the line ending into a space, so ``a\n b`` gave a b instead of a b (commonmark.js). Fixes #475 --- markdown_it/rules_inline/backticks.py | 9 ++++++- tests/test_port/fixtures/issue-fixes.md | 34 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/markdown_it/rules_inline/backticks.py b/markdown_it/rules_inline/backticks.py index 30af0161..986c8d25 100644 --- a/markdown_it/rules_inline/backticks.py +++ b/markdown_it/rules_inline/backticks.py @@ -1,6 +1,10 @@ # Parse backticks +import re + from .state_inline import StateInline +_NEWLINE_INDENT_RE = re.compile(r"\n[ \t]*") + def _build_last_runs(src: str) -> dict[int, int]: """Map each backtick run length to its last position in the source.""" @@ -56,7 +60,10 @@ def backtick(state: StateInline, silent: bool) -> bool: if not silent: token = state.push("code_inline", "code", 0) token.markup = marker - content = state.src[pos:match_start].replace("\n", " ") + # Line endings become spaces. The leading spaces or tabs of + # each continuation line are not part of the paragraph's raw + # content (CommonMark 4.8), so they are dropped too. + content = _NEWLINE_INDENT_RE.sub(" ", state.src[pos:match_start]) # Strip one space from each side, unless the content is all spaces if ( content.startswith(" ") diff --git a/tests/test_port/fixtures/issue-fixes.md b/tests/test_port/fixtures/issue-fixes.md index fefef099..e88c9bc9 100644 --- a/tests/test_port/fixtures/issue-fixes.md +++ b/tests/test_port/fixtures/issue-fixes.md @@ -244,3 +244,37 @@ para *a* . + +#475 a code span drops the indentation of a continuation line +. +`a + b` +. +

a b

+. + +#475 same with a tab +. +x `a + b` +. +

x a b

+. + +#475 same inside a list item with extra indentation +. +1. x `a + b` +. +
    +
  1. x a b
  2. +
+. + +#475 trailing spaces before the line ending are kept +. +`a + b` +. +

a b

+.