Skip to content

Commit

Permalink
Properly fix extra newline rendered at the beginning of code segments
Browse files Browse the repository at this point in the history
  • Loading branch information
eliben committed Aug 21, 2023
1 parent 83763d9 commit 57db236
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions tools/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func parseSegs(sourcePath string) ([]*Seg, string) {
lines = append(lines, strings.Replace(line, "\t", " ", -1))
source = append(source, line)
}
filecontent := strings.Join(source, "\n")
segs := []*Seg{}
lastSeen := ""
for _, line := range lines {
Expand Down Expand Up @@ -175,7 +174,12 @@ func parseSegs(sourcePath string) ([]*Seg, string) {
newSeg := Seg{Docs: "", Code: line}
segs = append(segs, &newSeg)
} else {
segs[len(segs)-1].Code = segs[len(segs)-1].Code + "\n" + line
lastSeg := segs[len(segs)-1]
if len(lastSeg.Code) == 0 {
lastSeg.Code = line
} else {
lastSeg.Code = lastSeg.Code + "\n" + line
}
}
debug("CODE: " + line)
lastSeen = "code"
Expand All @@ -186,7 +190,7 @@ func parseSegs(sourcePath string) ([]*Seg, string) {
seg.CodeLeading = (i < (len(segs) - 1))
seg.CodeRun = strings.Contains(seg.Code, "package main")
}
return segs, filecontent
return segs, strings.Join(source, "\n")
}

func chromaFormat(code, filePath string) string {
Expand Down Expand Up @@ -222,12 +226,6 @@ func parseAndRenderSegs(sourcePath string) ([]*Seg, string) {
seg.DocsRendered = markdown(seg.Docs)
}
if seg.Code != "" {
// TODO: with the update to Chroma 2.8.0 I had to change this, because the
// new chromoa would render this leading newline for all code segments,
// which would then be shifted down compared to the doc segments. Maybe
// the parsing should be modified to not produce this \n in the first
// place?
seg.Code = strings.TrimLeft(seg.Code, "\n")
seg.CodeRendered = chromaFormat(seg.Code, sourcePath)

// adding the content to the js code for copying to the clipboard
Expand Down

0 comments on commit 57db236

Please sign in to comment.