Skip to content

Commit

Permalink
✨ Support for inserting custom blocks siyuan-note/siyuan#8418
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 2, 2023
1 parent d3e4735 commit ba13714
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 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.

13 changes: 11 additions & 2 deletions parse/custom_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/88250/lute/lex"
)

// CustomBlockStart 判断围栏自定义块(;;;)是否开始。
// CustomBlockStart 判断围栏自定义块(;;;info)是否开始。
func CustomBlockStart(t *Tree, container *ast.Node) int {
if t.Context.indented {
return 0
Expand Down Expand Up @@ -92,15 +92,24 @@ func (t *Tree) parseCustomBlock() (ok bool, fenceOffset int, info string) {
}

infoTokens := t.Context.currentLine[t.Context.nextNonspace+fenceLen:]
if lex.ItemSemicolon == marker && 0 < bytes.IndexByte(infoTokens, lex.ItemSemicolon) {
if 0 < bytes.IndexByte(infoTokens, lex.ItemSemicolon) {
// info 部分不能包含 ;
return
}

if !bytes.HasSuffix(infoTokens, []byte("\n")) {
return
}

info = string(lex.TrimWhitespace(infoTokens))
info = html.UnescapeString(info)
if idx := strings.IndexByte(info, ' '); 0 <= idx {
info = info[:idx]
}
if 1 > len(strings.ReplaceAll(info, editor.Caret, "")) {
return
}

return true, t.Context.indent, info
}

Expand Down
12 changes: 9 additions & 3 deletions protyle.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,22 @@ func (lute *Lute) BlockDOM2EscapeMarkerContent(htmlStr string) (text string) {

func (lute *Lute) Tree2BlockDOM(tree *parse.Tree, options *render.Options) (vHTML string) {
renderer := render.NewProtyleRenderer(tree, options)
for nodeType, rendererFunc := range lute.Md2BlockDOMRendererFuncs {
renderer.ExtRendererFuncs[nodeType] = rendererFunc
}
output := renderer.Render()
vHTML = util.BytesToStr(output)
vHTML = strings.ReplaceAll(vHTML, editor.Caret, "<wbr>")
return
}

func RenderNodeBlockDOM(node *ast.Node, parseOptions *parse.Options, renderOptions *render.Options) string {
func (lute *Lute) RenderNodeBlockDOM(node *ast.Node) string {
root := &ast.Node{Type: ast.NodeDocument}
tree := &parse.Tree{Root: root, Context: &parse.Context{ParseOption: parseOptions}}
renderer := render.NewProtyleRenderer(tree, renderOptions)
tree := &parse.Tree{Root: root, Context: &parse.Context{ParseOption: lute.ParseOptions}}
renderer := render.NewProtyleRenderer(tree, lute.RenderOptions)
for nodeType, rendererFunc := range lute.Md2BlockDOMRendererFuncs {
renderer.ExtRendererFuncs[nodeType] = rendererFunc
}
renderer.Writer = &bytes.Buffer{}
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
rendererFunc := renderer.RendererFuncs[n.Type]
Expand Down
1 change: 1 addition & 0 deletions test/m2p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

var md2BlockDOMTests = []parseTest{

{"67", ";;;‸", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">;;;‸</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"66", ";;;info\nfoo&bar\"baz\n;;;", "<div data-type=\"NodeCustomBlock\" data-info=\"info\" data-content=\"foo&amp;bar&quot;baz\n\" data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeCustomBlock\" class=\"custom-block\" updated=\"20060102150405\"><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>\n"},
{"65", "[`foo`](bar \"baz\")", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">\u200b<span data-type=\"a code\" data-href=\"bar\" data-title=\"baz\">\u200bfoo</span>\u200b</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"64", "**[foo](bar \"baz\")**", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"strong a\" data-href=\"bar\" data-title=\"baz\">foo</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
Expand Down
1 change: 1 addition & 0 deletions test/spin_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

var spinBlockDOMTests = []*parseTest{

{"195", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">;;;‸</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>", "<div data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20060102150405\"><div contenteditable=\"true\" spellcheck=\"false\">;;;<wbr></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"194", "<div data-type=\"NodeCustomBlock\" data-info=\"info\" data-content=\"foo&amp;bar&quot;baz\n\" data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeCustomBlock\" class=\"custom-block\" updated=\"20060102150405\"><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>\n", "<div data-type=\"NodeCustomBlock\" data-info=\"info\" data-content=\"foo&amp;bar&quot;baz\n\" data-node-id=\"20060102150405-1a2b3c4\" data-node-index=\"1\" data-type=\"NodeCustomBlock\" class=\"custom-block\" updated=\"20060102150405\"><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>\n"},
{"193", "<div data-node-id=\"20230528220118-eyrael8\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20230528220122\"><div contenteditable=\"true\" spellcheck=\"false\">$<wbr>foo$</div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "<div data-node-id=\"20230528220118-eyrael8\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20230528220122\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo\" contenteditable=\"false\" class=\"render-node\"></span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"192", "<div data-node-id=\"20230507173424-vvsztt2\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20230507223334\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo&amp;quot;bar&amp;quot;baz\" contenteditable=\"false\" class=\"render-node\" data-render=\"true\"><span class=\"katex\"><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8889em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.10764em;\">f</span><span class=\"mord mathnormal\">oo</span><span class=\"mord\">\"</span><span class=\"mord mathnormal\">ba</span><span class=\"mord mathnormal\" style=\"margin-right:0.02778em;\">r</span><span class=\"mord\">\"</span><span class=\"mord mathnormal\">ba</span><span class=\"mord mathnormal\" style=\"margin-right:0.04398em;\">z</span></span></span></span></span>1<wbr>​</div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "<div data-node-id=\"20230507173424-vvsztt2\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20230507223334\"><div contenteditable=\"true\" spellcheck=\"false\"><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo&amp;quot;bar&amp;quot;baz\" contenteditable=\"false\" class=\"render-node\"></span> 1<wbr></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
Expand Down

0 comments on commit ba13714

Please sign in to comment.