mirror of
https://github.com/kovetskiy/mark.git
synced 2026-03-19 09:07:36 +08:00
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:
parent
32577c91f4
commit
a481424f7b
10
mark.go
10
mark.go
@ -265,7 +265,10 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
|
|||||||
Features: config.Features,
|
Features: config.Features,
|
||||||
ImageAlign: imageAlign,
|
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 {
|
if _, err := fmt.Fprintln(config.output(), html); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -340,7 +343,10 @@ func ProcessFile(file string, api *confluence.API, config Config) (*confluence.P
|
|||||||
ImageAlign: imageAlign,
|
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 {
|
if _, err = attachment.ResolveAttachments(api, target, inlineAttachments); err != nil {
|
||||||
return nil, fmt.Errorf("unable to create/update attachments: %w", err)
|
return nil, fmt.Errorf("unable to create/update attachments: %w", err)
|
||||||
|
|||||||
@ -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))
|
log.Tracef(nil, "rendering markdown:\n%s", string(markdown))
|
||||||
|
|
||||||
confluenceExtension := NewConfluenceExtension(stdlib, path, cfg)
|
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))
|
err := converter.Convert(markdown, &buf, parser.WithContext(ctx))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
html := buf.Bytes()
|
html := buf.Bytes()
|
||||||
|
|
||||||
log.Tracef(nil, "rendered markdown to html:\n%s", string(html))
|
log.Tracef(nil, "rendered markdown to html:\n%s", string(html))
|
||||||
|
|
||||||
return string(html), confluenceExtension.Attachments
|
return string(html), confluenceExtension.Attachments, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -67,7 +67,7 @@ func TestCompileMarkdown(t *testing.T) {
|
|||||||
Features: []string{"mkdocsadmonitions", "mention"},
|
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)
|
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"},
|
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)
|
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"},
|
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)
|
test.EqualValues(strings.TrimSuffix(string(html), "\n"), strings.TrimSuffix(actual, "\n"), filename+" vs "+htmlname)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user