2020-04-24 15:22:36 +02:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-04-24 15:22:36 +02:00
|
|
|
|
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
2022-10-05 20:55:36 +02:00
|
|
|
"fmt"
|
2020-04-24 15:22:36 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/yuin/goldmark/ast"
|
2022-09-13 18:33:37 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
2020-04-24 15:22:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// RenderConfig represents rendering configuration for this file
|
|
|
|
type RenderConfig struct {
|
2022-09-13 18:33:37 +02:00
|
|
|
Meta string
|
|
|
|
Icon string
|
|
|
|
TOC bool
|
|
|
|
Lang string
|
|
|
|
yamlNode *yaml.Node
|
2023-06-13 20:22:59 +02:00
|
|
|
|
|
|
|
// Used internally. Cannot be controlled by frontmatter.
|
|
|
|
metaLength int
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 18:33:37 +02:00
|
|
|
// UnmarshalYAML implement yaml.v3 UnmarshalYAML
|
|
|
|
func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
if rc == nil {
|
|
|
|
rc = &RenderConfig{
|
|
|
|
Meta: "table",
|
|
|
|
Icon: "table",
|
|
|
|
Lang: "",
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 18:33:37 +02:00
|
|
|
rc.yamlNode = value
|
2020-04-24 15:22:36 +02:00
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
type commonRenderConfig struct {
|
|
|
|
TOC bool `yaml:"include_toc"`
|
|
|
|
Lang string `yaml:"lang"`
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
2022-10-05 20:55:36 +02:00
|
|
|
var basic commonRenderConfig
|
|
|
|
if err := value.Decode(&basic); err != nil {
|
|
|
|
return fmt.Errorf("unable to decode into commonRenderConfig %w", err)
|
2022-09-13 18:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if basic.Lang != "" {
|
|
|
|
rc.Lang = basic.Lang
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.TOC = basic.TOC
|
2022-10-05 20:55:36 +02:00
|
|
|
|
|
|
|
type controlStringRenderConfig struct {
|
|
|
|
Gitea string `yaml:"gitea"`
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
var stringBasic controlStringRenderConfig
|
|
|
|
|
|
|
|
if err := value.Decode(&stringBasic); err == nil {
|
|
|
|
if stringBasic.Gitea != "" {
|
|
|
|
switch strings.TrimSpace(strings.ToLower(stringBasic.Gitea)) {
|
|
|
|
case "none":
|
|
|
|
rc.Meta = "none"
|
|
|
|
case "table":
|
|
|
|
rc.Meta = "table"
|
|
|
|
default: // "details"
|
|
|
|
rc.Meta = "details"
|
|
|
|
}
|
2022-09-13 18:33:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
2022-09-13 18:33:37 +02:00
|
|
|
|
|
|
|
type giteaControl struct {
|
2022-10-05 20:55:36 +02:00
|
|
|
Meta *string `yaml:"meta"`
|
|
|
|
Icon *string `yaml:"details_icon"`
|
|
|
|
TOC *bool `yaml:"include_toc"`
|
|
|
|
Lang *string `yaml:"lang"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type complexGiteaConfig struct {
|
|
|
|
Gitea *giteaControl `yaml:"gitea"`
|
|
|
|
}
|
|
|
|
var complex complexGiteaConfig
|
|
|
|
if err := value.Decode(&complex); err != nil {
|
|
|
|
return fmt.Errorf("unable to decode into complexRenderConfig %w", err)
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
if complex.Gitea == nil {
|
|
|
|
return nil
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
if complex.Gitea.Meta != nil {
|
|
|
|
switch strings.TrimSpace(strings.ToLower(*complex.Gitea.Meta)) {
|
|
|
|
case "none":
|
|
|
|
rc.Meta = "none"
|
|
|
|
case "table":
|
|
|
|
rc.Meta = "table"
|
|
|
|
default: // "details"
|
|
|
|
rc.Meta = "details"
|
|
|
|
}
|
2022-09-13 18:33:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
if complex.Gitea.Icon != nil {
|
|
|
|
rc.Icon = strings.TrimSpace(strings.ToLower(*complex.Gitea.Icon))
|
|
|
|
}
|
2022-09-13 18:33:37 +02:00
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
if complex.Gitea.Lang != nil && *complex.Gitea.Lang != "" {
|
|
|
|
rc.Lang = *complex.Gitea.Lang
|
2022-09-13 18:33:37 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 20:55:36 +02:00
|
|
|
if complex.Gitea.TOC != nil {
|
|
|
|
rc.TOC = *complex.Gitea.TOC
|
2022-09-13 18:33:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 15:22:36 +02:00
|
|
|
|
2022-09-13 18:33:37 +02:00
|
|
|
func (rc *RenderConfig) toMetaNode() ast.Node {
|
|
|
|
if rc.yamlNode == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch rc.Meta {
|
|
|
|
case "table":
|
|
|
|
return nodeToTable(rc.yamlNode)
|
|
|
|
case "details":
|
|
|
|
return nodeToDetails(rc.yamlNode, rc.Icon)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-24 15:22:36 +02:00
|
|
|
}
|