Skip to content

Commit

Permalink
Fix panic in case of a nested en-codeblock sections
Browse files Browse the repository at this point in the history
  • Loading branch information
wormi4ok committed Sep 12, 2021
1 parent b55a064 commit 2fba7fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion internal/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"golang.org/x/net/html"
"golang.org/x/net/html/atom"

"github.com/wormi4ok/evernote2md/encoding/enex"
"github.com/wormi4ok/evernote2md/encoding/markdown"
Expand Down Expand Up @@ -105,7 +106,7 @@ func appendMedia(node, media *html.Node) {
func parseOne(h string, context *html.Node) *html.Node {
nodes, err := html.ParseFragment(strings.NewReader(h), context)
if err != nil {
panic("parseHtml: " + err.Error())
panic("parseOne: " + err.Error())
}
return nodes[0]
}
Expand All @@ -132,6 +133,7 @@ func (r *Code) ReplaceTag(n *html.Node) {
}
f(n)
n.Data = "pre"
n.DataAtom = atom.Pre
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/evernote.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p>abc <span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">highlighted text</span></p><p>Some <span style="font-style: italic">italic text</span></p><p>Some <span style="font-weight: bold">bold text</span></p></p><a href="https://petrashov.ru"></a><en-media type="image/jpeg" hash="c9e6c70ea74388346ffa16ff8edbdf58"/><en-media type="image/jpeg" hash="1sdb49hgt574388346ffa19kh3edbdf09"/><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;"><div> //This is a code block</div><div>    fmt.Println("hello world")</div></div><ul><li><div>First item</div></li><li><div><span>Second item</span></div></li><ul><li>Nested item</li></ul></ul><div><br/></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 130px;"/><col style="width: 130px;"/><col style="width: 130px;"/></colgroup><tbody><tr><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Header 1</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Middle column</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Last column title</div></td></tr><tr><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Short text</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Verylongunbreakabletext</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Something here</div></td></tr><tr><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Half empty row</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr></tbody></table>
<p>abc <span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">highlighted text</span></p><p>Some <span style="font-style: italic">italic text</span></p><p>Some <span style="font-weight: bold">bold text</span></p></p><a href="https://petrashov.ru"></a><en-media type="image/jpeg" hash="c9e6c70ea74388346ffa16ff8edbdf58"/><en-media type="image/jpeg" hash="1sdb49hgt574388346ffa19kh3edbdf09"/><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);-en-codeblock:true;"><div> //This is a code block</div><div>    fmt.Println("hello world")</div></div><ul><li><div>First item</div></li><li><div><span>Second item</span></div></li><ul><li>Nested item</li></ul></ul><div><br/></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 130px;"/><col style="width: 130px;"/><col style="width: 130px;"/></colgroup><tbody><tr><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Header 1</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Middle column</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Last column title</div></td></tr><tr><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Short text</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Verylongunbreakabletext</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Something here</div></td></tr><tr><td style="width: 130px; padding: 8px; border: 1px solid;"><div>Half empty row</div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 130px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr></tbody></table><div style="-en-codeblock:true"><div style="-en-codeblock:true"></div></div>
4 changes: 4 additions & 0 deletions internal/testdata/golden-frontmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ Some **bold text**
|--------------|-----------------------|-----------------|
|Short text |Verylongunbreakabletext|Something here |
|Half empty row| | |

```
```
4 changes: 4 additions & 0 deletions internal/testdata/golden.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ Some **bold text**
|--------------|-----------------------|-----------------|
|Short text |Verylongunbreakabletext|Something here |
|Half empty row| | |

```
```

0 comments on commit 2fba7fa

Please sign in to comment.