Skip to content

Commit

Permalink
🎨 Protyle Inline formulas and memo support preserving line breaks siy…
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 17, 2023
1 parent f00e74c commit a3955cb
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 60 deletions.
17 changes: 13 additions & 4 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"
"unicode/utf8"

"github.com/88250/lute/editor"
"github.com/88250/lute/html"
"github.com/88250/lute/lex"
"github.com/88250/lute/util"
Expand Down Expand Up @@ -441,10 +442,14 @@ func (n *Node) Content() (ret string) {
buf.WriteString(n.TextMarkTextContent)
}
} else if "" != n.TextMarkInlineMathContent {
buf.WriteString(n.TextMarkInlineMathContent)
content := n.TextMarkInlineMathContent
content = strings.ReplaceAll(content, editor.CaretNewline, " ")
buf.WriteString(content)
}
if "" != n.TextMarkInlineMemoContent {
buf.WriteString(n.TextMarkInlineMemoContent)
content := n.TextMarkInlineMathContent
content = strings.ReplaceAll(content, editor.CaretNewline, " ")
buf.WriteString(content)
}
}
return WalkContinue
Expand Down Expand Up @@ -477,9 +482,13 @@ func (n *Node) Stat() (runeCnt, wordCnt, linkCnt, imgCnt, refCnt int) {
if 0 < len(n.TextMarkTextContent) {
buf = append(buf, n.TextMarkTextContent...)
} else if 0 < len(n.TextMarkInlineMathContent) {
buf = append(buf, n.TextMarkInlineMathContent...)
content := n.TextMarkInlineMathContent
content = strings.ReplaceAll(content, editor.CaretNewline, " ")
buf = append(buf, content...)
} else if "" != n.TextMarkInlineMemoContent {
buf = append(buf, n.TextMarkInlineMemoContent...)
content := n.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, " ")
buf = append(buf, content...)
}

if n.IsTextMarkType("a") {
Expand Down
10 changes: 5 additions & 5 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.

2 changes: 1 addition & 1 deletion parse/inline_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func SetTextMarkNode(node *ast.Node, n *html.Node, options *Options) {
node.TextMarkInlineMemoContent = util.GetTextMarkInlineMemoData(n)
inlineTree := Inline("", []byte(node.TextMarkInlineMemoContent), options)
if nil != inlineTree {
node.TextMarkInlineMemoContent = strings.ReplaceAll(inlineTree.Root.Content(), "\n", "")
node.TextMarkInlineMemoContent = strings.ReplaceAll(inlineTree.Root.Content(), "\n", editor.IALValEscNewLine)
node.TextMarkInlineMemoContent = strings.ReplaceAll(node.TextMarkInlineMemoContent, "\"", "&quot;")
}
default:
Expand Down
22 changes: 13 additions & 9 deletions render/html_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"unicode"
"unicode/utf8"

"github.com/88250/lute/html"

"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/util"
Expand Down Expand Up @@ -228,7 +228,9 @@ func (r *HtmlRenderer) renderTextMark(node *ast.Node, entering bool) ast.WalkSta
lastRune, _ := utf8.DecodeLastRuneInString(node.TextMarkTextContent)
if isCJK(lastRune) {
r.WriteString("<sup>(")
r.WriteString(node.TextMarkInlineMemoContent)
memo := node.TextMarkInlineMemoContent
memo = strings.ReplaceAll(memo, editor.IALValEscNewLine, " ")
r.WriteString(memo)
r.WriteString(")</sup>")
} else {
r.WriteString("<sup>(")
Expand Down Expand Up @@ -1399,20 +1401,22 @@ func (r *HtmlRenderer) renderTextMarkAttrs(node *ast.Node) (attrs [][]string) {
}
} else if "inline-math" == typ {
attrs = append(attrs, []string{"data-subtype", "math"})
inlineMathContent := node.TextMarkInlineMathContent
content := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = strings.ReplaceAll(inlineMathContent, "|", "&#124;")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
content = strings.ReplaceAll(content, "|", "&#124;")
content = strings.ReplaceAll(content, "\n", "<br/>")
}
attrs = append(attrs, []string{"data-content", inlineMathContent})
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-content", content})
attrs = append(attrs, []string{"contenteditable", "false"})
attrs = append(attrs, []string{"class", "render-node"})
} else if "file-annotation-ref" == typ {
attrs = append(attrs, []string{"data-id", node.TextMarkFileAnnotationRefID})
} else if "inline-memo" == typ {
inlineMemoContent := node.TextMarkInlineMemoContent
attrs = append(attrs, []string{"data-inline-memo-content", inlineMemoContent})
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-inline-memo-content", content})
}
}
return
Expand Down
8 changes: 6 additions & 2 deletions render/protyle_export_docx_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,13 @@ func (r *ProtyleExportDocxRenderer) renderHTMLTag0(node *ast.Node, currentTextMa
func (r *ProtyleExportDocxRenderer) getTextMarkTextContent(node *ast.Node) (ret string) {
ret = node.TextMarkTextContent
if node.IsTextMarkType("a") || node.IsTextMarkType("block-ref") || node.IsTextMarkType("file-annotation-ref") {
ret = node.TextMarkTextContent
content := node.TextMarkTextContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, " ")
ret = content
} else if node.IsTextMarkType("inline-memo") {
ret = node.TextMarkInlineMemoContent
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, " ")
ret = content
} else if node.IsTextMarkType("inline-math") {
ret = node.TextMarkInlineMathContent
}
Expand Down
30 changes: 18 additions & 12 deletions render/protyle_export_md_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,19 +300,22 @@ func (r *ProtyleExportMdRenderer) renderMdMarker(node *ast.Node, entering bool)
case "inline-memo":
lastRune, _ := utf8.DecodeLastRuneInString(node.TextMarkTextContent)
ret += node.TextMarkTextContent
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, " ")
if isCJK(lastRune) {
ret += "^(" + node.TextMarkInlineMemoContent + ")^"
ret += "^(" + content + ")^"
} else {
ret += "^(" + node.TextMarkInlineMemoContent + ")^"
ret += "^(" + content + ")^"
}
case "inline-math":
inlineMathContent := node.TextMarkInlineMathContent
content := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = lex.RepeatBackslashBeforePipe(inlineMathContent)
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
content = lex.RepeatBackslashBeforePipe(content)
content = strings.ReplaceAll(content, "\n", "<br/>")
}
ret += "$" + inlineMathContent + "$"
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
ret += "$" + content + "$"
}

for _, typ := range types {
Expand Down Expand Up @@ -388,21 +391,24 @@ func (r *ProtyleExportMdRenderer) renderMdMarker0(node *ast.Node, currentTextmar
if entering {
lastRune, _ := utf8.DecodeLastRuneInString(node.TextMarkTextContent)
ret += node.TextMarkTextContent
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, " ")
if isCJK(lastRune) {
ret += "^(" + node.TextMarkInlineMemoContent + ")^"
ret += "^(" + content + ")^"
} else {
ret += "^(" + node.TextMarkInlineMemoContent + ")^"
ret += "^(" + content + ")^"
}
}
case "inline-math":
if entering {
inlineMathContent := node.TextMarkInlineMathContent
content := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = lex.RepeatBackslashBeforePipe(inlineMathContent)
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
content = lex.RepeatBackslashBeforePipe(content)
content = strings.ReplaceAll(content, "\n", "<br/>")
}
ret += "$" + inlineMathContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
ret += "$" + content
} else {
ret += "$"
}
Expand Down
22 changes: 14 additions & 8 deletions render/protyle_export_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ func (r *ProtyleExportRenderer) renderTextMark(node *ast.Node, entering bool) as
lastRune, _ := utf8.DecodeLastRuneInString(node.TextMarkTextContent)
if isCJK(lastRune) {
r.WriteString("<sup>(")
r.WriteString(node.TextMarkInlineMemoContent)
memo := node.TextMarkInlineMemoContent
memo = strings.ReplaceAll(memo, editor.IALValEscNewLine, " ")
r.WriteString(memo)
r.WriteString(")</sup>")
} else {
r.WriteString("<sup>(")
r.WriteString(node.TextMarkInlineMemoContent)
memo := node.TextMarkInlineMemoContent
memo = strings.ReplaceAll(memo, editor.IALValEscNewLine, " ")
r.WriteString(memo)
r.WriteString(")</sup>")
}
} else {
Expand Down Expand Up @@ -1757,20 +1761,22 @@ func (r *ProtyleExportRenderer) renderTextMarkAttrs(node *ast.Node) (attrs [][]s
}
} else if "inline-math" == typ {
attrs = append(attrs, []string{"data-subtype", "math"})
inlineMathContent := node.TextMarkInlineMathContent
content := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = strings.ReplaceAll(inlineMathContent, "|", "&#124;")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
content = strings.ReplaceAll(content, "|", "&#124;")
content = strings.ReplaceAll(content, "\n", "<br/>")
}
attrs = append(attrs, []string{"data-content", inlineMathContent})
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-content", content})
attrs = append(attrs, []string{"contenteditable", "false"})
attrs = append(attrs, []string{"class", "render-node"})
} else if "file-annotation-ref" == typ {
attrs = append(attrs, []string{"data-id", node.TextMarkFileAnnotationRefID})
} else if "inline-memo" == typ {
inlineMemoContent := node.TextMarkInlineMemoContent
attrs = append(attrs, []string{"data-inline-memo-content", inlineMemoContent})
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-inline-memo-content", content})
}
}
return
Expand Down
22 changes: 14 additions & 8 deletions render/protyle_preview_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,15 @@ func (r *ProtylePreviewRenderer) renderTextMark(node *ast.Node, entering bool) a
lastRune, _ := utf8.DecodeLastRuneInString(node.TextMarkTextContent)
if isCJK(lastRune) {
r.WriteString("<sup>(")
r.WriteString(node.TextMarkInlineMemoContent)
memo := node.TextMarkInlineMemoContent
memo = strings.ReplaceAll(memo, editor.IALValEscNewLine, " ")
r.WriteString(memo)
r.WriteString(")</sup>")
} else {
r.WriteString("<sup>(")
r.WriteString(node.TextMarkInlineMemoContent)
memo := node.TextMarkInlineMemoContent
memo = strings.ReplaceAll(memo, editor.IALValEscNewLine, " ")
r.WriteString(memo)
r.WriteString(")</sup>")
}
} else {
Expand Down Expand Up @@ -1394,20 +1398,22 @@ func (r *ProtylePreviewRenderer) renderTextMarkAttrs(node *ast.Node) (attrs [][]
}
} else if "inline-math" == typ {
attrs = append(attrs, []string{"data-subtype", "math"})
inlineMathContent := node.TextMarkInlineMathContent
content := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = strings.ReplaceAll(inlineMathContent, "|", "&#124;")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
content = strings.ReplaceAll(content, "|", "&#124;")
content = strings.ReplaceAll(content, "\n", "<br/>")
}
attrs = append(attrs, []string{"data-content", inlineMathContent})
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-content", content})
attrs = append(attrs, []string{"contenteditable", "false"})
attrs = append(attrs, []string{"class", "render-node"})
} else if "file-annotation-ref" == typ {
attrs = append(attrs, []string{"data-id", node.TextMarkFileAnnotationRefID})
} else if "inline-memo" == typ {
inlineMemoContent := node.TextMarkInlineMemoContent
attrs = append(attrs, []string{"data-inline-memo-content", inlineMemoContent})
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-inline-memo-content", content})
}
}
return
Expand Down
18 changes: 10 additions & 8 deletions render/protyle_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1868,23 +1868,25 @@ func (r *ProtyleRenderer) renderTextMarkAttrs(node *ast.Node) (attrs [][]string)
}
} else if "inline-math" == typ {
attrs = append(attrs, []string{"data-subtype", "math"})
inlineMathContent := node.TextMarkInlineMathContent
content := node.TextMarkInlineMathContent
if node.ParentIs(ast.NodeTableCell) {
// Improve the handling of inline-math containing `|` in the table https://github.com/siyuan-note/siyuan/issues/9227
inlineMathContent = strings.ReplaceAll(inlineMathContent, "|", "&#124;")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\n", "<br/>")
content = strings.ReplaceAll(content, "|", "&#124;")
content = strings.ReplaceAll(content, "\n", "<br/>")
}
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
// Improve inline formulas input https://github.com/siyuan-note/siyuan/issues/8972
//inlineMathContent = strings.ReplaceAll(inlineMathContent, editor.Caret, "")
inlineMathContent = strings.ReplaceAll(inlineMathContent, "\"", "&amp;quot;")
attrs = append(attrs, []string{"data-content", inlineMathContent})
//content = strings.ReplaceAll(inlineMathContent, editor.Caret, "")
content = strings.ReplaceAll(content, "\"", "&amp;quot;")
attrs = append(attrs, []string{"data-content", content})
attrs = append(attrs, []string{"contenteditable", "false"})
attrs = append(attrs, []string{"class", "render-node"})
} else if "file-annotation-ref" == typ {
attrs = append(attrs, []string{"data-id", node.TextMarkFileAnnotationRefID})
} else if "inline-memo" == typ {
inlineMemoContent := node.TextMarkInlineMemoContent
attrs = append(attrs, []string{"data-inline-memo-content", inlineMemoContent})
content := node.TextMarkInlineMemoContent
content = strings.ReplaceAll(content, editor.IALValEscNewLine, "\n")
attrs = append(attrs, []string{"data-inline-memo-content", content})
}
}
return
Expand Down
Loading

0 comments on commit a3955cb

Please sign in to comment.