1650a26eb5
Backport https://github.com/go-gitea/gitea/pull/25184 by @jtran Closes #25225. Fixes https://github.com/go-gitea/gitea/issues/25160. `data-source-position` of checkboxes in a task list was incorrect whenever there was YAML front matter. This would result in issue content or PR descriptions getting corrupted with random `x` or space characters when a user checked or unchecked a task.
129 lines
2.6 KiB
Go
129 lines
2.6 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package markdown
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/yuin/goldmark/ast"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// RenderConfig represents rendering configuration for this file
|
|
type RenderConfig struct {
|
|
Meta string
|
|
Icon string
|
|
TOC bool
|
|
Lang string
|
|
yamlNode *yaml.Node
|
|
|
|
// Used internally. Cannot be controlled by frontmatter.
|
|
metaLength int
|
|
}
|
|
|
|
// UnmarshalYAML implement yaml.v3 UnmarshalYAML
|
|
func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
|
|
if rc == nil {
|
|
rc = &RenderConfig{
|
|
Meta: "table",
|
|
Icon: "table",
|
|
Lang: "",
|
|
}
|
|
}
|
|
rc.yamlNode = value
|
|
|
|
type commonRenderConfig struct {
|
|
TOC bool `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 != "" {
|
|
switch strings.TrimSpace(strings.ToLower(stringBasic.Gitea)) {
|
|
case "none":
|
|
rc.Meta = "none"
|
|
case "table":
|
|
rc.Meta = "table"
|
|
default: // "details"
|
|
rc.Meta = "details"
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type giteaControl struct {
|
|
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)
|
|
}
|
|
|
|
if complex.Gitea == nil {
|
|
return nil
|
|
}
|
|
|
|
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"
|
|
}
|
|
}
|
|
|
|
if complex.Gitea.Icon != nil {
|
|
rc.Icon = strings.TrimSpace(strings.ToLower(*complex.Gitea.Icon))
|
|
}
|
|
|
|
if complex.Gitea.Lang != nil && *complex.Gitea.Lang != "" {
|
|
rc.Lang = *complex.Gitea.Lang
|
|
}
|
|
|
|
if complex.Gitea.TOC != nil {
|
|
rc.TOC = *complex.Gitea.TOC
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|