From fd6bc094ed445b6954a67df55e75d7db95fa8879 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Mon, 5 Feb 2024 01:23:05 +0000 Subject: [PATCH] Fix trailing newline in code blocks Given this input: > ``` > some code > ``` We were adding an additional newline character before the code close tag even though the code block by definition already includes that newline. e.g., blackfriday is hadning us `some code\n` as the literal content of the code block. Signed-off-by: Brian Goff --- md2man/roff.go | 2 +- md2man/roff_test.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/md2man/roff.go b/md2man/roff.go index 4174df9..37c4c34 100644 --- a/md2man/roff.go +++ b/md2man/roff.go @@ -38,7 +38,7 @@ const ( codespanTag = "\\fB" codespanCloseTag = "\\fR" codeTag = "\n.EX\n" - codeCloseTag = "\n.EE\n" + codeCloseTag = ".EE\n" // Do not prepend a newline character since code blocks, by definition, include a newline already (or at least as how blackfriday gives us on). quoteTag = "\n.PP\n.RS\n" quoteCloseTag = "\n.RE\n" listTag = "\n.RS\n" diff --git a/md2man/roff_test.go b/md2man/roff_test.go index d421d6b..6b44ebe 100644 --- a/md2man/roff_test.go +++ b/md2man/roff_test.go @@ -13,10 +13,14 @@ type TestParams struct { func TestCodeBlocks(t *testing.T) { tests := []string{ "```\nsome code\n```\n", - ".nh\n\n.EX\nsome code\n\n.EE\n", + ".nh\n\n.EX\nsome code\n.EE\n", "```bash\necho foo\n```\n", - ".nh\n\n.EX\necho foo\n\n.EE\n", + ".nh\n\n.EX\necho foo\n.EE\n", + + // make sure literal new lines surrounding the markdown block are preserved as they are intentional + "```bash\n\nsome code\n\n```", + ".nh\n\n.EX\n\nsome code\n\n.EE\n", } doTestsParam(t, tests, TestParams{blackfriday.FencedCode}) }