Repository: go-gitea/gitea
File: modules/highlight/highlight.go (Go)

Code before:
		return PlainText(code), "", nil
	}

	formatter := html.New(html.WithClasses(true),
		html.WithLineNumbers(false),
		html.PreventSurroundingPre(true),
	)

	var lexer chroma.Lexer

	// provided language overrides everything
	if language != "" {
		lexer = lexers.Get(language)
	}

	if lexer == nil {
		if val, ok := highlightMapping[filepath.Ext(fileName)]; ok {
			lexer = lexers.Get(val)
		}
	}

	if lexer == nil {
		guessLanguage := analyze.GetCodeLanguage(fileName, code)
		if normalizedGuessLanguage, ok := normalizeEnryToChroma[guessLanguage]; ok {
			guessLanguage = normalizedGuessLanguage
		}

		lexer = lexers.Get(guessLanguage)
		if lexer == nil {
			lexer = lexers.Match(fileName)
			if lexer == nil {
				lexer = lexers.Fallback
			}
		}
	}

	lexerName := formatLexerName(lexer.Config().Name)

	iterator, err := lexer.Tokenise(nil, string(code))
	if err != nil {
		return nil, "", fmt.Errorf("can't tokenize code: %w", err)
	}

	tokensLines := chroma.SplitTokensIntoLines(iterator.Tokens())
	htmlBuf := &bytes.Buffer{}

	lines := make([]template.HTML, 0, len(tokensLines))
	for _, tokens := range tokensLines {
		iterator = chroma.Literator(tokens...)
		err = formatter.Format(htmlBuf, githubStyles, iterator)
		if err != nil {

Review comment (suggestion):
~~It seems that the logic should be in `analyze.GetCodeLanguage`, and test that function.~~

A function like `GetCodeLanguageForChroma` or `GetCodeLanguageForHighlighting` should be better.

Code after:
	if err != nil {
		log.Error("Can't format code: %v", err)
		return template.HTML(template.HTMLEscapeString(code))
	}

	_ = htmlw.Flush()
	// Chroma will add newlines for certain lexers in order to highlight them properly
	// Once highlighted, strip them here, so they don't cause copy/paste trouble in HTML output
	return template.HTML(strings.TrimSuffix(htmlbuf.String(), "\n"))
}

// RenderFullFile returns a slice of chroma syntax highlighted HTML lines of code and the matched lexer name
func RenderFullFile(fileName, language string, code []byte) ([]template.HTML, string, error) {
	if len(code) > sizeLimit {
		return RenderPlainText(code), "", nil
	}

	formatter := html.New(html.WithClasses(true),
		html.WithLineNumbers(false),
		html.PreventSurroundingPre(true),
	)

	lexer := GetChromaLexerWithFallback(fileName, language, code)
	lexerName := formatLexerName(lexer.Config().Name)

	iterator, err := lexer.Tokenise(nil, string(code))
	if err != nil {
		return nil, "", fmt.Errorf("can't tokenize code: %w", err)
	}

	tokensLines := chroma.SplitTokensIntoLines(iterator.Tokens())
	htmlBuf := &bytes.Buffer{}

	lines := make([]template.HTML, 0, len(tokensLines))
	for _, tokens := range tokensLines {
		iterator = chroma.Literator(tokens...)
		err = formatter.Format(htmlBuf, globalVars().githubStyles, iterator)
		if err != nil {
			return nil, "", fmt.Errorf("can't format code: %w", err)
		}
		lines = append(lines, template.HTML(htmlBuf.String()))
		htmlBuf.Reset()
	}

	return lines, lexerName, nil
}

// RenderPlainText returns non-highlighted HTML for code
func RenderPlainText(code []byte) []template.HTML {
	r := bufio.NewReader(bytes.NewReader(code))
	m := make([]template.HTML, 0, bytes.Count(code, []byte{'\n'})+1)