github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/analyze/code_langauge.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package analyze
     7  
     8  import (
     9  	"path/filepath"
    10  
    11  	"github.com/go-enry/go-enry/v2"
    12  )
    13  
    14  // GetCodeLanguage detects code language based on file name and content
    15  func GetCodeLanguage(filename string, content []byte) string {
    16  	if language, ok := enry.GetLanguageByExtension(filename); ok {
    17  		return language
    18  	}
    19  
    20  	if language, ok := enry.GetLanguageByFilename(filename); ok {
    21  		return language
    22  	}
    23  
    24  	if len(content) == 0 {
    25  		return enry.OtherLanguage
    26  	}
    27  
    28  	return enry.GetLanguage(filepath.Base(filename), content)
    29  }