Merge pull request #666 from alixander/escape-url

unescape img hrefs
This commit is contained in:
Alexander Wang 2023-01-14 15:49:43 -08:00 committed by GitHub
commit b7d1c9b563
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View file

@ -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)

View file

@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"fmt"
"html"
"io/ioutil"
"mime"
"net/http"
@ -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 {
@ -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
@ -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 {