Skip to content

Commit

Permalink
🐛 Vditor 支持 Fix Vanessa219/vditor#125
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 14, 2020
1 parent 293b851 commit 300b881
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
4 changes: 2 additions & 2 deletions blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ var blockStarts = []blockStartFunc{
// 判断分隔线(--- ***)是否开始
func(t *Tree, container *Node) int {
if !t.context.indented {
if ok, markers := t.parseThematicBreak(); ok {
if ok, caretTokens := t.parseThematicBreak(); ok {
t.context.closeUnmatchedBlocks()
thematicBreak := t.context.addChild(NodeThematicBreak, t.context.nextNonspace)
thematicBreak.tokens = markers
thematicBreak.tokens = caretTokens
t.context.advanceOffset(t.context.currentLineLen-t.context.offset, false)
return 2
}
Expand Down
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.

4 changes: 2 additions & 2 deletions test/spinv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

var spinVditorDOMTests = []*parseTest{

{"88", `<ul data-tight="true" data-marker="*" data-block="0"><li data-marker="*"><p>test</p></li></ul><ul data-tight="true" data-marker="-" data-block="0"><li data-marker="-"><p>--<wbr></p></li></ul>`, "<ul data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\" class=\"vditor-task\"><p data-block=\"0\"><input type=\"checkbox\" /> foo\n</p><ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\" class=\"vditor-task\"><input type=\"checkbox\" /> bar</li></ul><p data-block=\"0\">b<wbr>\n</p></li></ul>"},
{"88", `<ul data-tight="true" data-marker="*" data-block="0"><li data-marker="*"><p>test</p></li></ul><ul data-tight="true" data-marker="-" data-block="0"><li data-marker="-"><p>--<wbr></p></li></ul>`, "<ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\">test</li></ul><hr data-block=\"0\" /><p data-block=\"0\"><wbr>\n</p>"},
{"87", `<ul data-tight="true" data-marker="*" data-block="0"><li data-marker="*" class="vditor-task"><p><input type="checkbox"> foo</p><ul data-tight="true" data-marker="*" data-block="0"><li data-marker="*" class="vditor-task"><p><input type="checkbox"> bar</p></li></ul><p data-block="0">b<wbr></p></li></ul>`, "<ul data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\" class=\"vditor-task\"><p data-block=\"0\"><input type=\"checkbox\" /> foo\n</p><ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\" class=\"vditor-task\"><input type=\"checkbox\" /> bar</li></ul><p data-block=\"0\">b<wbr>\n</p></li></ul>"},
{"86", "<p data-block=\"0\"><strong><s>foo</s></strong>bar<wbr></p>", "<p data-block=\"0\"><strong data-marker=\"**\"><s data-marker=\"~~\">foo</s></strong>bar<wbr>\n</p>"},
{"85", "<p data-block=\"0\"><span data-marker=\"**\"><b>foo </b><span>b<wbr></span></span>", "<p data-block=\"0\"><strong data-marker=\"**\">foo</strong> b<wbr>\n</p>"},
Expand Down Expand Up @@ -56,7 +56,7 @@ var spinVditorDOMTests = []*parseTest{
{"55", "<p data-block=\"0\">[foo<wbr>](bar)\n</p>", "<p data-block=\"0\"><a href=\"bar\">foo<wbr></a>\n</p>"},
{"54", "<table data-block=\"0\"><thead><tr><th>col1</th></tr></thead><tbody><tr><td><wbr>\n</td></tr></tbody></table>", "<table data-block=\"0\"><thead><tr><th>col1</th></tr></thead><tbody><tr><td><wbr></td></tr><tr><td></td></tr></tbody></table>"},
{"53", "<table data-block=\"0\"><thead><tr><th>col1</th></tr></thead><tbody><tr><td><wbr><br></td></tr></tbody></table>", "<table data-block=\"0\"><thead><tr><th>col1</th></tr></thead><tbody><tr><td><wbr></td></tr></tbody></table>"},
{"52", "<p data-block=\"0\">---<wbr>\n</p>", "<hr data-block=\"0\" />"},
{"52", "<p data-block=\"0\">---<wbr>\n</p>", "<hr data-block=\"0\" /><p data-block=\"0\"><wbr>\n</p>"},
{"51", "<p data-block=\"0\">### <wbr>\n</p>", "<p data-block=\"0\">### <wbr>\n</p>"},
{"50", "<details open=\"\">\n<summary>foo</summary><ul data-tight=\"true\" data-block=\"0\"><li data-marker=\"*\">bar</li></ul></details>", "<div class=\"vditor-wysiwyg__block\" data-type=\"html-block\" data-block=\"0\"><pre><code>&lt;details open=&quot;&quot;&gt;\n&lt;summary&gt;foo&lt;/summary&gt;</code></pre></div><ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\">bar</li></ul><div class=\"vditor-wysiwyg__block\" data-type=\"html-block\" data-block=\"0\"><pre><code>&lt;/details&gt;</code></pre></div>"},
{"49", "<details>\n<summary>foo</summary><ul data-tight=\"true\" data-block=\"0\"><li data-marker=\"*\">bar</li></ul></details>", "<div class=\"vditor-wysiwyg__block\" data-type=\"html-block\" data-block=\"0\"><pre><code>&lt;details&gt;\n&lt;summary&gt;foo&lt;/summary&gt;</code></pre></div><ul data-tight=\"true\" data-marker=\"*\" data-block=\"0\"><li data-marker=\"*\">bar</li></ul><div class=\"vditor-wysiwyg__block\" data-type=\"html-block\" data-block=\"0\"><pre><code>&lt;/details&gt;</code></pre></div>"},
Expand Down
15 changes: 11 additions & 4 deletions thematic_break.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ package lute

import "bytes"

func (t *Tree) parseThematicBreak() (ok bool, markers []byte) {
func (t *Tree) parseThematicBreak() (ok bool, caretTokens []byte) {
markerCnt := 0
var marker byte
ln := t.context.currentLine
var caretInLn bool
if t.context.option.VditorWYSIWYG {
ln = bytes.ReplaceAll(ln, []byte(caret), []byte(""))
if bytes.Contains(ln, []byte(caret)) {
caretInLn = true
ln = bytes.ReplaceAll(ln, []byte(caret), []byte(""))
}
}

length := len(ln)
for i := t.context.nextNonspace; i < length-1; i++ {
token := ln[i]
markers = append(markers, token)
if itemSpace == token || itemTab == token {
continue
}
Expand All @@ -42,5 +45,9 @@ func (t *Tree) parseThematicBreak() (ok bool, markers []byte) {
markerCnt++
}

return 3 <= markerCnt, markers
if t.context.option.VditorWYSIWYG && caretInLn {
caretTokens = []byte(caret)
}

return 3 <= markerCnt, caretTokens
}
6 changes: 6 additions & 0 deletions vditor_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,12 @@ func (r *VditorRenderer) renderTaskListItemMarker(node *Node, entering bool) Wal

func (r *VditorRenderer) renderThematicBreak(node *Node, entering bool) WalkStatus {
r.tag("hr", [][]string{{"data-block", "0"}}, true)
if nil != node.tokens {
r.tag("p", [][]string{{"data-block", "0"}}, false)
r.writeBytes(node.tokens)
r.writeByte(itemNewline)
r.tag("/p", nil, false)
}
return WalkStop
}

Expand Down

0 comments on commit 300b881

Please sign in to comment.