github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/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/goldmark/goldmark_config"
    22  	"github.com/gohugoio/hugo/markup/highlight"
    23  	"github.com/gohugoio/hugo/markup/tableofcontents"
    24  	"github.com/gohugoio/hugo/parser"
    25  	"github.com/mitchellh/mapstructure"
    26  )
    27  
    28  type Config struct {
    29  	// Default markdown handler for md/markdown extensions.
    30  	// Default is "goldmark".
    31  	// Before Hugo 0.60 this was "blackfriday".
    32  	DefaultMarkdownHandler string
    33  
    34  	Highlight       highlight.Config
    35  	TableOfContents tableofcontents.Config
    36  
    37  	// Content renderers
    38  	Goldmark    goldmark_config.Config
    39  	AsciidocExt asciidocext_config.Config
    40  }
    41  
    42  func Decode(cfg config.Provider) (conf Config, err error) {
    43  	conf = Default
    44  
    45  	m := cfg.GetStringMap("markup")
    46  	if m == nil {
    47  		return
    48  	}
    49  	normalizeConfig(m)
    50  
    51  	err = mapstructure.WeakDecode(m, &conf)
    52  	if err != nil {
    53  		return
    54  	}
    55  
    56  	if err = highlight.ApplyLegacyConfig(cfg, &conf.Highlight); err != nil {
    57  		return
    58  	}
    59  
    60  	return
    61  }
    62  
    63  func normalizeConfig(m map[string]any) {
    64  	v, err := maps.GetNestedParam("goldmark.parser", ".", m)
    65  	if err != nil {
    66  		return
    67  	}
    68  	vm := maps.ToStringMap(v)
    69  	// Changed from a bool in 0.81.0
    70  	if vv, found := vm["attribute"]; found {
    71  		if vvb, ok := vv.(bool); ok {
    72  			vm["attribute"] = goldmark_config.ParserAttribute{
    73  				Title: vvb,
    74  			}
    75  		}
    76  	}
    77  }
    78  
    79  var Default = Config{
    80  	DefaultMarkdownHandler: "goldmark",
    81  
    82  	TableOfContents: tableofcontents.DefaultConfig,
    83  	Highlight:       highlight.DefaultConfig,
    84  
    85  	Goldmark:    goldmark_config.Default,
    86  	AsciidocExt: asciidocext_config.Default,
    87  }
    88  
    89  func init() {
    90  	docsProvider := func() docshelper.DocProvider {
    91  		return docshelper.DocProvider{"config": map[string]any{"markup": parser.LowerCaseCamelJSONMarshaller{Value: Default}}}
    92  	}
    93  	docshelper.AddDocProviderFunc(docsProvider)
    94  }