From 049899c70e74dcb94accc8fdbe4815c1aea76697 Mon Sep 17 00:00:00 2001 From: eleith Date: Mon, 30 Nov 2026 15:56:01 -0800 Subject: [PATCH] =?UTF-8?q?Use=20Markdown=20fences=20longer=20than=20the?= =?UTF-8?q?=20snippet=E2=80=99s=20backticks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + lua/scratch_comments/export/markdown.lua | 7 ++++--- tests/unit/markdown_spec.lua | 7 ++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16cf961..596891a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Remove stale quickfix entries when clearing comments without touching other lists. - Leave the quickfix list alone if it contains rows from another plugin. - Export to write-only clipboards without trying to read the clipboard back. +- Keep nested backtick fences from breaking Markdown exports. ### Changed diff --git a/lua/scratch_comments/export/markdown.lua b/lua/scratch_comments/export/markdown.lua index f3bc33c..ad68dbc 100644 --- a/lua/scratch_comments/export/markdown.lua +++ b/lua/scratch_comments/export/markdown.lua @@ -7,10 +7,11 @@ local function extension(path) end local function fence_for(text) - if (text or ""):find("```", 1, true) then - return "````" + local length = 3 + for run in (text or ""):gmatch("`+") do + length = math.max(length, #run + 1) end - return "```" + return string.rep("`", length) end ---@param views ScratchCommentView[] diff --git a/tests/unit/markdown_spec.lua b/tests/unit/markdown_spec.lua index 3e5a67c..615695f 100644 --- a/tests/unit/markdown_spec.lua +++ b/tests/unit/markdown_spec.lua @@ -72,9 +72,10 @@ describe("markdown", function() expect.no_equality(rendered:find("- line 3, col 7–11 (comment-1)", 1, true), nil) end) - it("fences a snippet that contains a fence with four backticks", function() - local rendered = markdown.render({ view({ snippet = "```\ncode\n```" }) }, {}) - expect.no_equality(rendered:find("````lua\n```\ncode\n```\n````", 1, true), nil) + it("wraps a snippet in a fence longer than any fence inside it", function() + local snippet = "`````\nmore code\n`````\n```lua\ncode\n```" + local rendered = markdown.render({ view({ snippet = snippet }) }, {}) + expect.no_equality(rendered:find("\n``````lua\n" .. snippet .. "\n``````\n", 1, true), nil) end) it("lists orphans in their own section", function() -- 2.54.1