Skip to content

Commit

Permalink
🎨 Improve Wikipedia clipping siyuan-note/siyuan#11640
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 5, 2024
1 parent fa2c3b4 commit 3a5841e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
22 changes: 16 additions & 6 deletions h2m.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package lute

import (
"bytes"
"path"
"strings"
"unicode"

Expand Down Expand Up @@ -608,6 +609,11 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
return
}

if nil != n.FirstChild && atom.Img == n.FirstChild.DataAtom && strings.Contains(util.DomAttrValue(n.FirstChild, "src"), "wikimedia.org") {
// Wikipedia 链接嵌套图片的情况只保留图片
break
}

node.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
Expand Down Expand Up @@ -654,12 +660,16 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
}
}

// 为 Wikipedia svg 优化
if strings.Contains(src, "wikipedia/commons/thumb/") && strings.Contains(src, ".svg.png") && strings.Contains(src, ".svg/") {
src = strings.ReplaceAll(src, "/commons/thumb/", "/commons/")
src = strings.ReplaceAll(src, ".svg.png", ".svg")
idx := strings.Index(src, ".svg/")
src = src[:idx+4]
// Wikipedia 使用图片原图 https://github.com/siyuan-note/siyuan/issues/11640
if strings.Contains(src, "wikipedia/commons/thumb/") {
ext := path.Ext(src)
if strings.Contains(src, ".svg.png") {
ext = ".svg"
}
if idx := strings.Index(src, ext+"/"); 0 < idx {
src = src[:idx+len(ext)]
src = strings.ReplaceAll(src, "/commons/thumb/", "/commons/")
}
}

node.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: util.StrToBytes(src)})
Expand Down
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.

Loading

0 comments on commit 3a5841e

Please sign in to comment.