fix: return error instead of panic from CompileMarkdown

Markdown conversion failures called panic(err), crashing the process
rather than allowing graceful error handling. Change the return type
to (string, []attachment.Attachment, error) and propagate the error.
Update all callers (mark.go, markdown_test.go) accordingly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Manuel Rüger 2026-03-13 01:13:51 +01:00
parent 32577c91f4
commit a481424f7b
3 changed files with 14 additions and 8 deletions

10
mark.go
View File

@ -265,7 +265,10 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
Features: config.Features,
ImageAlign: imageAlign,
}
html, _ := markmd.CompileMarkdown(markdown, std, file, cfg)
html, _, err := markmd.CompileMarkdown(markdown, std, file, cfg)
if err != nil {
return nil, fmt.Errorf("unable to compile markdown: %w", err)
}
if _, err := fmt.Fprintln(config.output(), html); err != nil {
return nil, err
}
@ -340,7 +343,10 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
ImageAlign: imageAlign,
}
html, inlineAttachments := markmd.CompileMarkdown(markdown, std, file, cfg)
html, inlineAttachments, err := markmd.CompileMarkdown(markdown, std, file, cfg)
if err != nil {
return nil, fmt.Errorf("unable to compile markdown: %w", err)
}
if _, err = attachment.ResolveAttachments(api, target, inlineAttachments); err != nil {
return nil, fmt.Errorf("unable to create/update attachments: %w", err)

View File

@ -90,7 +90,7 @@ func (c *ConfluenceExtension) Extend(m goldmark.Markdown) {
))
}
func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types.MarkConfig) (string, []attachment.Attachment) {
func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types.MarkConfig) (string, []attachment.Attachment, error) {
log.Tracef(nil, "rendering markdown:\n%s", string(markdown))
confluenceExtension := NewConfluenceExtension(stdlib, path, cfg)
@ -119,12 +119,12 @@ func CompileMarkdown(markdown []byte, stdlib *stdlib.Lib, path string, cfg types
err := converter.Convert(markdown, &buf, parser.WithContext(ctx))
if err != nil {
panic(err)
return "", nil, err
}
html := buf.Bytes()
log.Tracef(nil, "rendered markdown to html:\n%s", string(html))
return string(html), confluenceExtension.Attachments
return string(html), confluenceExtension.Attachments, nil
}

View File

@ -67,7 +67,7 @@ func TestCompileMarkdown(t *testing.T) {
Features: []string{"mkdocsadmonitions", "mention"},
}
actual, _ := mark.CompileMarkdown(markdown, lib, filename, cfg)
actual, _, _ := mark.CompileMarkdown(markdown, lib, filename, cfg)
test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname)
}
}
@ -109,7 +109,7 @@ func TestCompileMarkdownDropH1(t *testing.T) {
Features: []string{"mkdocsadmonitions", "mention"},
}
actual, _ := mark.CompileMarkdown(markdown, lib, filename, cfg)
actual, _, _ := mark.CompileMarkdown(markdown, lib, filename, cfg)
test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname)
}
@ -153,7 +153,7 @@ func TestCompileMarkdownStripNewlines(t *testing.T) {
Features: []string{"mkdocsadmonitions", "mention"},
}
actual, _ := mark.CompileMarkdown(markdown, lib, filename, cfg)
actual, _, _ := mark.CompileMarkdown(markdown, lib, filename, cfg)
test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname)
}