Skip to content

Commit 7a8e34b

Browse files
authored
Deal with markdown template without metadata (#21639) (#21654)
Backport #21639 . Fixed #21636. Related to #20987. A markdown template without metadata should not be treated as an invalid template. And this PR fixed another bug that non-template files(neither .md nor .yaml) are treated as yaml files. <img width="504" alt="image" src="https://user-images.githubusercontent.com/9418365/198968668-40082fa1-4f25-4d3e-9b73-1dbf6d1a7521.png">
1 parent e4a10f8 commit 7a8e34b

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Diff for: modules/issue/template/unmarshal.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/markup/markdown"
1515
"code.gitea.io/gitea/modules/setting"
1616
api "code.gitea.io/gitea/modules/structs"
17+
"code.gitea.io/gitea/modules/util"
1718

1819
"gopkg.in/yaml.v2"
1920
)
@@ -95,14 +96,27 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {
9596
}{}
9697

9798
if typ := it.Type(); typ == api.IssueTemplateTypeMarkdown {
98-
templateBody, err := markdown.ExtractMetadata(string(content), it)
99-
if err != nil {
100-
return nil, err
101-
}
102-
it.Content = templateBody
103-
if it.About == "" {
104-
if _, err := markdown.ExtractMetadata(string(content), compatibleTemplate); err == nil && compatibleTemplate.About != "" {
105-
it.About = compatibleTemplate.About
99+
if templateBody, err := markdown.ExtractMetadata(string(content), it); err != nil {
100+
// The only thing we know here is that we can't extract metadata from the content,
101+
// it's hard to tell if metadata doesn't exist or metadata isn't valid.
102+
// There's an example template:
103+
//
104+
// ---
105+
// # Title
106+
// ---
107+
// Content
108+
//
109+
// It could be a valid markdown with two horizontal lines, or an invalid markdown with wrong metadata.
110+
111+
it.Content = string(content)
112+
it.Name = filepath.Base(it.FileName)
113+
it.About, _ = util.SplitStringAtByteN(it.Content, 80)
114+
} else {
115+
it.Content = templateBody
116+
if it.About == "" {
117+
if _, err := markdown.ExtractMetadata(string(content), compatibleTemplate); err == nil && compatibleTemplate.About != "" {
118+
it.About = compatibleTemplate.About
119+
}
106120
}
107121
}
108122
} else if typ == api.IssueTemplateTypeYaml {

Diff for: modules/structs/issue.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (it IssueTemplate) Type() IssueTemplateType {
170170
if ext := filepath.Ext(it.FileName); ext == ".md" {
171171
return IssueTemplateTypeMarkdown
172172
} else if ext == ".yaml" || ext == ".yml" {
173-
return "yaml"
173+
return IssueTemplateTypeYaml
174174
}
175-
return IssueTemplateTypeYaml
175+
return ""
176176
}

0 commit comments

Comments
 (0)