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