Debian Codegraph @

markup/markup_config/config.go

raw
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 "fmt"
18
19 "github.com/gohugoio/hugo/common/hmaps"
20 "github.com/gohugoio/hugo/config"
21 "github.com/gohugoio/hugo/markup/asciidocext/asciidocext_config"
22 "github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
23 "github.com/gohugoio/hugo/markup/highlight"
24 "github.com/gohugoio/hugo/markup/rst/rst_config"
25 "github.com/gohugoio/hugo/markup/tableofcontents"
26 "github.com/mitchellh/mapstructure"
27 )
28
29 type Config struct {
30 // Default markdown handler for md/markdown extensions.
31 // Default is "goldmark".
32 DefaultMarkdownHandler string
33
34 // The configuration used by code highlighters.
35 Highlight highlight.Config
36
37 // Table of contents configuration
38 TableOfContents tableofcontents.Config
39
40 // Configuration for the Goldmark markdown engine.
41 Goldmark goldmark_config.Config
42
43 // Configuration for the AsciiDoc external markdown engine.
44 AsciiDocExt asciidocext_config.Config
45
46 // Configuration for the reStructuredText external markdown engine.
47 RST rst_config.Config
48 }
49
50 func (c *Config) Init() error {
51 if err := c.Goldmark.Init(); err != nil {
52 return fmt.Errorf("goldmark: %s", err)
53 }
54 if err := c.RST.Init(); err != nil {
55 return fmt.Errorf("rst: %s", err)
56 }
57 return nil
58 }
59
60 func Decode(cfg config.Provider) (conf Config, err error) {
61 conf = Default
62
63 m := cfg.GetStringMap("markup")
64 if m == nil {
65 return
66 }
67 m = hmaps.CleanConfigStringMap(m)
68
69 normalizeConfig(m)
70
71 err = mapstructure.WeakDecode(m, &conf)
72 if err != nil {
73 return
74 }
75
76 if err = conf.Init(); err != nil {
77 return
78 }
79
80 if err = highlight.ApplyLegacyConfig(cfg, &conf.Highlight); err != nil {
81 return
82 }
83
84 return
85 }
86
87 func normalizeConfig(m map[string]any) {
88 v, err := hmaps.GetNestedParam("goldmark.parser", ".", m)
89 if err == nil {
90 vm := hmaps.ToStringMap(v)
91 // Changed from a bool in 0.81.0
92 if vv, found := vm["attribute"]; found {
93 if vvb, ok := vv.(bool); ok {
94 vm["attribute"] = goldmark_config.ParserAttribute{
95 Title: vvb,
96 }
97 }
98 }
99 }
100
101 // Handle changes to the Goldmark configuration.
102 v, err = hmaps.GetNestedParam("goldmark.extensions", ".", m)
103 if err == nil {
104 vm := hmaps.ToStringMap(v)
105
106 // We changed the typographer extension config from a bool to a struct in 0.112.0.
107 migrateGoldmarkConfig(vm, "typographer", goldmark_config.Typographer{Disable: true})
108
109 // We changed the footnote extension config from a bool to a struct in 0.151.0.
110 migrateGoldmarkConfig(vm, "footnote", goldmark_config.Footnote{Enable: false})
111 }
112 }
113
114 func migrateGoldmarkConfig(vm map[string]any, key string, falseVal any) {
115 if vv, found := vm[key]; found {
116 if vvb, ok := vv.(bool); ok {
117 if !vvb {
118 vm[key] = falseVal
119 } else {
120 delete(vm, key)
121 }
122 }
123 }
124 }
125
126 var Default = Config{
127 DefaultMarkdownHandler: "goldmark",
128
129 TableOfContents: tableofcontents.DefaultConfig,
130 Highlight: highlight.DefaultConfig,
131
132 Goldmark: goldmark_config.Default,
133 AsciiDocExt: asciidocext_config.Default,
134 RST: rst_config.Default,
135 }