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