@@ -7,32 +7,42 @@ import (
7
7
"fmt"
8
8
"strings"
9
9
10
+ "code.gitea.io/gitea/modules/markup"
11
+
10
12
"github.com/yuin/goldmark/ast"
11
13
"gopkg.in/yaml.v3"
12
14
)
13
15
14
16
// RenderConfig represents rendering configuration for this file
15
17
type RenderConfig struct {
16
- Meta string
18
+ Meta markup. RenderMetaMode
17
19
Icon string
18
- TOC bool
20
+ TOC string // "false": hide, "side"/empty: in sidebar, "main"/"true": in main view
19
21
Lang string
20
22
yamlNode * yaml.Node
21
23
}
22
24
25
+ func renderMetaModeFromString (s string ) markup.RenderMetaMode {
26
+ switch strings .TrimSpace (strings .ToLower (s )) {
27
+ case "none" :
28
+ return markup .RenderMetaAsNone
29
+ case "table" :
30
+ return markup .RenderMetaAsTable
31
+ default : // "details"
32
+ return markup .RenderMetaAsDetails
33
+ }
34
+ }
35
+
23
36
// UnmarshalYAML implement yaml.v3 UnmarshalYAML
24
37
func (rc * RenderConfig ) UnmarshalYAML (value * yaml.Node ) error {
25
38
if rc == nil {
26
- rc = & RenderConfig {
27
- Meta : "table" ,
28
- Icon : "table" ,
29
- Lang : "" ,
30
- }
39
+ return nil
31
40
}
41
+
32
42
rc .yamlNode = value
33
43
34
44
type commonRenderConfig struct {
35
- TOC bool `yaml:"include_toc"`
45
+ TOC string `yaml:"include_toc"`
36
46
Lang string `yaml:"lang"`
37
47
}
38
48
var basic commonRenderConfig
@@ -54,58 +64,45 @@ func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
54
64
55
65
if err := value .Decode (& stringBasic ); err == nil {
56
66
if stringBasic .Gitea != "" {
57
- switch strings .TrimSpace (strings .ToLower (stringBasic .Gitea )) {
58
- case "none" :
59
- rc .Meta = "none"
60
- case "table" :
61
- rc .Meta = "table"
62
- default : // "details"
63
- rc .Meta = "details"
64
- }
67
+ rc .Meta = renderMetaModeFromString (stringBasic .Gitea )
65
68
}
66
69
return nil
67
70
}
68
71
69
- type giteaControl struct {
72
+ type yamlRenderConfig struct {
70
73
Meta * string `yaml:"meta"`
71
74
Icon * string `yaml:"details_icon"`
72
- TOC * bool `yaml:"include_toc"`
75
+ TOC * string `yaml:"include_toc"`
73
76
Lang * string `yaml:"lang"`
74
77
}
75
78
76
- type complexGiteaConfig struct {
77
- Gitea * giteaControl `yaml:"gitea"`
79
+ type yamlRenderConfigWrapper struct {
80
+ Gitea * yamlRenderConfig `yaml:"gitea"`
78
81
}
79
- var complex complexGiteaConfig
80
- if err := value .Decode (& complex ); err != nil {
81
- return fmt .Errorf ("unable to decode into complexRenderConfig %w" , err )
82
+
83
+ var cfg yamlRenderConfigWrapper
84
+ if err := value .Decode (& cfg ); err != nil {
85
+ return fmt .Errorf ("unable to decode into yamlRenderConfigWrapper %w" , err )
82
86
}
83
87
84
- if complex .Gitea == nil {
88
+ if cfg .Gitea == nil {
85
89
return nil
86
90
}
87
91
88
- if complex .Gitea .Meta != nil {
89
- switch strings .TrimSpace (strings .ToLower (* complex .Gitea .Meta )) {
90
- case "none" :
91
- rc .Meta = "none"
92
- case "table" :
93
- rc .Meta = "table"
94
- default : // "details"
95
- rc .Meta = "details"
96
- }
92
+ if cfg .Gitea .Meta != nil {
93
+ rc .Meta = renderMetaModeFromString (* cfg .Gitea .Meta )
97
94
}
98
95
99
- if complex .Gitea .Icon != nil {
100
- rc .Icon = strings .TrimSpace (strings .ToLower (* complex .Gitea .Icon ))
96
+ if cfg .Gitea .Icon != nil {
97
+ rc .Icon = strings .TrimSpace (strings .ToLower (* cfg .Gitea .Icon ))
101
98
}
102
99
103
- if complex .Gitea .Lang != nil && * complex .Gitea .Lang != "" {
104
- rc .Lang = * complex .Gitea .Lang
100
+ if cfg .Gitea .Lang != nil && * cfg .Gitea .Lang != "" {
101
+ rc .Lang = * cfg .Gitea .Lang
105
102
}
106
103
107
- if complex .Gitea .TOC != nil {
108
- rc .TOC = * complex .Gitea .TOC
104
+ if cfg .Gitea .TOC != nil {
105
+ rc .TOC = * cfg .Gitea .TOC
109
106
}
110
107
111
108
return nil
@@ -116,9 +113,9 @@ func (rc *RenderConfig) toMetaNode() ast.Node {
116
113
return nil
117
114
}
118
115
switch rc .Meta {
119
- case "table" :
116
+ case markup . RenderMetaAsTable :
120
117
return nodeToTable (rc .yamlNode )
121
- case "details" :
118
+ case markup . RenderMetaAsDetails :
122
119
return nodeToDetails (rc .yamlNode , rc .Icon )
123
120
default :
124
121
return nil
0 commit comments