From 90eb31250eca94e9df7865312cf407c34bdf7b43 Mon Sep 17 00:00:00 2001 From: sett Date: Tue, 11 Apr 2023 16:26:20 +0200 Subject: [PATCH] fix: handle empty image title in pdf conversions --- globals/version.go | 2 +- pdf/conversions/image.go | 4 ---- pdf/conversions/string.go | 21 +++++++++++++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/globals/version.go b/globals/version.go index 1309827..257e2d1 100644 --- a/globals/version.go +++ b/globals/version.go @@ -1,3 +1,3 @@ package globals -const Version = "2.7.3" +const Version = "2.7.4" diff --git a/pdf/conversions/image.go b/pdf/conversions/image.go index 7924454..3f9ec1d 100644 --- a/pdf/conversions/image.go +++ b/pdf/conversions/image.go @@ -55,10 +55,6 @@ func Image(image *ast.Image, node ast.Node) (retO *spec.XObject, retA *spec.Adda titlePara := String(string(image.Title)) titleSegs := (*titlePara).(*elements.Paragraph).Segments para.Add(titleSegs...) - //para.Add(&spec.Segment{ - // Content: string(image.Title), - // Font: spec.SerifRegular, - //}) var a spec.Addable = ¶ retP = &a return diff --git a/pdf/conversions/string.go b/pdf/conversions/string.go index b415d99..abfd532 100644 --- a/pdf/conversions/string.go +++ b/pdf/conversions/string.go @@ -4,6 +4,7 @@ import ( "github.com/sett17/mdpaper/v2/globals" goldmark_cite "github.com/sett17/mdpaper/v2/goldmark-cite" goldmark_figref "github.com/sett17/mdpaper/v2/goldmark-figref" + "github.com/sett17/mdpaper/v2/pdf/elements" "github.com/sett17/mdpaper/v2/pdf/spec" "github.com/yuin/goldmark" "github.com/yuin/goldmark/ast" @@ -11,7 +12,7 @@ import ( "strings" ) -func String(s string) *spec.Addable { +func String(s string) (ret *spec.Addable) { p := goldmark.New( goldmark.WithExtensions( &goldmark_cite.CitationExtension{}, @@ -23,7 +24,23 @@ func String(s string) *spec.Addable { prevFile := globals.File globals.File = buf parsed := p.Parse(text.NewReader(buf)) - ret := Paragraph(parsed.FirstChild().(*ast.Paragraph), false) + if para, ok := parsed.FirstChild().(*ast.Paragraph); ok { + ret = Paragraph(para, false) + } else { + para := elements.Paragraph{ + Text: spec.Text{ + FontSize: globals.Cfg.Text.FontSize, + LineHeight: globals.Cfg.Text.LineHeight, + }, + } + emptySeg := spec.Segment{ + Content: "", + Font: spec.SerifRegular, + } + para.Add(&emptySeg) + var a spec.Addable = ¶ + ret = &a + } globals.File = prevFile return ret