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

unescape img hrefs #666

Merged
merged 3 commits into from
Jan 14, 2023
Merged
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
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
- Fixes tooltip/link attributes being ignored for `sql_table` and `class`. [#658](https://github.com/terrastruct/d2/pull/658)
- Fixes arrowheads sometimes appearing broken with sketch on. [#656](https://github.com/terrastruct/d2/pull/656)
- Fixes code snippets not being tall enough with leading newlines. [#664](https://github.com/terrastruct/d2/pull/664)
- Icon URLs that needed escaping (e.g. with ampersands) are handled correctly by CLI. [#666](https://github.com/terrastruct/d2/pull/666)
9 changes: 5 additions & 4 deletions lib/imgbundler/imgbundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"fmt"
"html"
"io/ioutil"
"mime"
"net/http"
Expand Down Expand Up @@ -66,7 +67,7 @@ func filterImageElements(imgs [][][]byte, isRemote bool) [][][]byte {
continue
}

u, err := url.Parse(href)
u, err := url.Parse(html.UnescapeString(href))
isRemoteImg := err == nil && strings.HasPrefix(u.Scheme, "http")

if isRemoteImg == isRemote {
Expand Down Expand Up @@ -147,9 +148,9 @@ func worker(ctx context.Context, href []byte, isRemote bool) ([]byte, error) {
var mimeType string
var err error
if isRemote {
buf, mimeType, err = httpGet(ctx, string(href))
buf, mimeType, err = httpGet(ctx, html.UnescapeString(string(href)))
} else {
buf, err = os.ReadFile(string(href))
buf, err = os.ReadFile(html.UnescapeString(string(href)))
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -194,7 +195,7 @@ func httpGet(ctx context.Context, href string) ([]byte, string, error) {
func sniffMimeType(href, buf []byte, isRemote bool) string {
p := string(href)
if isRemote {
u, err := url.Parse(p)
u, err := url.Parse(html.UnescapeString(p))
if err != nil {
p = ""
} else {
Expand Down