2022-11-28 01:25:40PM
This commit is contained in:
parent
dda3c6f8a5
commit
9ad44bdec0
2 changed files with 39 additions and 9 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -15,8 +16,7 @@ import (
|
||||||
"oss.terrastruct.com/d2/lib/xmain"
|
"oss.terrastruct.com/d2/lib/xmain"
|
||||||
)
|
)
|
||||||
|
|
||||||
var uriImgRe = regexp.MustCompile(`<image href="(http[^"]+)"`)
|
var imageRe = regexp.MustCompile(`<image href="([^"]+)"`)
|
||||||
var localImgRe = regexp.MustCompile(`<image href="([^http][^"]+)"`)
|
|
||||||
|
|
||||||
type resp struct {
|
type resp struct {
|
||||||
srctxt string
|
srctxt string
|
||||||
|
|
@ -25,26 +25,35 @@ type resp struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func InlineLocal(ms *xmain.State, in []byte) ([]byte, error) {
|
func InlineLocal(ms *xmain.State, in []byte) ([]byte, error) {
|
||||||
return inline(ms, localImgRe, in)
|
return inline(ms, in, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InlineRemote(ms *xmain.State, in []byte) ([]byte, error) {
|
func InlineRemote(ms *xmain.State, in []byte) ([]byte, error) {
|
||||||
return inline(ms, uriImgRe, in)
|
return inline(ms, in, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func inline(ms *xmain.State, re *regexp.Regexp, in []byte) ([]byte, error) {
|
func inline(ms *xmain.State, in []byte, isRemote bool) ([]byte, error) {
|
||||||
svg := string(in)
|
svg := string(in)
|
||||||
|
|
||||||
imgs := re.FindAllStringSubmatch(svg, -1)
|
imgs := imageRe.FindAllStringSubmatch(svg, -1)
|
||||||
|
|
||||||
|
var filtered [][]string
|
||||||
|
for _, img := range imgs {
|
||||||
|
u, err := url.Parse(img[1])
|
||||||
|
isRemoteImg := err == nil && strings.HasPrefix(u.Scheme, "http")
|
||||||
|
if isRemoteImg == isRemote {
|
||||||
|
filtered = append(filtered, img)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
respChan := make(chan resp)
|
respChan := make(chan resp)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
wg.Add(len(imgs))
|
wg.Add(len(filtered))
|
||||||
for _, img := range imgs {
|
for _, img := range filtered {
|
||||||
if re == uriImgRe {
|
if isRemote {
|
||||||
go fetch(ctx, img[0], img[1], respChan)
|
go fetch(ctx, img[0], img[1], respChan)
|
||||||
} else {
|
} else {
|
||||||
go read(ctx, img[0], img[1], respChan)
|
go read(ctx, img[0], img[1], respChan)
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,27 @@ func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
return f(req), nil
|
return f(req), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegex(t *testing.T) {
|
||||||
|
urls := []string{
|
||||||
|
"https://icons.terrastruct.com/essentials/004-picture.svg",
|
||||||
|
"http://icons.terrastruct.com/essentials/004-picture.svg",
|
||||||
|
}
|
||||||
|
|
||||||
|
notURLs := []string{
|
||||||
|
"hi.png",
|
||||||
|
"./cat.png",
|
||||||
|
"/cat.png",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, href := range append(urls, notURLs...) {
|
||||||
|
str := fmt.Sprintf(`<image href="%s" />`, href)
|
||||||
|
matches := imageRe.FindAllStringSubmatch(str, -1)
|
||||||
|
if len(matches) != 1 {
|
||||||
|
t.Fatalf("uri regex didn't match %s", str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestInlineRemote(t *testing.T) {
|
func TestInlineRemote(t *testing.T) {
|
||||||
svgURL := "https://icons.terrastruct.com/essentials/004-picture.svg"
|
svgURL := "https://icons.terrastruct.com/essentials/004-picture.svg"
|
||||||
pngURL := "https://cdn4.iconfinder.com/data/icons/smart-phones-technologies/512/android-phone.png"
|
pngURL := "https://cdn4.iconfinder.com/data/icons/smart-phones-technologies/512/android-phone.png"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue