2020-04-24 09:22:36 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-24 09:22:36 -04:00
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2022-10-05 14:55:36 -04:00
|
|
|
"fmt"
|
2020-04-24 09:22:36 -04:00
|
|
|
"strings"
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
|
|
|
|
2020-04-24 09:22:36 -04:00
|
|
|
"github.com/yuin/goldmark/ast"
|
2022-09-13 12:33:37 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-04-24 09:22:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// RenderConfig represents rendering configuration for this file
|
|
|
|
type RenderConfig struct {
|
2023-04-17 15:05:19 -04:00
|
|
|
Meta markup.RenderMetaMode
|
2022-09-13 12:33:37 -04:00
|
|
|
Icon string
|
2023-04-17 15:05:19 -04:00
|
|
|
TOC string // "false": hide, "side"/empty: in sidebar, "main"/"true": in main view
|
2022-09-13 12:33:37 -04:00
|
|
|
Lang string
|
|
|
|
yamlNode *yaml.Node
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 12:33:37 -04:00
|
|
|
// UnmarshalYAML implement yaml.v3 UnmarshalYAML
|
|
|
|
func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
if rc == nil {
|
2023-04-17 15:05:19 -04:00
|
|
|
return nil
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
2023-04-17 15:05:19 -04:00
|
|
|
|
2022-09-13 12:33:37 -04:00
|
|
|
rc.yamlNode = value
|
2020-04-24 09:22:36 -04:00
|
|
|
|
2022-10-05 14:55:36 -04:00
|
|
|
type commonRenderConfig struct {
|
2023-04-17 15:05:19 -04:00
|
|
|
TOC string `yaml:"include_toc"`
|
2022-10-05 14:55:36 -04:00
|
|
|
Lang string `yaml:"lang"`
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
2022-10-05 14:55:36 -04:00
|
|
|
var basic commonRenderConfig
|
|
|
|
if err := value.Decode(&basic); err != nil {
|
|
|
|
return fmt.Errorf("unable to decode into commonRenderConfig %w", err)
|
2022-09-13 12:33:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if basic.Lang != "" {
|
|
|
|
rc.Lang = basic.Lang
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.TOC = basic.TOC
|
2022-10-05 14:55:36 -04:00
|
|
|
|
|
|
|
type controlStringRenderConfig struct {
|
|
|
|
Gitea string `yaml:"gitea"`
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
|
|
|
|
2022-10-05 14:55:36 -04:00
|
|
|
var stringBasic controlStringRenderConfig
|
|
|
|
|
|
|
|
if err := value.Decode(&stringBasic); err == nil {
|
|
|
|
if stringBasic.Gitea != "" {
|
2023-04-17 15:05:19 -04:00
|
|
|
rc.Meta = renderMetaModeFromString(stringBasic.Gitea)
|
2022-09-13 12:33:37 -04:00
|
|
|
}
|
|
|
|
return nil
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
2022-09-13 12:33:37 -04:00
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
type yamlRenderConfig struct {
|
2022-10-05 14:55:36 -04:00
|
|
|
Meta *string `yaml:"meta"`
|
|
|
|
Icon *string `yaml:"details_icon"`
|
2023-04-17 15:05:19 -04:00
|
|
|
TOC *string `yaml:"include_toc"`
|
2022-10-05 14:55:36 -04:00
|
|
|
Lang *string `yaml:"lang"`
|
|
|
|
}
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
type yamlRenderConfigWrapper struct {
|
|
|
|
Gitea *yamlRenderConfig `yaml:"gitea"`
|
2022-10-05 14:55:36 -04:00
|
|
|
}
|
2023-04-17 15:05:19 -04:00
|
|
|
|
|
|
|
var cfg yamlRenderConfigWrapper
|
|
|
|
if err := value.Decode(&cfg); err != nil {
|
|
|
|
return fmt.Errorf("unable to decode into yamlRenderConfigWrapper %w", err)
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
if cfg.Gitea == nil {
|
2022-10-05 14:55:36 -04:00
|
|
|
return nil
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
if cfg.Gitea.Meta != nil {
|
|
|
|
rc.Meta = renderMetaModeFromString(*cfg.Gitea.Meta)
|
2022-09-13 12:33:37 -04:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
if cfg.Gitea.Icon != nil {
|
|
|
|
rc.Icon = strings.TrimSpace(strings.ToLower(*cfg.Gitea.Icon))
|
2022-10-05 14:55:36 -04:00
|
|
|
}
|
2022-09-13 12:33:37 -04:00
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
if cfg.Gitea.Lang != nil && *cfg.Gitea.Lang != "" {
|
|
|
|
rc.Lang = *cfg.Gitea.Lang
|
2022-09-13 12:33:37 -04:00
|
|
|
}
|
|
|
|
|
2023-04-17 15:05:19 -04:00
|
|
|
if cfg.Gitea.TOC != nil {
|
|
|
|
rc.TOC = *cfg.Gitea.TOC
|
2022-09-13 12:33:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 09:22:36 -04:00
|
|
|
|
2022-09-13 12:33:37 -04:00
|
|
|
func (rc *RenderConfig) toMetaNode() ast.Node {
|
|
|
|
if rc.yamlNode == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch rc.Meta {
|
2023-04-17 15:05:19 -04:00
|
|
|
case markup.RenderMetaAsTable:
|
2022-09-13 12:33:37 -04:00
|
|
|
return nodeToTable(rc.yamlNode)
|
2023-04-17 15:05:19 -04:00
|
|
|
case markup.RenderMetaAsDetails:
|
2022-09-13 12:33:37 -04:00
|
|
|
return nodeToDetails(rc.yamlNode, rc.Icon)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 09:22:36 -04:00
|
|
|
}
|