-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement handling todo items with checkboxes
- Loading branch information
Showing
6 changed files
with
67 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "Courier New", 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, "Courier New", 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters