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

+.