Skip to content

Commit

Permalink
Wrap title value in quotes in the frontmatter metadata
Browse files Browse the repository at this point in the history
Added a new function `quote` in the frontmatter template, to safely
print the value surrounded with quote marks and correctly
escape the quotes in the original string.
Fixes #111
  • Loading branch information
wormi4ok committed Dec 24, 2023
1 parent 2fd3e57 commit 61ea88a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion internal/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
const FrontMatterTemplate = `---
date: '{{.CTime}}'
updated_at: '{{.MTime}}'
title: {{ trim .Title }}
title: {{ trim .Title | quote }}
{{- if .TagList }}
tags: [ {{ .TagList }} ]
{{- end -}}
Expand Down Expand Up @@ -185,6 +185,9 @@ func (c *Converter) addFrontMatter(note *enex.Note, md *markdown.Note) {
"trim": func(text string) string {
return strings.TrimSpace(text)
},
"quote": func(text string) string {
return fmt.Sprintf("%q", text)
},
}).Parse(c.FrontMatterTemplate)
if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion internal/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestConvert(t *testing.T) {
{
name: "FrontMatter",
arg: &enex.Note{
Title: "Test note",
Title: "Test \"note",
Content: goldenFile(t, "evernote.html"),
Created: "20121202T112233Z",
Updated: "20201220T223344Z",
Expand Down
4 changes: 2 additions & 2 deletions internal/testdata/golden-frontmatter.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
date: '2012-12-02 11:22:33 +0000'
updated_at: '2020-12-20 22:33:44 +0000'
title: Test note
title: "Test \"note"
tags: [ 'tag1', 'tag2' ]
latitude: 50.00000000000000
longitude: 30.00000000000000
source: mobile.android

---

# Test note
# Test "note

`tag1` `tag2`

Expand Down

0 comments on commit 61ea88a

Please sign in to comment.