Skip to content

Commit

Permalink
use platform specific files and build tags
Browse files Browse the repository at this point in the history
  • Loading branch information
hkparker committed Mar 9, 2025
1 parent 86f16e9 commit 24a4931
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
21 changes: 1 addition & 20 deletions widget/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package widget
import (
"io"
"net/url"
"runtime"
"strings"
"syscall/js"

"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"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 @@ -129,23 +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 {
if runtime.GOOS == "js" || runtime.GOOS == "wasip1" {
if !strings.HasPrefix(dest, "/") {
dest = "/" + dest
}
origin := js.Global().Get("location").Get("origin").String()
u, err = storage.ParseURI(origin + dest)
if err != nil {
return nil, nil
}
} else {
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_native.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build ci || !(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}}
}
28 changes: 28 additions & 0 deletions widget/markdown_image_wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build !ci && (!android || !ios || !mobile) && (wasm || test_web_driver)

package widget

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/storage"
"github.com/yuin/goldmark/ast"
"strings"
"syscall/js"
)

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}}
}

0 comments on commit 24a4931

Please sign in to comment.