Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2022-09-15 11:57:45 +08:00
commit a481841495
4 changed files with 6 additions and 118 deletions

File diff suppressed because one or more lines are too long

View file

@ -7,7 +7,7 @@ require (
github.com/88250/css v0.1.2
github.com/88250/flock v0.8.2
github.com/88250/gulu v1.2.3-0.20220909041418-fdfa4d7380bf
github.com/88250/lute v1.7.5-0.20220915015845-9ff9c7159b88
github.com/88250/lute v1.7.5-0.20220915034356-e5dab9ee72c9
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1
github.com/88250/pdfcpu v0.3.13
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1

View file

@ -19,8 +19,8 @@ github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5 h1:8HdZozCsXS
github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/88250/gulu v1.2.3-0.20220909041418-fdfa4d7380bf h1:q+iiBuKjdvUKj5dQQHCRs+g9oChTWkZmm7r1FMvtkfI=
github.com/88250/gulu v1.2.3-0.20220909041418-fdfa4d7380bf/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
github.com/88250/lute v1.7.5-0.20220915015845-9ff9c7159b88 h1:M9DYHQm0MOc1Tyy4ccYyR5r5dTvOEAuiKTPHvAmXIj4=
github.com/88250/lute v1.7.5-0.20220915015845-9ff9c7159b88/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
github.com/88250/lute v1.7.5-0.20220915034356-e5dab9ee72c9 h1:VIw3wMkTmA7nMYc2Tj7NGYrlAGRJZ2BADT4/k26tAQw=
github.com/88250/lute v1.7.5-0.20220915034356-e5dab9ee72c9/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1 h1:9Cb+iN639vUI2OcIBc+4oGwml9/0J6bL6dWNb8Al+1s=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1/go.mod h1:jH6MMPr8G7AMzaVmWHXZQiB1DKO3giWbcWZ7UoJ1teI=
github.com/88250/pdfcpu v0.3.13 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=

View file

@ -34,7 +34,6 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/editor"
"github.com/88250/lute/html"
"github.com/88250/lute/lex"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/88250/pdfcpu/pkg/api"
@ -921,68 +920,11 @@ func exportMarkdownContent(id string) (hPath, exportedMd string) {
luteEngine := NewLute()
luteEngine.SetFootnotes(true)
luteEngine.SetKramdownIAL(false)
exportedMd = formatExportMd(tree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
renderer := render.NewProtyleExportMdRenderer(tree, luteEngine.RenderOptions)
exportedMd = gulu.Str.FromBytes(renderer.Render())
return
}
func formatExportMd(node *ast.Node, parseOptions *parse.Options, renderOptions *render.Options) string {
root := &ast.Node{Type: ast.NodeDocument}
tree := &parse.Tree{Root: root, Context: &parse.Context{ParseOption: parseOptions}}
renderer := render.NewFormatRenderer(tree, renderOptions)
renderer.Writer = &bytes.Buffer{}
renderer.Writer.Grow(4096)
renderer.NodeWriterStack = append(renderer.NodeWriterStack, renderer.Writer)
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
switch n.Type {
case ast.NodeParagraph: // 段落需要单独处理。导出为 Markdown 时段落开头空两格不生效 https://github.com/siyuan-note/siyuan/issues/3167
return renderExportMdParagraph(renderer, n, entering)
case ast.NodeMathBlockContent:
return renderExportMdMathBlockContent(renderer, n, entering)
case ast.NodeInlineMathContent:
return renderExportMdInlineMathContent(renderer, n, entering)
case ast.NodeCodeBlockCode:
return renderExportMdCodeBlockCode(renderer, n, entering)
default:
rendererFunc := renderer.RendererFuncs[n.Type]
return rendererFunc(n, entering)
}
})
return gulu.Str.FromBytes(renderer.Writer.Bytes())
}
func renderExportMdCodeBlockCode(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
if entering {
tokens := node.Tokens
info := node.Parent.ChildByType(ast.NodeCodeBlockFenceInfoMarker)
if nil != info &&
bytes.Contains(info.CodeBlockInfo, []byte("flowchart")) ||
bytes.Contains(info.CodeBlockInfo, []byte("mermaid")) ||
bytes.Contains(info.CodeBlockInfo, []byte("graphviz")) ||
bytes.Contains(info.CodeBlockInfo, []byte("plantuml")) {
tokens = html.UnescapeHTML(tokens)
}
r.Write(tokens)
}
return ast.WalkContinue
}
func renderExportMdMathBlockContent(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
if entering {
tokens := html.UnescapeHTML(node.Tokens)
r.Write(tokens)
r.WriteByte(lex.ItemNewline)
}
return ast.WalkContinue
}
func renderExportMdInlineMathContent(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
if entering {
tokens := html.UnescapeHTML(node.Tokens)
r.Write(tokens)
}
return ast.WalkContinue
}
func processKaTexMacros(n *ast.Node) {
if ast.NodeInlineMathContent != n.Type && ast.NodeMathBlockContent != n.Type {
return
@ -1022,60 +964,6 @@ func processKaTexMacros(n *ast.Node) {
n.Tokens = []byte(mathContent)
}
func renderExportMdParagraph(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
if entering {
if r.Options.ChineseParagraphBeginningSpace && ast.NodeDocument == node.Parent.Type {
r.WriteString("  ")
}
} else {
if !r.Options.KeepParagraphBeginningSpace && nil != node.FirstChild {
node.FirstChild.Tokens = bytes.TrimSpace(node.FirstChild.Tokens)
}
if node.ParentIs(ast.NodeTableCell) {
if nil != node.Next && ast.NodeText != node.Next.Type {
r.WriteString("<br /><br />")
}
return ast.WalkContinue
}
if withoutKramdownBlockIAL(r, node) {
r.Newline()
}
inTightList := false
lastListItemLastPara := false
if parent := node.Parent; nil != parent {
if ast.NodeListItem == parent.Type { // ListItem.Paragraph
listItem := parent
if nil != listItem.Parent && nil != listItem.Parent.ListData {
// 必须通过列表(而非列表项)上的紧凑标识判断,因为在设置该标识时仅设置了 List.Tight
// 设置紧凑标识的具体实现可参考函数 List.Finalize()
inTightList = listItem.Parent.ListData.Tight
if nextItem := listItem.Next; nil == nextItem {
nextPara := node.Next
lastListItemLastPara = nil == nextPara
}
} else {
inTightList = true
}
}
}
if !inTightList || (lastListItemLastPara) {
if withoutKramdownBlockIAL(r, node) {
r.WriteByte(lex.ItemNewline)
}
}
}
return ast.WalkContinue
}
func withoutKramdownBlockIAL(r *render.FormatRenderer, node *ast.Node) bool {
return !r.Options.KramdownBlockIAL || 0 == len(node.KramdownIAL)
}
func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros bool) (ret *parse.Tree) {
luteEngine := NewLute()
ret = tree