diff --git a/widget/markdown.go b/widget/markdown.go index c6148fed29..f182852bf1 100644 --- a/widget/markdown.go +++ b/widget/markdown.go @@ -10,7 +10,6 @@ import ( "github.com/yuin/goldmark/renderer" "fyne.io/fyne/v2" - "fyne.io/fyne/v2/storage" ) // NewRichTextFromMarkdown configures a RichText widget by parsing the provided markdown content. @@ -127,12 +126,7 @@ func renderNode(source []byte, n ast.Node, blockquote bool) ([]RichTextSegment, case *ast.Blockquote: return renderChildren(source, n, true) case *ast.Image: - dest := string(t.Destination) - u, err := storage.ParseURI(dest) - if err != nil { - u = storage.NewFileURI(dest) - } - return []RichTextSegment{&ImageSegment{Source: u, Title: string(t.Title), Alignment: fyne.TextAlignCenter}}, nil + return parseMarkdownImage(t), nil } return nil, nil } diff --git a/widget/markdown_image_notweb.go b/widget/markdown_image_notweb.go new file mode 100644 index 0000000000..f6e14b48bc --- /dev/null +++ b/widget/markdown_image_notweb.go @@ -0,0 +1,18 @@ +//go:build !wasm && !test_web_driver + +package widget + +import ( + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/storage" + "github.com/yuin/goldmark/ast" +) + +func parseMarkdownImage(t *ast.Image) []RichTextSegment { + dest := string(t.Destination) + u, err := storage.ParseURI(dest) + if err != nil { + u = storage.NewFileURI(dest) + } + return []RichTextSegment{&ImageSegment{Source: u, Title: string(t.Title), Alignment: fyne.TextAlignCenter}} +} diff --git a/widget/markdown_image_wasm.go b/widget/markdown_image_wasm.go new file mode 100644 index 0000000000..0cfbfcd4a8 --- /dev/null +++ b/widget/markdown_image_wasm.go @@ -0,0 +1,29 @@ +//go:build !ci && (!android || !ios || !mobile) && (wasm || test_web_driver) + +package widget + +import ( + "strings" + "syscall/js" + + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/storage" + "github.com/yuin/goldmark/ast" +) + +func parseMarkdownImage(t *ast.Image) []RichTextSegment { + dest := string(t.Destination) + u, err := storage.ParseURI(dest) + if err != nil { + if !strings.HasPrefix(dest, "/") { + dest = "/" + dest + } + origin := js.Global().Get("location").Get("origin").String() + u, err = storage.ParseURI(origin + dest) + if err != nil { + fyne.LogError("Can't load image in markdown", err) + return []RichTextSegment{} + } + } + return []RichTextSegment{&ImageSegment{Source: u, Title: string(t.Title), Alignment: fyne.TextAlignCenter}} +}