This commit is contained in:
Liang Ding 2023-01-03 00:08:47 +08:00
parent 145641cd06
commit bb0ebefb1d
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 58 additions and 4 deletions

View file

@ -99,7 +99,7 @@ func Export2Liandi(id string) (err error) {
title := path.Base(tree.HPath)
tags := tree.Root.IALAttr("tags")
content := exportMarkdownContent0(tree, "https://b3logfile.com/siyuan/",
content := exportMarkdownContent0(tree, "https://b3logfile.com/siyuan/", true,
4, 1, 0,
"#", "#",
"", "",
@ -729,7 +729,7 @@ func ExportStdMarkdown(id string) string {
if IsSubscriber() {
cloudAssetsBase = "https://assets.b3logfile.com/siyuan/"
}
return exportMarkdownContent0(tree, cloudAssetsBase,
return exportMarkdownContent0(tree, cloudAssetsBase, false,
Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
@ -1167,7 +1167,7 @@ func exportMarkdownContent(id string) (hPath, exportedMd string) {
return
}
hPath = tree.HPath
exportedMd = exportMarkdownContent0(tree, "",
exportedMd = exportMarkdownContent0(tree, "", false,
Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
@ -1175,7 +1175,7 @@ func exportMarkdownContent(id string) (hPath, exportedMd string) {
return
}
func exportMarkdownContent0(tree *parse.Tree, cloudAssetsBase string,
func exportMarkdownContent0(tree *parse.Tree, cloudAssetsBase string, assetsDestSpace2Underscore bool,
blockRefMode, blockEmbedMode, fileAnnotationRefMode int,
tagOpenMarker, tagCloseMarker string,
blockRefTextLeft, blockRefTextRight string,
@ -1191,6 +1191,26 @@ func exportMarkdownContent0(tree *parse.Tree, cloudAssetsBase string,
if "" != cloudAssetsBase {
luteEngine.RenderOptions.LinkBase = cloudAssetsBase + Conf.User.UserId + "/"
}
if assetsDestSpace2Underscore { // 上传到社区图床的资源文件会将空格转为下划线,所以这里也需要将文档内容做相应的转换
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
}
if ast.NodeLinkDest == n.Type {
if util.IsAssetLinkDest(n.Tokens) {
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte(" "), []byte("_"))
}
} else if n.IsTextMarkType("a") {
href := n.TextMarkAHref
if util.IsAssetLinkDest([]byte(href)) {
n.TextMarkAHref = strings.ReplaceAll(href, " ", "_")
}
}
return ast.WalkContinue
})
}
renderer := render.NewProtyleExportMdRenderer(tree, luteEngine.RenderOptions)
ret = gulu.Str.FromBytes(renderer.Render())
return

View file

@ -514,6 +514,40 @@ func buildSpanFromNode(n *ast.Node, tree *parse.Tree, rootID, boxID, p string) (
IAL: treenode.IALStr(n),
}
spans = append(spans, span)
if n.IsTextMarkType("a") {
dest := n.TextMarkAHref
if util.IsAssetLinkDest([]byte(dest)) {
var title string
if titleNode := n.ChildByType(ast.NodeLinkTitle); nil != titleNode {
title = gulu.Str.FromBytes(titleNode.Tokens)
}
var hash string
var hashErr error
if lp := assetLocalPath(dest, boxLocalPath, docDirLocalPath); "" != lp {
if !gulu.File.IsDir(lp) {
hash, hashErr = util.GetEtag(lp)
if nil != hashErr {
logging.LogErrorf("calc asset [%s] hash failed: %s", lp, hashErr)
}
}
}
name, _ := util.LastID(dest)
asset := &Asset{
ID: ast.NewNodeID(),
BlockID: parentBlock.ID,
RootID: rootID,
Box: boxID,
DocPath: p,
Path: dest,
Name: name,
Title: title,
Hash: hash,
}
assets = append(assets, asset)
}
}
walkStatus = ast.WalkSkipChildren
return
case ast.NodeDocument: