github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/config/services/servicesConfig.go (about)

     1  // Copyright 2019 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package services
    15  
    16  import (
    17  	"github.com/gohugoio/hugo/config"
    18  	"github.com/mitchellh/mapstructure"
    19  )
    20  
    21  const (
    22  	servicesConfigKey = "services"
    23  
    24  	disqusShortnameKey = "disqusshortname"
    25  	googleAnalyticsKey = "googleanalytics"
    26  	rssLimitKey        = "rssLimit"
    27  )
    28  
    29  // Config is a privacy configuration for all the relevant services in Hugo.
    30  type Config struct {
    31  	Disqus          Disqus
    32  	GoogleAnalytics GoogleAnalytics
    33  	Instagram       Instagram
    34  	Twitter         Twitter
    35  	RSS             RSS
    36  }
    37  
    38  // Disqus holds the functional configuration settings related to the Disqus template.
    39  type Disqus struct {
    40  	// A Shortname is the unique identifier assigned to a Disqus site.
    41  	Shortname string
    42  }
    43  
    44  // GoogleAnalytics holds the functional configuration settings related to the Google Analytics template.
    45  type GoogleAnalytics struct {
    46  	// The GA tracking ID.
    47  	ID string
    48  }
    49  
    50  // Instagram holds the functional configuration settings related to the Instagram shortcodes.
    51  type Instagram struct {
    52  	// The Simple variant of the Instagram is decorated with Bootstrap 4 card classes.
    53  	// This means that if you use Bootstrap 4 or want to provide your own CSS, you want
    54  	// to disable the inline CSS provided by Hugo.
    55  	DisableInlineCSS bool
    56  
    57  	// App or Client Access Token.
    58  	// If you are using a Client Access Token, remember that you must combine it with your App ID
    59  	// using a pipe symbol (<APPID>|<CLIENTTOKEN>) otherwise the request will fail.
    60  	AccessToken string
    61  }
    62  
    63  // Twitter holds the functional configuration settings related to the Twitter shortcodes.
    64  type Twitter struct {
    65  	// The Simple variant of Twitter is decorated with a basic set of inline styles.
    66  	// This means that if you want to provide your own CSS, you want
    67  	// to disable the inline CSS provided by Hugo.
    68  	DisableInlineCSS bool
    69  }
    70  
    71  // RSS holds the functional configuration settings related to the RSS feeds.
    72  type RSS struct {
    73  	// Limit the number of pages.
    74  	Limit int
    75  }
    76  
    77  // DecodeConfig creates a services Config from a given Hugo configuration.
    78  func DecodeConfig(cfg config.Provider) (c Config, err error) {
    79  	m := cfg.GetStringMap(servicesConfigKey)
    80  
    81  	err = mapstructure.WeakDecode(m, &c)
    82  
    83  	// Keep backwards compatibility.
    84  	if c.GoogleAnalytics.ID == "" {
    85  		// Try the global config
    86  		c.GoogleAnalytics.ID = cfg.GetString(googleAnalyticsKey)
    87  	}
    88  	if c.Disqus.Shortname == "" {
    89  		c.Disqus.Shortname = cfg.GetString(disqusShortnameKey)
    90  	}
    91  
    92  	if c.RSS.Limit == 0 {
    93  		c.RSS.Limit = cfg.GetInt(rssLimitKey)
    94  	}
    95  
    96  	return
    97  }