From 6fc6cd09e7a65760bf9bb29b79a57e3cc21f203c Mon Sep 17 00:00:00 2001 From: Falk Harrison Date: Tue, 29 Sep 2026 09:46:35 +0200 Subject: [PATCH] fix(convert): preserve unlabelled TLDR code blocks --- pkg/convert/convert_test.go | 18 ++++++++++++++++++ pkg/convert/tldr.go | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/convert/convert_test.go b/pkg/convert/convert_test.go index 4996c76..8a3abe6 100644 --- a/pkg/convert/convert_test.go +++ b/pkg/convert/convert_test.go @@ -566,6 +566,24 @@ tar -czvf {{path/to/archive.tar.gz}} {{path/to/directory}} } } +func TestConvertTldrBareFencePreservesMultilineCommandAndFollowingExample(t *testing.T) { + input := "# echo\n\n" + + "- Print two greetings:\n\n```\necho hello\necho world\n```\n\n" + + "- Print farewell:\n\n`echo goodbye`\n" + + converted, err := ConvertTldr(input, "echo.md") + if err != nil { + t.Fatalf("ConvertTldr failed: %v", err) + } + + want := "# Converted TLDR for Echo\n\n" + + "## Print two greetings\n\n```sh\necho hello\necho world\n```\n\n" + + "## Print farewell\n\n```sh\necho goodbye\n```\n\n" + if converted != want { + t.Errorf("ConvertTldr output mismatch\ngot:\n%s\nwant:\n%s", converted, want) + } +} + // ============================================================================ // TLDR placeholder-shape tests // ============================================================================ diff --git a/pkg/convert/tldr.go b/pkg/convert/tldr.go index 54dcde8..b90d935 100644 --- a/pkg/convert/tldr.go +++ b/pkg/convert/tldr.go @@ -67,13 +67,13 @@ func (p *tldrParser) parseLine(lines []string, index int) int { // `\`\`\`sh ... \`\`\“ form (used by some real-world pages). func readCommand(lines []string, index int) (cmd string, consumed int) { line := strings.TrimSpace(lines[index]) - if strings.HasPrefix(line, "`") && strings.HasSuffix(line, "`") && len(line) >= 2 { - return line[1 : len(line)-1], index - } if strings.HasPrefix(line, "```") { body, end := consumeCodeBlock(lines, index+1) return body, end } + if strings.HasPrefix(line, "`") && strings.HasSuffix(line, "`") && len(line) >= 2 { + return line[1 : len(line)-1], index + } return "", index }