Skip to content

Commit

Permalink
Fix trailing newline in code blocks
Browse files Browse the repository at this point in the history
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 <cpuguy83@gmail.com>
  • Loading branch information
cpuguy83 committed Mar 17, 2024
1 parent c2c0656 commit fd6bc09
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion md2man/roff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 6 additions & 2 deletions md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down

0 comments on commit fd6bc09

Please sign in to comment.