github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/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/markup/asciidocext/asciidocext_config"
    20  	"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
    21  	"github.com/gohugoio/hugo/markup/highlight"
    22  	"github.com/gohugoio/hugo/markup/tableofcontents"
    23  	"github.com/mitchellh/mapstructure"
    24  )
    25  
    26  type Config struct {
    27  	// Default markdown handler for md/markdown extensions.
    28  	// Default is "goldmark".
    29  	DefaultMarkdownHandler string
    30  
    31  	// The configuration used by code highlighters.
    32  	Highlight highlight.Config
    33  
    34  	// Table of contents configuration
    35  	TableOfContents tableofcontents.Config
    36  
    37  	// Configuration for the Goldmark markdown engine.
    38  	Goldmark goldmark_config.Config
    39  
    40  	// Configuration for the Asciidoc external markdown engine.
    41  	AsciidocExt asciidocext_config.Config
    42  }
    43  
    44  func Decode(cfg config.Provider) (conf Config, err error) {
    45  	conf = Default
    46  
    47  	m := cfg.GetStringMap("markup")
    48  	if m == nil {
    49  		return
    50  	}
    51  	m = maps.CleanConfigStringMap(m)
    52  
    53  	normalizeConfig(m)
    54  
    55  	err = mapstructure.WeakDecode(m, &conf)
    56  	if err != nil {
    57  		return
    58  	}
    59  
    60  	if err = highlight.ApplyLegacyConfig(cfg, &conf.Highlight); err != nil {
    61  		return
    62  	}
    63  
    64  	return
    65  }
    66  
    67  func normalizeConfig(m map[string]any) {
    68  	v, err := maps.GetNestedParam("goldmark.parser", ".", m)
    69  	if err == nil {
    70  		vm := maps.ToStringMap(v)
    71  		// Changed from a bool in 0.81.0
    72  		if vv, found := vm["attribute"]; found {
    73  			if vvb, ok := vv.(bool); ok {
    74  				vm["attribute"] = goldmark_config.ParserAttribute{
    75  					Title: vvb,
    76  				}
    77  			}
    78  		}
    79  	}
    80  
    81  	// Changed from a bool in 0.112.0.
    82  	v, err = maps.GetNestedParam("goldmark.extensions", ".", m)
    83  	if err == nil {
    84  		vm := maps.ToStringMap(v)
    85  		const typographerKey = "typographer"
    86  		if vv, found := vm[typographerKey]; found {
    87  			if vvb, ok := vv.(bool); ok {
    88  				if !vvb {
    89  					vm[typographerKey] = goldmark_config.Typographer{
    90  						Disable: true,
    91  					}
    92  				} else {
    93  					delete(vm, typographerKey)
    94  				}
    95  			}
    96  		}
    97  	}
    98  }
    99  
   100  var Default = Config{
   101  	DefaultMarkdownHandler: "goldmark",
   102  
   103  	TableOfContents: tableofcontents.DefaultConfig,
   104  	Highlight:       highlight.DefaultConfig,
   105  
   106  	Goldmark:    goldmark_config.Default,
   107  	AsciidocExt: asciidocext_config.Default,
   108  }