Skip to content

Commit 38200ab

Browse files
committed
Fix newlines around code in litgo
There were a couple of spots where lack of newlines around code was causing the markdown parser to misinterpret some markdown. This should fix that.
1 parent e24520f commit 38200ab

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

docs/book/utils/litgo/literate.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,35 @@ func extractContents(contents []byte, path string) (string, error) {
188188
out.WriteString("</summary>")
189189
}
190190
if strings.TrimSpace(pair.comment) != "" {
191+
out.WriteString("\n")
191192
out.WriteString(pair.comment)
192193
}
193194

194195
if strings.TrimSpace(pair.code) != "" {
195-
out.WriteString("\n```go\n")
196-
out.WriteString(pair.code)
197-
out.WriteString("\n```\n")
196+
out.WriteString("\n\n```go")
197+
out.WriteString(wrapWithNewlines(pair.code))
198+
out.WriteString("```\n")
198199
}
199200
if pair.collapse != ""{
200-
out.WriteString("</details>")
201+
out.WriteString("\n</details>")
201202
}
202203
// TODO(directxman12): nice side-by-side sections
203204
}
204205

205206
return out.String(), nil
206207
}
207208

209+
// wrapWithNewlines ensures that we begin and end with a newline character.
210+
func wrapWithNewlines(src string) string {
211+
if src[0] != '\n' {
212+
src = "\n" + src
213+
}
214+
if src[len(src)-1] != '\n' {
215+
src = src + "\n"
216+
}
217+
return src
218+
}
219+
208220
func main() {
209221
if err := plugin.Run(Literate{}, os.Stdin, os.Stdout, os.Args[1:]...); err != nil {
210222
log.Fatal(err.Error())

0 commit comments

Comments
 (0)