From a0162420507ed34adfbde37443ecaba2e29684c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Backstr=C3=B6m?= Date: Fri, 9 Oct 2026 11:38:52 +0200 Subject: [PATCH 1/2] earmark_parser: Preserve code span whitespace Earmark adopted full trimming and whitespace collapsing from CommonMark 0.28. CommonMark 0.29 changed the rule because whitespace can be significant in code spans. Preserve one-sided edge spaces and repeated internal spaces. Normalize line endings to spaces, and remove one edge space only when both edges contain spaces. Assisted-by: Codex:gpt-5 --- lib/earmark_parser/ast/inline.ex | 17 ++++++++--- test/acceptance/ast/escape_test.exs | 2 +- test/acceptance/ast/inline_code_test.exs | 37 ++++++++++++++++++----- test/acceptance/ast/lists/spaced_test.exs | 4 +-- 4 files changed, 44 insertions(+), 16 deletions(-) diff --git a/lib/earmark_parser/ast/inline.ex b/lib/earmark_parser/ast/inline.ex index 87a8fac..f0c28d3 100644 --- a/lib/earmark_parser/ast/inline.ex +++ b/lib/earmark_parser/ast/inline.ex @@ -277,17 +277,24 @@ defmodule EarmarkParser.Ast.Inline do if match = Regex.run(code, src) do [match, _, content] = match - # Commonmark - content1 = - content - |> String.trim() - |> String.replace(~r{\s+}, " ") + content1 = normalize_code_span(content) out = codespan(content1, lnb) {behead(src, match), lnb, prepend(context, out), use_linky?} end end + defp normalize_code_span(content) do + content = String.replace(content, ~r{\r\n?|\n}, " ") + + if String.starts_with?(content, " ") and String.ends_with?(content, " ") and + String.trim(content, " ") != "" do + content |> String.slice(1, String.length(content) - 2) + else + content + end + end + def converter_for_inline_ial({src, lnb, context, use_linky?}) do inline_ial = ~r<^\s*\{:\s*(.*?)\s*}> diff --git a/test/acceptance/ast/escape_test.exs b/test/acceptance/ast/escape_test.exs index 0999681..79e66da 100644 --- a/test/acceptance/ast/escape_test.exs +++ b/test/acceptance/ast/escape_test.exs @@ -33,7 +33,7 @@ defmodule Acceptance.Ast.EscapeTest do test "less obviously - escpe the escapes" do markdown = "\\\\` code`" - ast = p(["\\", tag("code", "code", [class: "inline"], %{line: 1})]) + ast = p(["\\", tag("code", " code", [class: "inline"], %{line: 1})]) messages = [] assert as_ast(markdown) == {:ok, [ast], messages} diff --git a/test/acceptance/ast/inline_code_test.exs b/test/acceptance/ast/inline_code_test.exs index 2df66ee..e89b701 100644 --- a/test/acceptance/ast/inline_code_test.exs +++ b/test/acceptance/ast/inline_code_test.exs @@ -89,35 +89,35 @@ defmodule Acceptance.Ast.InlineCodeTest do end describe "whitespace treatment" do - test "squashing" do + test "preserves internal spaces" do markdown = "`alpha beta`" - html = "

alpha beta

\n" + html = "

alpha beta

\n" ast = parse_html(html) messages = [] assert as_ast(markdown) == {:ok, ast, messages} end - test "remove at start" do + test "preserves one-sided leading space" do markdown = "` alpha beta`" - html = "

alpha beta

\n" + html = "

alpha beta

\n" ast = parse_html(html) messages = [] assert as_ast(markdown) == {:ok, ast, messages} end - test "remove and squash" do + test "removes one space from both ends" do markdown = "` alpha beta `" - html = "

alpha beta

\n" + html = "

alpha beta

\n" ast = parse_html(html) messages = [] assert as_ast(markdown) == {:ok, ast, messages} end - test "remove and squash newlines too" do - markdown = "`\n alpha \n\n beta `" + test "converts line endings to spaces" do + markdown = "`alpha\nbeta`" html = "

alpha beta

\n" ast = parse_html(html) messages = [] @@ -125,6 +125,27 @@ defmodule Acceptance.Ast.InlineCodeTest do assert as_ast(markdown) == {:ok, ast, messages} end + test "preserves one-sided trailing space" do + markdown = "`level = `" + html = "

level =

\n" + ast = parse_html(html) + messages = [] + + assert as_ast(markdown) == {:ok, ast, messages} + end + + test "preserves spaces-only content" do + markdown = "` `" + + ast = [ + {"p", [], [{"code", [{"class", "inline"}], [" "], %{line: 1}}], %{}} + ] + + messages = [] + + assert as_ast(markdown) == {:ok, ast, messages} + end + test "inline code inside lists (was regtest #48)" do markdown = " * `a\n * b`" html = ~s[\n] diff --git a/test/acceptance/ast/lists/spaced_test.exs b/test/acceptance/ast/lists/spaced_test.exs index da45b99..ddbe41b 100644 --- a/test/acceptance/ast/lists/spaced_test.exs +++ b/test/acceptance/ast/lists/spaced_test.exs @@ -13,7 +13,7 @@ defmodule Acceptance.Ast.Lists.SpacedPendingTest do ul( li([ p("Item 1"), - p(["This is ", inline_code("pending", [], %{line: 3})]) + p(["This is ", inline_code(" pending", [], %{line: 3})]) ]) ) ] @@ -33,7 +33,7 @@ defmodule Acceptance.Ast.Lists.SpacedPendingTest do ul( li([ p("Item 1"), - p(["This is ", inline_code("pending", [], %{line: 3})]) + p(["This is ", inline_code(" pending", [], %{line: 3})]) ]) ) ] From 6b039e2a7a391a156a8f68876222dc92a5cc83c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Backstr=C3=B6m?= Date: Thu, 29 Oct 2026 11:16:44 +0100 Subject: [PATCH 2/2] Fix CI to use ubuntu 24 for Erlang 25 tests --- .github/workflows/elixir.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index c18eab7..c67120d 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -17,7 +17,7 @@ jobs: include: - elixir: 1.15.x otp: 25 - os: ubuntu-latest + os: ubuntu-24.04 warnings_as_errors: true - elixir: 1.16.x otp: 26 @@ -29,7 +29,7 @@ jobs: warnings_as_errors: true - elixir: 1.18.0-otp-25 otp: 25 - os: ubuntu-latest + os: ubuntu-24.04 warnings_as_errors: true - elixir: 1.18.0-otp-27 otp: 27