From afdf01f4b2276a9e028f26b19911e74af26bbd5c Mon Sep 17 00:00:00 2001 From: Eric Nagel Date: Mon, 10 Jun 2024 12:22:53 +0200 Subject: [PATCH] fix: Adapt logic for formatting comments for the documentation so hash symbols can still be used for anchor links This fixes #10. The logic before removed all hash symbols, which will cause broken anchor links in the Markdown documentation. --- internal/yamlutils/yaml_utils.go | 3 +-- internal/yamlutils/yaml_utils_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/yamlutils/yaml_utils.go b/internal/yamlutils/yaml_utils.go index 2822d6f..3238e4c 100644 --- a/internal/yamlutils/yaml_utils.go +++ b/internal/yamlutils/yaml_utils.go @@ -62,11 +62,10 @@ func RemoveYamlDocumentSeparators(yamlContent []byte) []byte { // Returns: // - string: The cleaned comment string. func FormatCommentAsPlainText(comment string) string { - comment = strings.ReplaceAll(comment, "#", "") lines := strings.Split(comment, "\n") for i, line := range lines { - lines[i] = strings.TrimSpace(line) + lines[i] = strings.TrimSpace(strings.TrimPrefix(line, "#")) } cleanedComment := strings.Join(lines, "\n") diff --git a/internal/yamlutils/yaml_utils_test.go b/internal/yamlutils/yaml_utils_test.go index 42d57da..7e2b8a9 100644 --- a/internal/yamlutils/yaml_utils_test.go +++ b/internal/yamlutils/yaml_utils_test.go @@ -118,6 +118,19 @@ func TestFormatCommentAsPlainTextFormatsMultiLineComment(t *testing.T) { assert.Equal(t, expectedFormattedComment, actualFormattedComment) } +func TestFormatCommentAsPlainTextOnlyRemovesHashSymbolInTheBeginning(t *testing.T) { + t.Parallel() + + multilineComment := `# This is a comment with a # in it +# This is a comment with an [anchor link](#anchor) in it` + + expectedFormattedComment := "This is a comment with a # in it\nThis is a comment with an [anchor link](#anchor) in it" + + actualFormattedComment := FormatCommentAsPlainText(multilineComment) + + assert.Equal(t, expectedFormattedComment, actualFormattedComment) +} + func TestReadYamlFilesFromDirectoryWithNoYamlFilesReturnsNoYamlFiles(t *testing.T) { t.Parallel()