Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load markdown images from origin when not a URL #5579

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions widget/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions widget/markdown_image_notweb.go
Original file line number Diff line number Diff line change
@@ -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}}
}
29 changes: 29 additions & 0 deletions widget/markdown_image_wasm.go
Original file line number Diff line number Diff line change
@@ -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}}
}
Loading