diff --git a/cmd/mungedocs/toc.go b/cmd/mungedocs/toc.go index 86f9677f634..9011e117db9 100644 --- a/cmd/mungedocs/toc.go +++ b/cmd/mungedocs/toc.go @@ -35,7 +35,8 @@ func updateTOC(filePath string, markdown []byte) ([]byte, error) { if err != nil { return nil, err } - updatedMarkdown, err := updateMacroBlock(markdown, "", "", string(toc)) + lines := splitLines(markdown) + updatedMarkdown, err := updateMacroBlock(lines, "", "", string(toc)) if err != nil { return nil, err } diff --git a/cmd/mungedocs/util.go b/cmd/mungedocs/util.go index 6a55f0aa9be..f26df9b714c 100644 --- a/cmd/mungedocs/util.go +++ b/cmd/mungedocs/util.go @@ -22,17 +22,23 @@ import ( "strings" ) -// Replaces the text between matching "beginMark" and "endMark" within "document" with "insertThis". -// -// Delimiters should occupy own line. -// Returns copy of document with modifications. -func updateMacroBlock(document []byte, beginMark, endMark, insertThis string) ([]byte, error) { - var buffer bytes.Buffer +// Splits a document up into a slice of lines. +func splitLines(document []byte) []string { lines := strings.Split(string(document), "\n") // Skip trailing empty string from Split-ing if len(lines) > 0 && lines[len(lines)-1] == "" { lines = lines[:len(lines)-1] } + return lines +} + +// Replaces the text between matching "beginMark" and "endMark" within the +// document represented by "lines" with "insertThis". +// +// Delimiters should occupy own line. +// Returns copy of document with modifications. +func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]byte, error) { + var buffer bytes.Buffer betweenBeginAndEnd := false for _, line := range lines { trimmedLine := strings.Trim(line, " \n") diff --git a/cmd/mungedocs/util_test.go b/cmd/mungedocs/util_test.go index 68454c1c779..49fbcee6381 100644 --- a/cmd/mungedocs/util_test.go +++ b/cmd/mungedocs/util_test.go @@ -34,7 +34,7 @@ func Test_updateMacroBlock(t *testing.T) { "Lorem ipsum \n BEGIN\nfoo\n\nEND\nsit amet\n"}, } for _, c := range cases { - actual, err := updateMacroBlock([]byte(c.in), "BEGIN", "END", "foo\n") + actual, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo\n") assert.NoError(t, err) if c.out != string(actual) { t.Errorf("Expected '%v' but got '%v'", c.out, string(actual)) @@ -56,7 +56,7 @@ func Test_updateMacroBlock_errors(t *testing.T) { {"BEGIN\nBEGIN\nEND\nEND"}, } for _, c := range cases { - _, err := updateMacroBlock([]byte(c.in), "BEGIN", "END", "foo") + _, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo") assert.Error(t, err) } }