Skip to content

Commit

Permalink
🎨 Protyle Improve image width setting siyuan-note/siyuan#12516
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 1, 2024
1 parent 638ecbd commit 35cc811
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
4 changes: 2 additions & 2 deletions javascript/lute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion javascript/lute.min.js.map

Large diffs are not rendered by default.

12 changes: 11 additions & 1 deletion parse/inline_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,17 @@ func SetSpanIAL(node *ast.Node, n *html.Node) {
if style := util.DomAttrValue(n.Parent, "style"); "" != style {
if insertedIAL {
m := Tokens2IAL(node.Next.Tokens)
m = append(m, []string{"style", style})
merged := false
for _, kv := range m {
if "style" == kv[0] {
kv[1] = kv[1] + style
merged = true
break
}
}
if !merged {
m = append(m, []string{"style", style})
}
node.Next.Tokens = IAL2Tokens(m)
node.SetIALAttr("style", style)
node.KramdownIAL = m
Expand Down
31 changes: 30 additions & 1 deletion render/protyle_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,20 @@ func (r *ProtyleRenderer) renderImage(node *ast.Node, entering bool) ast.WalkSta
r.Tag("/span", nil, false)
attrs = [][]string{}
if style := node.IALAttr("style"); "" != style {
attrs = append(attrs, []string{"style", style})
styles := strings.Split(style, ";")
var width string
for _, s := range styles {
if strings.Contains(s, "width") {
width = s
break
}
}
width = strings.ReplaceAll(width, "vw", "%")
width = strings.TrimSpace(width)
if "" != width {
width += ";"
attrs = append(attrs, []string{"style", width})
}
}
r.Tag("span", attrs, false)
r.Tag("span", [][]string{{"class", "protyle-action protyle-icons"}}, false)
Expand All @@ -1291,6 +1304,22 @@ func (r *ProtyleRenderer) renderImage(node *ast.Node, entering bool) ast.WalkSta
titleTokens = title.Tokens
attrs = append(attrs, []string{"title", r.escapeRefText(string(titleTokens))})
}
if style := node.IALAttr("style"); "" != style {
styles := strings.Split(style, ";")
var width string
for _, s := range styles {
if strings.Contains(s, "width") {
width = s
}
}
style = strings.ReplaceAll(style, width+";", "")
style = strings.ReplaceAll(style, "flex: 0 0 auto;", "")
style = strings.ReplaceAll(style, "display: block;", "")
style = strings.TrimSpace(style)
if "" != style {
attrs = append(attrs, []string{"style", style})
}
}
r.Tag("img", attrs, true)

buf := r.Writer.Bytes()
Expand Down
Loading

0 comments on commit 35cc811

Please sign in to comment.