History

Server-side syntax highlighting for all code (#12047)

* Server-side syntax hilighting for all code This PR does a few things: * Remove all traces of highlight.js * Use chroma library to provide fast syntax hilighting directly on the server * Provide syntax hilighting for diffs * Re-style both unified and split diffs views * Add custom syntax hilighting styling for both regular and arc-green Fixes #7729 Fixes #10157 Fixes #11825 Fixes #7728 Fixes #3872 Fixes #3682 And perhaps gets closer to #9553 * fix line marker * fix repo search * Fix single line select * properly load settings * npm uninstall highlight.js * review suggestion * code review * forgot to call function * fix test * Apply suggestions from code review suggestions from @silverwind thanks Co-authored-by: silverwind <me@silverwind.io> * code review * copy/paste error * Use const for highlight size limit * Update web_src/less/_repository.less Co-authored-by: Lauris BH <lauris@nix.lv> * update size limit to 1MB and other styling tweaks * fix highlighting for certain diff sections * fix test * add worker back as suggested

mrsdizzie committed 6 years ago

Commit af7ffaa1 parent ce5f2b9336 files · +37289 −765Browse files

Showing the changes to services/gitdiff/gitdiff.go (1 of 336 files). Show all

Unified · Split

services/gitdiff/gitdiff.go+23−24
@@ -10,7 +10,6 @@ import ( · 9 unmodified lines
1010 "bytes"
1111 "context"
1212 "fmt"
13− "html"
1413 "html/template"
1514 "io"
1615 "io/ioutil"
@@ -164,15 +163,16 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int · 147 unmodified lines
164163 // escape a line's content or return <br> needed for copy/paste purposes
165164 func getLineContent(content string) string {
166165 if len(content) > 0 {
167− return html.EscapeString(content)
166+ return content
168167 }
169− return "<br>"
168+ return "\n"
170169 }
171170
172171 // DiffSection represents a section of a DiffFile.
173172 type DiffSection struct {
174− Name string
175− Lines []*DiffLine
173+ FileName string
174+ Name string
175+ Lines []*DiffLine
176176 }
177177
178178 var (
@@ -181,24 +181,23 @@ var ( · 2 unmodified lines
181181 codeTagSuffix = []byte(`</span>`)
182182 )
183183
184−func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
184+func diffToHTML(fileName string, diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
185185 buf := bytes.NewBuffer(nil)
186186
187187 for i := range diffs {
188188 switch {
189189 case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd:
190190 buf.Write(addedCodePrefix)
191− buf.WriteString(getLineContent(diffs[i].Text))
191+ buf.WriteString(highlight.Code(fileName, diffs[i].Text))
192192 buf.Write(codeTagSuffix)
193193 case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
194194 buf.Write(removedCodePrefix)
195− buf.WriteString(getLineContent(diffs[i].Text))
195+ buf.WriteString(highlight.Code(fileName, diffs[i].Text))
196196 buf.Write(codeTagSuffix)
197197 case diffs[i].Type == diffmatchpatch.DiffEqual:
198− buf.WriteString(getLineContent(diffs[i].Text))
198+ buf.WriteString(highlight.Code(fileName, getLineContent(diffs[i].Text)))
199199 }
200200 }
201−
202201 return template.HTML(buf.Bytes())
203202 }
204203
@@ -256,6 +255,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem · 51 unmodified lines
256255 if setting.Git.DisableDiffHighlight {
257256 return template.HTML(getLineContent(diffLine.Content[1:]))
258257 }
258+
259259 var (
260260 compareDiffLine *DiffLine
261261 diff1 string
@@ -264,31 +264,32 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem · 2 unmodified lines
264264
265265 // try to find equivalent diff line. ignore, otherwise
266266 switch diffLine.Type {
267+ case DiffLineSection:
268+ return template.HTML(getLineContent(diffLine.Content[1:]))
267269 case DiffLineAdd:
268270 compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
269271 if compareDiffLine == nil {
270− return template.HTML(getLineContent(diffLine.Content[1:]))
272+ return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n"))
271273 }
272274 diff1 = compareDiffLine.Content
273275 diff2 = diffLine.Content
274276 case DiffLineDel:
275277 compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
276278 if compareDiffLine == nil {
277− return template.HTML(getLineContent(diffLine.Content[1:]))
279+ return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n"))
278280 }
279281 diff1 = diffLine.Content
280282 diff2 = compareDiffLine.Content
281283 default:
282284 if strings.IndexByte(" +-", diffLine.Content[0]) > -1 {
283− return template.HTML(getLineContent(diffLine.Content[1:]))
285+ return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content[1:]+"\n"))
284286 }
285− return template.HTML(getLineContent(diffLine.Content))
287+ return template.HTML(highlight.Code(diffSection.FileName, diffLine.Content))
286288 }
287289
288290 diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true)
289291 diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
290−
291− return diffToHTML(diffRecord, diffLine.Type)
292+ return diffToHTML(diffSection.FileName, diffRecord, diffLine.Type)
292293 }
293294
294295 // DiffFile represents a file diff.
@@ -313,11 +314,6 @@ func (diffFile *DiffFile) GetType() int { · 18 unmodified lines
313314 return int(diffFile.Type)
314315 }
315316
316−// GetHighlightClass returns highlight class for a filename.
317−func (diffFile *DiffFile) GetHighlightClass() string {
318− return highlight.FileNameToHighlightClass(diffFile.Name)
319−}
320−
321317 // GetTailSection creates a fake DiffLineSection if the last section is not the end of the file
322318 func (diffFile *DiffFile) GetTailSection(gitRepo *git.Repository, leftCommitID, rightCommitID string) *DiffSection {
323319 if len(diffFile.Sections) == 0 || diffFile.Type != DiffFileChange || diffFile.IsBin || diffFile.IsLFSFile {
@@ -348,7 +344,7 @@ func (diffFile *DiffFile) GetTailSection(gitRepo *git.Repository, leftCommitID, · 24 unmodified lines
348344 LeftIdx: leftLineCount,
349345 RightIdx: rightLineCount,
350346 }}
351− tailSection := &DiffSection{Lines: []*DiffLine{tailDiffLine}}
347+ tailSection := &DiffSection{FileName: diffFile.Name, Lines: []*DiffLine{tailDiffLine}}
352348 return tailSection
353349
354350 }
@@ -405,8 +401,7 @@ const cmdDiffHead = "diff --git " · 50 unmodified lines
405401 // TODO: move this function to gogits/git-module
406402 func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*Diff, error) {
407403 var (
408− diff = &Diff{Files: make([]*DiffFile, 0)}
409−
404+ diff = &Diff{Files: make([]*DiffFile, 0)}
410405 curFile = &DiffFile{}
411406 curSection = &DiffSection{
412407 Lines: make([]*DiffLine, 0, 10),
@@ -481,6 +476,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D · 68 unmodified lines
481476 leftLine++
482477 rightLine++
483478 curSection.Lines = append(curSection.Lines, diffLine)
479+ curSection.FileName = curFile.Name
484480 continue
485481 case line[0] == '@':
486482 curSection = &DiffSection{}
@@ -492,6 +488,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D · 5 unmodified lines
492488 SectionInfo: lineSectionInfo,
493489 }
494490 curSection.Lines = append(curSection.Lines, diffLine)
491+ curSection.FileName = curFile.Name
495492 // update line number.
496493 leftLine = lineSectionInfo.LeftIdx
497494 rightLine = lineSectionInfo.RightIdx
@@ -502,6 +499,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D · 4 unmodified lines
502499 diffLine := &DiffLine{Type: DiffLineAdd, Content: line, RightIdx: rightLine}
503500 rightLine++
504501 curSection.Lines = append(curSection.Lines, diffLine)
502+ curSection.FileName = curFile.Name
505503 continue
506504 case line[0] == '-':
507505 curFile.Deletion++
@@ -511,6 +509,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D · 3 unmodified lines
511509 leftLine++
512510 }
513511 curSection.Lines = append(curSection.Lines, diffLine)
512+ curSection.FileName = curFile.Name
514513 case strings.HasPrefix(line, "Binary"):
515514 curFile.IsBin = true
516515 continue