Skip to content

Commit

Permalink
Implement handling todo items with checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
wormi4ok committed Aug 6, 2022
1 parent f70373c commit 5d54d69
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
32 changes: 4 additions & 28 deletions encoding/markdown/markdown.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package markdown

import (
"fmt"
"io"
"strings"
"time"

"github.com/mattn/godown"
"golang.org/x/net/html"
)

// ResourceType gives a hint on the way to represent Resource
Expand Down Expand Up @@ -41,34 +38,13 @@ type (
// Convert wraps a call to external dependency to provide
// stable interface for package users
func Convert(w io.Writer, r io.Reader, highlights bool) error {
var rules []godown.CustomRule
rules := []godown.CustomRule{
&TodoItem{}, // Handling checkboxes is always enabled
}

if highlights {
rules = append(rules, &HighlightedText{})
}

return godown.Convert(w, r, &godown.Option{CustomRules: rules})
}

// HighlightedText is a parsing rule to convert Evernote highlights to HTML spans with a background color
type HighlightedText struct{}

// Rule implements godown.CustomRule interface to extend basic conversion rules and
// convert text highlighted in Evernote to an inline HTML `span` tag with a custom background color
func (r *HighlightedText) Rule(next godown.WalkFunc) (string, godown.WalkFunc) {
return "span", func(node *html.Node, w io.Writer, nest int, option *godown.Option) {
if node.Attr == nil {
next(node, w, nest, option)
return
}

for _, attr := range node.Attr {
if attr.Key == "style" && strings.Contains(attr.Val, "-evernote-highlight:true") {
_, _ = fmt.Fprint(w, `<span style="background-color: #ffaaaa">`)
next(node, w, nest, option)
_, _ = fmt.Fprint(w, "</span>")
} else {
next(node, w, nest, option)
}
}
}
}
53 changes: 53 additions & 0 deletions encoding/markdown/rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package markdown

import (
"fmt"
"io"
"strings"

"github.com/mattn/godown"
"golang.org/x/net/html"
)

// HighlightedText is a parsing rule to convert Evernote highlights to HTML spans with a background color
type HighlightedText struct{}

// Rule implements godown.CustomRule interface to extend basic conversion rules and
// convert text highlighted in Evernote to an inline HTML `span` tag with a custom background color
func (r *HighlightedText) Rule(next godown.WalkFunc) (string, godown.WalkFunc) {
return "span", func(node *html.Node, w io.Writer, nest int, option *godown.Option) {
if node.Attr == nil {
next(node, w, nest, option)
return
}

for _, attr := range node.Attr {
if attr.Key == "style" && strings.Contains(attr.Val, "-evernote-highlight:true") {
_, _ = fmt.Fprint(w, `<span style="background-color: #ffaaaa">`)
next(node, w, nest, option)
_, _ = fmt.Fprint(w, "</span>")
} else {
next(node, w, nest, option)
}
}
}
}

// TodoItem is a parsing rule to convert Evernote checkboxes to corresponding GitHub Flavoured Markdown items
type TodoItem struct{}

// Rule implements godown.CustomRule interface to handle Evernote-specific "en-todo" tag
// It converts the tag to a Markdown format with correct "checked" state
func (r TodoItem) Rule(next godown.WalkFunc) (string, godown.WalkFunc) {
return "en-todo", func(node *html.Node, w io.Writer, nest int, option *godown.Option) {
for _, attr := range node.Attr {
if attr.Key == "checked" && attr.Val == "true" {
_, _ = fmt.Fprint(w, "[x] ")
next(node, w, nest, option)
return
}
}
_, _ = fmt.Fprint(w, "[ ] ")
next(node, w, nest, option)
}
}
1 change: 1 addition & 0 deletions internal/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/sergi/go-diff/diffmatchpatch"

"github.com/wormi4ok/evernote2md/encoding/enex"
"github.com/wormi4ok/evernote2md/encoding/markdown"
"github.com/wormi4ok/evernote2md/internal"
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><div style="-en-codeblock:true"><div style="-en-codeblock:true"></div></div>
<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><ul style=""><li><div><en-todo checked="true"/>Collect underpants</div></li><li style=""><div><en-todo checked="false"/>???</div></li><ul style=""><li style=""><div><en-todo checked="false"/>Profit</div></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 @@ -32,6 +32,10 @@ Some **bold text**
- Second item
- Nested item

- [x] Collect underpants
- [ ] ???
- [ ] Profit

|Header 1 |Middle column |Last column title|
|--------------|-----------------------|-----------------|
|Short text |Verylongunbreakabletext|Something here |
Expand Down
4 changes: 4 additions & 0 deletions internal/testdata/golden.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Some **bold text**
- Second item
- Nested item

- [x] Collect underpants
- [ ] ???
- [ ] Profit

|Header 1 |Middle column |Last column title|
|--------------|-----------------------|-----------------|
|Short text |Verylongunbreakabletext|Something here |
Expand Down

0 comments on commit 5d54d69

Please sign in to comment.