Skip to content

Commit

Permalink
BREAKING CHANGE: Turn off escaping of the spacial characters by default
Browse files Browse the repository at this point in the history
Use --escape-special-chars flag to fall back to previous behaviour.

Fixes #100
  • Loading branch information
wormi4ok committed Dec 31, 2023
1 parent 888e1a1 commit 9c9153a
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 15 deletions.
7 changes: 5 additions & 2 deletions encoding/markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ 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 {
func Convert(w io.Writer, r io.Reader, highlights, escapeSpecialChars bool) error {
rules := []godown.CustomRule{
&TodoItem{}, // Handling checkboxes is always enabled
}
Expand All @@ -46,5 +46,8 @@ func Convert(w io.Writer, r io.Reader, highlights bool) error {
rules = append(rules, &HighlightedText{})
}

return godown.Convert(w, r, &godown.Option{CustomRules: rules})
return godown.Convert(w, r, &godown.Option{
CustomRules: rules,
DoNotEscape: !escapeSpecialChars,
})
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ require (
github.com/fatih/color v1.15.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
)

replace github.com/mattn/godown => github.com/wormi4ok/godown v0.4.0
replace github.com/mattn/godown => github.com/wormi4ok/godown v0.5.0
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand All @@ -33,8 +33,8 @@ github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NF
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/wormi4ok/godown v0.4.0 h1:baxURAO0WuaEd06fo5WkgosHUwjQSJuV3wnnx28G0zs=
github.com/wormi4ok/godown v0.4.0/go.mod h1:LPSYhfL9Q6QZXNyT43NPdZzY3Yb7qKsbjM9ke/SR4+A=
github.com/wormi4ok/godown v0.5.0 h1:YUbB0EasyHSha3drOQ0yj6thinsGgLh2azPfTB1FDDw=
github.com/wormi4ok/godown v0.5.0/go.mod h1:c6bBSlINjMU1cDpiBxWDXJ7sRdUx+frNvBzEk98Haec=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
13 changes: 10 additions & 3 deletions internal/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ source: {{ trim .Source -}}
type Converter struct {
TagTemplate string
EnableHighlights bool
EscapeSpecialChars bool
EnableFrontMatter bool
FrontMatterTemplate string

Expand All @@ -58,7 +59,7 @@ type Converter struct {
}

// NewConverter creates a Converter with valid tagTemplate
func NewConverter(tagTemplate string, enableFrontMatter bool, enableHighlights bool) (*Converter, error) {
func NewConverter(tagTemplate string, enableFrontMatter, enableHighlights, escapeSpecialChars bool) (*Converter, error) {
if tagTemplate == "" {
tagTemplate = DefaultTagTemplate
}
Expand All @@ -67,7 +68,13 @@ func NewConverter(tagTemplate string, enableFrontMatter bool, enableHighlights b
return nil, errors.New("tag format should contain exactly one {{tag}} template variable")
}

return &Converter{TagTemplate: tagTemplate, EnableFrontMatter: enableFrontMatter, FrontMatterTemplate: FrontMatterTemplate, EnableHighlights: enableHighlights}, nil
return &Converter{
TagTemplate: tagTemplate,
EnableHighlights: enableHighlights,
EscapeSpecialChars: escapeSpecialChars,
EnableFrontMatter: enableFrontMatter,
FrontMatterTemplate: FrontMatterTemplate,
}, nil
}

// Convert an Evernote file to markdown
Expand Down Expand Up @@ -139,7 +146,7 @@ func (c *Converter) toMarkdown(note *enex.Note, md *markdown.Note) {
return
}
var b bytes.Buffer
err := markdown.Convert(&b, bytes.NewReader(note.Content), c.EnableHighlights)
err := markdown.Convert(&b, bytes.NewReader(note.Content), c.EnableHighlights, c.EscapeSpecialChars)
if c.err = err; err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestConvert(t *testing.T) {
},
}
for _, tt := range tests {
c, _ := internal.NewConverter("", tt.enableFrontMatter, true)
c, _ := internal.NewConverter("", tt.enableFrontMatter, true, true)
t.Run(tt.name, func(t *testing.T) {
got, err := c.Convert(tt.arg)
if (err != nil) != tt.wantErr {
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
var input, outputOverride string
var outputDir = filepath.FromSlash("./notes")
var tagTemplate = internal.DefaultTagTemplate
var folders, noHighlights, resetTimestamps, addFrontMatter, debug bool
var folders, noHighlights, escapeSpecialChars, resetTimestamps, addFrontMatter, debug bool

flaggy.AddPositionalValue(&input, "input", 1, true, "Evernote export file, directory or a glob pattern")
flaggy.AddPositionalValue(&outputDir, "output", 2, false, "Output directory")
Expand All @@ -50,6 +50,7 @@ func main() {

flaggy.Bool(&folders, "", "folders", "Put every note in a separate folder")
flaggy.Bool(&noHighlights, "", "noHighlights", "Disable converting Evernote highlights to inline HTML tags")
flaggy.Bool(&escapeSpecialChars, "", "escape-special-chars", "Escape special characters to ensure correct rendering of the converted files")
flaggy.Bool(&resetTimestamps, "", "resetTimestamps", "Create files ignoring timestamps in the note attributes")
flaggy.Bool(&addFrontMatter, "", "addFrontMatter", "Prepend FrontMatter to markdown files")
flaggy.Bool(&debug, "v", "debug", "Show debug output")
Expand All @@ -63,7 +64,7 @@ func main() {
files, err := matchInput(input)
failWhen(err)
output := newNoteFilesDir(outputDir, folders, !resetTimestamps)
converter, err := internal.NewConverter(tagTemplate, addFrontMatter, !noHighlights)
converter, err := internal.NewConverter(tagTemplate, addFrontMatter, !noHighlights, escapeSpecialChars)
failWhen(err)

setLogLevel(debug)
Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_run(t *testing.T) {
}
files, _ := matchInput(input)
output := newNoteFilesDir(tmpDir, false, false)
converter, _ := internal.NewConverter("", true, false)
converter, _ := internal.NewConverter("", true, false, true)
run(files, output, newSpinner(true), converter)

want := filepath.Join(output.Path(), "Test.md")
Expand Down

0 comments on commit 9c9153a

Please sign in to comment.