From 7e055e782f13faaf337f156f2bb82627da9bc979 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Fri, 28 Apr 2023 15:02:50 +0200 Subject: [PATCH] fix newline handling for markdown creation Signed-off-by: jkoberg --- ocis-pkg/markdown/markdown.go | 42 ++++++++++++++++-------------- ocis-pkg/markdown/markdown_test.go | 10 +++---- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/ocis-pkg/markdown/markdown.go b/ocis-pkg/markdown/markdown.go index c95391ce7..66b3dd1c8 100644 --- a/ocis-pkg/markdown/markdown.go +++ b/ocis-pkg/markdown/markdown.go @@ -50,28 +50,20 @@ func (md MD) TocString() string { // WriteContent writes the MDs content to the given writer func (md MD) WriteContent(w io.Writer) (int64, error) { - max, written := len(md.Headings), int64(0) + written := int64(0) write := func(s string) error { n, err := w.Write([]byte(s)) written += int64(n) return err } - for i, h := range md.Headings { + for _, h := range md.Headings { if err := write(strings.Repeat("#", h.Level) + " " + h.Header + "\n"); err != nil { return written, err } - if err := write("\n"); err != nil { - return written, err - } if len(h.Content) > 0 { if err := write(h.Content); err != nil { return written, err } - if i < max-1 { - if err := write("\n"); err != nil { - return written, err - } - } } } return written, nil @@ -110,20 +102,30 @@ func NewMD(b []byte) MD { content = strings.Builder{} } } - parts := strings.Split(string(b), "\n") - for _, p := range parts { - if p == "" { + parts := strings.Split("\n"+string(b), "\n#") + numparts := len(parts) - 1 + for i, p := range parts { + if i == 0 { + // omit part before first heading continue } - if p[:1] == "#" { // this is a header - sendHeading() - heading = headingFromString(p) - } else { - // readd lost "\n" - _, _ = content.WriteString(p + "\n") + + all := strings.SplitN(p, "\n", 2) + if len(all) != 2 { + continue } + + head, con := all[0], all[1] + // readd lost "#" + heading = headingFromString("#" + head) + _, _ = content.WriteString(con) + // readd lost "\n" - omit for last part + if i < numparts { + _, _ = content.WriteString("\n") + } + // add heading + sendHeading() } - sendHeading() return md } diff --git a/ocis-pkg/markdown/markdown_test.go b/ocis-pkg/markdown/markdown_test.go index 615bb3441..a6aea313c 100644 --- a/ocis-pkg/markdown/markdown_test.go +++ b/ocis-pkg/markdown/markdown_test.go @@ -15,19 +15,19 @@ some abstract description subtitle one description ## SubTitle 2 - subtitle two description - ### Subpoint to SubTitle 2 description to subpoint + +more text ` SmallMD = MD{ Headings: []Heading{ - {Level: 1, Header: "Title", Content: "some abstract description\n"}, - {Level: 2, Header: "SubTitle 1", Content: "subtitle one description\n"}, + {Level: 1, Header: "Title", Content: "\nsome abstract description\n\n"}, + {Level: 2, Header: "SubTitle 1", Content: "\nsubtitle one description\n\n"}, {Level: 2, Header: "SubTitle 2", Content: "subtitle two description\n"}, - {Level: 3, Header: "Subpoint to SubTitle 2", Content: "description to subpoint\n"}, + {Level: 3, Header: "Subpoint to SubTitle 2", Content: "\ndescription to subpoint\n\nmore text\n"}, }, } )