github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/markup/markup_config/config.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 markup_config
    15  
    16  import (
    17  	"github.com/gohugoio/hugo/common/maps"
    18  	"github.com/gohugoio/hugo/config"
    19  	"github.com/gohugoio/hugo/docshelper"
    20  	"github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
    21  	"github.com/gohugoio/hugo/markup/blackfriday/blackfriday_config"
    22  	"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
    23  	"github.com/gohugoio/hugo/markup/highlight"
    24  	"github.com/gohugoio/hugo/markup/tableofcontents"
    25  	"github.com/gohugoio/hugo/parser"
    26  	"github.com/mitchellh/mapstructure"
    27  )
    28  
    29  type Config struct {
    30  	// Default markdown handler for md/markdown extensions.
    31  	// Default is "goldmark".
    32  	// Before Hugo 0.60 this was "blackfriday".
    33  	DefaultMarkdownHandler string
    34  
    35  	Highlight       highlight.Config
    36  	TableOfContents tableofcontents.Config
    37  
    38  	// Content renderers
    39  	Goldmark    goldmark_config.Config
    40  	BlackFriday blackfriday_config.Config
    41  
    42  	AsciidocExt asciidocext_config.Config
    43  }
    44  
    45  func Decode(cfg config.Provider) (conf Config, err error) {
    46  	conf = Default
    47  
    48  	m := cfg.GetStringMap("markup")
    49  	if m == nil {
    50  		return
    51  	}
    52  	normalizeConfig(m)
    53  
    54  	err = mapstructure.WeakDecode(m, &conf)
    55  	if err != nil {
    56  		return
    57  	}
    58  
    59  	if err = applyLegacyConfig(cfg, &conf); err != nil {
    60  		return
    61  	}
    62  
    63  	if err = highlight.ApplyLegacyConfig(cfg, &conf.Highlight); err != nil {
    64  		return
    65  	}
    66  
    67  	return
    68  }
    69  
    70  func normalizeConfig(m map[string]interface{}) {
    71  	v, err := maps.GetNestedParam("goldmark.parser", ".", m)
    72  	if err != nil {
    73  		return
    74  	}
    75  	vm := maps.ToStringMap(v)
    76  	// Changed from a bool in 0.81.0
    77  	if vv, found := vm["attribute"]; found {
    78  		if vvb, ok := vv.(bool); ok {
    79  			vm["attribute"] = goldmark_config.ParserAttribute{
    80  				Title: vvb,
    81  			}
    82  		}
    83  	}
    84  }
    85  
    86  func applyLegacyConfig(cfg config.Provider, conf *Config) error {
    87  	if bm := cfg.GetStringMap("blackfriday"); bm != nil {
    88  		// Legacy top level blackfriday config.
    89  		err := mapstructure.WeakDecode(bm, &conf.BlackFriday)
    90  		if err != nil {
    91  			return err
    92  		}
    93  	}
    94  
    95  	if conf.BlackFriday.FootnoteAnchorPrefix == "" {
    96  		conf.BlackFriday.FootnoteAnchorPrefix = cfg.GetString("footnoteAnchorPrefix")
    97  	}
    98  
    99  	if conf.BlackFriday.FootnoteReturnLinkContents == "" {
   100  		conf.BlackFriday.FootnoteReturnLinkContents = cfg.GetString("footnoteReturnLinkContents")
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  var Default = Config{
   107  	DefaultMarkdownHandler: "goldmark",
   108  
   109  	TableOfContents: tableofcontents.DefaultConfig,
   110  	Highlight:       highlight.DefaultConfig,
   111  
   112  	Goldmark:    goldmark_config.Default,
   113  	BlackFriday: blackfriday_config.Default,
   114  
   115  	AsciidocExt: asciidocext_config.Default,
   116  }
   117  
   118  func init() {
   119  	docsProvider := func() docshelper.DocProvider {
   120  		return docshelper.DocProvider{"config": map[string]interface{}{"markup": parser.LowerCaseCamelJSONMarshaller{Value: Default}}}
   121  	}
   122  	docshelper.AddDocProviderFunc(docsProvider)
   123  }