Skip to content

Commit

Permalink
修正中文文件名缩放不正确问题
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Apr 19, 2024
1 parent 62379ef commit d208b12
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
9 changes: 8 additions & 1 deletion service/modules/renderers/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ func TestMarkdownAll(t *testing.T) {
Markdown: `- ![avatar](test_data/avatar.jpg?scale=0.3)`,
Html: `<ul>
<li><img src="test_data/avatar.jpg" alt="avatar" loading="lazy" width=138 height=138 /></li>
</ul>`,
},
{
ID: 2.1,
Markdown: `- ![avatar](test_data/中文.jpg?scale=0.3)`,
Html: `<ul>
<li><img src="test_data/%E4%B8%AD%E6%96%87.jpg" alt="avatar" loading="lazy" width=138 height=138 /></li>
</ul>`,
},
{
Expand Down Expand Up @@ -108,7 +115,7 @@ func TestMarkdownAll(t *testing.T) {
}
for _, tc := range cases {
md := renderers.NewMarkdown(tc.Options...)
if tc.ID == 2 {
if tc.ID == 2.1 {
log.Println(`debug`)
}
_, html, err := md.Render(tc.Markdown)
Expand Down
20 changes: 10 additions & 10 deletions service/modules/renderers/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"log"
"net/http"
"net/url"
urlpkg "net/url"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -281,7 +282,6 @@ func (me *_Markdown) renderImage(w util.BufWriter, source []byte, node ast.Node,
w.WriteString(fmt.Sprintf(`class="%s"`, strings.Join(classes, " ")))
}

updatedURL := url.String()
// 看起来是文章内的相对链接?
// 如果是的话,需要 resolve 到相对应的目录。
if !url.IsAbs() && !strings.HasPrefix(url.Path, `/`) && me.pathResolver != nil {
Expand All @@ -291,11 +291,10 @@ func (me *_Markdown) renderImage(w util.BufWriter, source []byte, node ast.Node,
// fallthrough
} else {
url.Path = pathRelative
updatedURL = url.String()
}
}

width, height := size(updatedURL)
width, height := size(url)
if width > 0 && height > 0 {
widthScaled, heightScaled := int(float64(width)*scale), int(float64(height)*scale)
w.WriteString(fmt.Sprintf(` width=%d height=%d`, widthScaled, heightScaled))
Expand All @@ -319,16 +318,17 @@ func nodeToHTMLText(n ast.Node, source []byte) []byte {
return buf.Bytes()
}

func size(path string) (int, int) {
func size(url *urlpkg.URL) (int, int) {
var fp io.ReadCloser
if strings.Contains(path, `://`) {
resp, err := http.Get(path)
switch strings.ToLower(url.Scheme) {
case `http`, `https`:
resp, err := http.Get(url.String())
if err != nil {
panic(err)
}
fp = resp.Body
} else {
f, err := os.Open(path)
default:
f, err := os.Open(url.Path)
if err != nil {
// panic(err)
return 0, 0
Expand All @@ -342,7 +342,7 @@ func size(path string) (int, int) {
if _, err := sfp.Seek(0, io.SeekStart); err != nil {
panic(err)
}
if strings.EqualFold(filepath.Ext(path), `.svg`) {
if strings.EqualFold(filepath.Ext(url.Path), `.svg`) {
type _SvgSize struct {
Width string `xml:"width,attr"`
Height string `xml:"height,attr"`
Expand All @@ -365,7 +365,7 @@ func size(path string) (int, int) {
// TODO 移除通过 @2x 类似的图片缩放支持。
for i := 1; i <= 10; i++ {
scaleFmt := fmt.Sprintf(`@%dx.`, i)
if strings.Contains(filepath.Base(path), scaleFmt) {
if strings.Contains(filepath.Base(url.Path), scaleFmt) {
width /= i
height /= i
break
Expand Down
Binary file added service/modules/renderers/test_data/中文.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d208b12

Please sign in to comment.