github.com/dominikszabo/hugo-ds-clean@v0.47.1/helpers/docshelper.go (about)

     1  package helpers
     2  
     3  import (
     4  	"path/filepath"
     5  	"sort"
     6  	"strings"
     7  
     8  	"github.com/alecthomas/chroma/lexers"
     9  	"github.com/gohugoio/hugo/docshelper"
    10  )
    11  
    12  // This is is just some helpers used to create some JSON used in the Hugo docs.
    13  func init() {
    14  
    15  	docsProvider := func() map[string]interface{} {
    16  		docs := make(map[string]interface{})
    17  
    18  		var chromaLexers []interface{}
    19  
    20  		sort.Sort(lexers.Registry.Lexers)
    21  
    22  		for _, l := range lexers.Registry.Lexers {
    23  
    24  			config := l.Config()
    25  
    26  			var filenames []string
    27  			filenames = append(filenames, config.Filenames...)
    28  			filenames = append(filenames, config.AliasFilenames...)
    29  
    30  			aliases := config.Aliases
    31  
    32  			for _, filename := range filenames {
    33  				alias := strings.TrimSpace(strings.TrimPrefix(filepath.Ext(filename), "."))
    34  				if alias != "" {
    35  					aliases = append(aliases, alias)
    36  				}
    37  			}
    38  
    39  			sort.Strings(aliases)
    40  			aliases = UniqueStrings(aliases)
    41  
    42  			lexerEntry := struct {
    43  				Name    string
    44  				Aliases []string
    45  			}{
    46  				config.Name,
    47  				aliases,
    48  			}
    49  
    50  			chromaLexers = append(chromaLexers, lexerEntry)
    51  
    52  			docs["lexers"] = chromaLexers
    53  		}
    54  		return docs
    55  
    56  	}
    57  
    58  	docshelper.AddDocProvider("chroma", docsProvider)
    59  }