Skip to content

Commit

Permalink
🎨 Vditor 支持 支持类似 Typora 的及时渲染模式(保留 Markdown 标记符) Vanessa219/vditor#27
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Mar 14, 2020
1 parent b7fb45f commit 3178ddd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 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.

17 changes: 17 additions & 0 deletions test/spinv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,20 @@ func TestSpinVditorDOM(t *testing.T) {
}
}
}

var spinVditorIRDOMTests = []*parseTest{

{"0", "<p data-block=\"0\">foo\n</p><p data-block=\"0\"><wbr><br></p>", "<h1 data-block=\"0\" data-id=\"#custom-id\" data-marker=\"#\">foo <wbr></h1>"},
}

func TestSpinVditorIRDOM(t *testing.T) {
luteEngine := lute.New()
luteEngine.ToC = true

for _, test := range spinVditorIRDOMTests {
html := luteEngine.SpinVditorIRDOM(test.from)
if test.to != html {
t.Fatalf("test case [%s] failed\nexpected\n\t%q\ngot\n\t%q\noriginal html\n\t%q", test.name, test.to, html, test.from)
}
}
}
17 changes: 17 additions & 0 deletions test/v2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,20 @@ func TestVditorDOM2Md(t *testing.T) {
}
}
}

var md2VditorIRTests = []parseTest{

{"0", "*foo*", "<p data-block=\"0\"><span class=\"vditor-ir__node\"><span class=\"vditor-ir__marker\">*</span><em>foo</em><span class=\"vditor-ir__marker\">*</span></span>\n</p>"},
}

func TestMd2VditorIR(t *testing.T) {
luteEngine := lute.New()
luteEngine.ToC = true

for _, test := range md2VditorIRTests {
md := luteEngine.Md2VditorIRDOM(test.from)
if test.to != md {
t.Fatalf("test case [%s] failed\nexpected\n\t%q\ngot\n\t%q\noriginal html\n\t%q", test.name, test.to, md, test.from)
}
}
}
31 changes: 30 additions & 1 deletion vditorir.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
package lute

import (
"bytes"
"strings"

"github.com/88250/lute/html/atom"

"github.com/88250/lute/html"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
Expand Down Expand Up @@ -119,7 +122,7 @@ func (lute *Lute) vditorIRDOM2Md(htmlStr string) (markdown string) {

var md string
for _, htmlNode := range htmlNodes {
md += lute.domText(htmlNode)
md += lute.irdomText(htmlNode)
}
tree := parse.Parse("", []byte(md), lute.Options)

Expand All @@ -130,3 +133,29 @@ func (lute *Lute) vditorIRDOM2Md(htmlStr string) (markdown string) {
markdown = string(formatted)
return
}

func (lute *Lute) irdomText(n *html.Node) string {
buf := &bytes.Buffer{}
for next := n; nil != next; next = next.NextSibling {
lute.irdomText0(next, buf)
}
return buf.String()
}

func (lute *Lute) irdomText0(n *html.Node, buffer *bytes.Buffer) {
if nil == n {
return
}
switch n.DataAtom {
case 0:
buffer.WriteString(n.Data)
case atom.Br:
buffer.WriteString("\n")
case atom.P:
buffer.WriteString("\n")
}

for child := n.FirstChild; nil != child; child = child.NextSibling {
lute.irdomText0(child, buffer)
}
}

0 comments on commit 3178ddd

Please sign in to comment.