From f371f58bd2bdd80f0d3588b7868df7231ec9c69c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Sat, 16 Jan 2021 20:18:03 +0100 Subject: [PATCH] Add Lead and RawContent to the saved note metadata --- adapter/sqlite/db.go | 2 + adapter/sqlite/fixtures/notes.yml | 16 +++- adapter/sqlite/note_dao.go | 40 ++++---- adapter/sqlite/note_dao_test.go | 146 +++++++++++++++++------------- core/note/index.go | 30 +++--- core/note/list.go | 32 ++++--- 6 files changed, 148 insertions(+), 118 deletions(-) diff --git a/adapter/sqlite/db.go b/adapter/sqlite/db.go index 63046dc..485f43a 100644 --- a/adapter/sqlite/db.go +++ b/adapter/sqlite/db.go @@ -51,7 +51,9 @@ func (db *DB) Migrate() error { id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, path TEXT NOT NULL, title TEXT DEFAULT('') NOT NULL, + lead TEXT DEFAULT('') NOT NULL, body TEXT DEFAULT('') NOT NULL, + raw_content TEXT DEFAULT('') NOT NULL, word_count INTEGER DEFAULT(0) NOT NULL, checksum TEXT NOT NULL, created DATETIME DEFAULT(CURRENT_TIMESTAMP) NOT NULL, diff --git a/adapter/sqlite/fixtures/notes.yml b/adapter/sqlite/fixtures/notes.yml index 869166e..f93a3aa 100644 --- a/adapter/sqlite/fixtures/notes.yml +++ b/adapter/sqlite/fixtures/notes.yml @@ -1,7 +1,9 @@ - id: 1 path: "log/2021-01-03.md" title: "January 3, 2021" - body: "A daily note" + lead: "A daily note" + body: "A daily note\n\nWith lot of content" + raw_content: "# A daily note\nA daily note\n\nWith lot of content" word_count: 3 checksum: "qwfpgj" created: "2020-11-22T16:27:45Z" @@ -10,7 +12,9 @@ - id: 2 path: "log/2021-01-04.md" title: "January 4, 2021" + lead: "A second daily note" body: "A second daily note" + raw_content: "# A second daily note" word_count: 4 checksum: "arstde" created: "2020-11-29T08:20:18Z" @@ -19,7 +23,9 @@ - id: 3 path: "index.md" title: "Index" + lead: "Index of the Zettelkasten" body: "Index of the Zettelkasten" + raw_content: "# Index\nIndex of the Zettelkasten" word_count: 4 checksum: "iaefhv" created: "2019-12-04T11:59:11Z" @@ -28,7 +34,9 @@ - id: 4 path: "f39c8.md" title: "An interesting note" + lead: "Its content will surprise you" body: "Its content will surprise you" + raw_content: "# An interesting note\nIts content will surprise you" word_count: 5 checksum: "irkwyc" created: "2020-01-19T10:58:41Z" @@ -37,7 +45,9 @@ - id: 5 path: "ref/test/b.md" title: "A nested note" + lead: "This one is in a sub sub directory" body: "This one is in a sub sub directory" + raw_content: "# A nested note\nThis one is in a sub sub directory" word_count: 8 checksum: "yvwbae" created: "2019-11-20T20:32:56Z" @@ -46,7 +56,9 @@ - id: 6 path: "ref/test/a.md" title: "Another nested note" + lead: "It shall appear before b.md" body: "It shall appear before b.md" + raw_content: "#Another nested note\nIt shall appear before b.md" word_count: 5 checksum: "iecywst" created: "2019-11-20T20:32:56Z" @@ -55,7 +67,9 @@ - id: 7 path: "log/2021-02-04.md" title: "February 4, 2021" + lead: "A third daily note" body: "A third daily note" + raw_content: "# A third daily note" word_count: 4 checksum: "earkte" created: "2020-11-29T08:20:18Z" diff --git a/adapter/sqlite/note_dao.go b/adapter/sqlite/note_dao.go index d7c7865..5f517c3 100644 --- a/adapter/sqlite/note_dao.go +++ b/adapter/sqlite/note_dao.go @@ -36,12 +36,12 @@ func NewNoteDAO(tx Transaction, logger util.Logger) *NoteDAO { ORDER BY path ASC `), addStmt: tx.PrepareLazy(` - INSERT INTO notes (path, title, body, word_count, checksum, created, modified) - VALUES (?, ?, ?, ?, ?, ?, ?) + INSERT INTO notes (path, title, lead, body, raw_content, word_count, checksum, created, modified) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) `), updateStmt: tx.PrepareLazy(` UPDATE notes - SET title = ?, body = ?, word_count = ?, checksum = ?, modified = ? + SET title = ?, lead = ?, body = ?, raw_content = ?, word_count = ?, checksum = ?, modified = ? WHERE path = ? `), removeStmt: tx.PrepareLazy(` @@ -94,7 +94,7 @@ func (d *NoteDAO) Indexed() (<-chan paths.Metadata, error) { func (d *NoteDAO) Add(note note.Metadata) error { _, err := d.addStmt.Exec( - note.Path, note.Title, note.Body, note.WordCount, note.Checksum, + note.Path, note.Title, note.Lead, note.Body, note.RawContent, note.WordCount, note.Checksum, note.Created, note.Modified, ) return errors.Wrapf(err, "%v: can't add note to the index", note.Path) @@ -112,7 +112,7 @@ func (d *NoteDAO) Update(note note.Metadata) error { } _, err = d.updateStmt.Exec( - note.Title, note.Body, note.WordCount, note.Checksum, note.Modified, + note.Title, note.Lead, note.Body, note.RawContent, note.WordCount, note.Checksum, note.Modified, note.Path, ) return errors.Wrapf(err, "%v: failed to update note index", note.Path) @@ -158,13 +158,13 @@ func (d *NoteDAO) Find(opts note.FinderOpts, callback func(note.Match) error) (i count++ var ( - id, wordCount int - title, body, snippet string - path, checksum string - created, modified time.Time + id, wordCount int + title, lead, body, rawContent, snippet string + path, checksum string + created, modified time.Time ) - err := rows.Scan(&id, &path, &title, &body, &wordCount, &created, &modified, &checksum, &snippet) + err := rows.Scan(&id, &path, &title, &lead, &body, &rawContent, &wordCount, &created, &modified, &checksum, &snippet) if err != nil { d.logger.Err(err) continue @@ -173,13 +173,15 @@ func (d *NoteDAO) Find(opts note.FinderOpts, callback func(note.Match) error) (i callback(note.Match{ Snippet: snippet, Metadata: note.Metadata{ - Path: path, - Title: title, - Body: body, - WordCount: wordCount, - Created: created, - Modified: modified, - Checksum: checksum, + Path: path, + Title: title, + Lead: lead, + Body: body, + RawContent: rawContent, + WordCount: wordCount, + Created: created, + Modified: modified, + Checksum: checksum, }, }) } @@ -195,7 +197,7 @@ type findQuery struct { } func (d *NoteDAO) findRows(opts note.FinderOpts) (*sql.Rows, error) { - snippetCol := `""` + snippetCol := `n.lead` whereExprs := make([]string, 0) orderTerms := make([]string, 0) args := make([]interface{}, 0) @@ -253,7 +255,7 @@ func (d *NoteDAO) findRows(opts note.FinderOpts) (*sql.Rows, error) { } orderTerms = append(orderTerms, `n.title ASC`) - query := "SELECT n.id, n.path, n.title, n.body, n.word_count, n.created, n.modified, n.checksum, " + snippetCol + query := "SELECT n.id, n.path, n.title, n.lead, n.body, n.raw_content, n.word_count, n.created, n.modified, n.checksum, " + snippetCol query += ` FROM notes n diff --git a/adapter/sqlite/note_dao_test.go b/adapter/sqlite/note_dao_test.go index 5304854..9ae8fc2 100644 --- a/adapter/sqlite/note_dao_test.go +++ b/adapter/sqlite/note_dao_test.go @@ -58,26 +58,30 @@ func TestNoteDAOIndexed(t *testing.T) { func TestNoteDAOAdd(t *testing.T) { testNoteDAO(t, func(tx Transaction, dao *NoteDAO) { err := dao.Add(note.Metadata{ - Path: "log/added.md", - Title: "Added note", - Body: "Note body", - WordCount: 2, - Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), - Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), - Checksum: "check", + Path: "log/added.md", + Title: "Added note", + Lead: "Note", + Body: "Note body", + RawContent: "# Added note\nNote body", + WordCount: 2, + Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), + Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), + Checksum: "check", }) assert.Nil(t, err) row, err := queryNoteRow(tx, `path = "log/added.md"`) assert.Nil(t, err) assert.Equal(t, row, noteRow{ - Path: "log/added.md", - Title: "Added note", - Body: "Note body", - WordCount: 2, - Checksum: "check", - Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), - Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), + Path: "log/added.md", + Title: "Added note", + Lead: "Note", + Body: "Note body", + RawContent: "# Added note\nNote body", + WordCount: 2, + Checksum: "check", + Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), + Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), }) }) } @@ -93,26 +97,30 @@ func TestNoteDAOAddExistingNote(t *testing.T) { func TestNoteDAOUpdate(t *testing.T) { testNoteDAO(t, func(tx Transaction, dao *NoteDAO) { err := dao.Update(note.Metadata{ - Path: "ref/test/a.md", - Title: "Updated note", - Body: "Updated body", - Checksum: "updated checksum", - WordCount: 42, - Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), - Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), + Path: "ref/test/a.md", + Title: "Updated note", + Lead: "Updated lead", + Body: "Updated body", + RawContent: "Updated raw content", + Checksum: "updated checksum", + WordCount: 42, + Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), + Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), }) assert.Nil(t, err) row, err := queryNoteRow(tx, `path = "ref/test/a.md"`) assert.Nil(t, err) assert.Equal(t, row, noteRow{ - Path: "ref/test/a.md", - Title: "Updated note", - Body: "Updated body", - Checksum: "updated checksum", - WordCount: 42, - Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), - Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), + Path: "ref/test/a.md", + Title: "Updated note", + Lead: "Updated lead", + Body: "Updated body", + RawContent: "Updated raw content", + Checksum: "updated checksum", + WordCount: 42, + Created: time.Date(2019, 11, 20, 20, 32, 56, 0, time.UTC), + Modified: time.Date(2020, 11, 22, 16, 49, 47, 0, time.UTC), }) }) } @@ -166,52 +174,60 @@ func TestNoteDAOFindMatch(t *testing.T) { }, []note.Match{ { - Snippet: "Index of the Zettelkasten", Metadata: note.Metadata{ - Path: "index.md", - Title: "Index", - Body: "Index of the Zettelkasten", - WordCount: 4, - Created: time.Date(2019, 12, 4, 11, 59, 11, 0, time.UTC), - Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.UTC), - Checksum: "iaefhv", + Path: "index.md", + Title: "Index", + Lead: "Index of the Zettelkasten", + Body: "Index of the Zettelkasten", + RawContent: "# Index\nIndex of the Zettelkasten", + WordCount: 4, + Created: time.Date(2019, 12, 4, 11, 59, 11, 0, time.UTC), + Modified: time.Date(2019, 12, 4, 12, 17, 21, 0, time.UTC), + Checksum: "iaefhv", }, + Snippet: "Index of the Zettelkasten", }, { - Snippet: "A daily note", Metadata: note.Metadata{ - Path: "log/2021-01-03.md", - Title: "January 3, 2021", - Body: "A daily note", - WordCount: 3, - Created: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC), - Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC), - Checksum: "qwfpgj", + Path: "log/2021-02-04.md", + Title: "February 4, 2021", + Lead: "A third daily note", + Body: "A third daily note", + RawContent: "# A third daily note", + WordCount: 4, + Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC), + Modified: time.Date(2020, 11, 10, 8, 20, 18, 0, time.UTC), + Checksum: "earkte", }, + Snippet: "A third daily note", }, { - Snippet: "A third daily note", Metadata: note.Metadata{ - Path: "log/2021-02-04.md", - Title: "February 4, 2021", - Body: "A third daily note", - WordCount: 4, - Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC), - Modified: time.Date(2020, 11, 10, 8, 20, 18, 0, time.UTC), - Checksum: "earkte", + Path: "log/2021-01-04.md", + Title: "January 4, 2021", + Lead: "A second daily note", + Body: "A second daily note", + RawContent: "# A second daily note", + WordCount: 4, + Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC), + Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC), + Checksum: "arstde", }, + Snippet: "A second daily note", }, { - Snippet: "A second daily note", Metadata: note.Metadata{ - Path: "log/2021-01-04.md", - Title: "January 4, 2021", - Body: "A second daily note", - WordCount: 4, - Created: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC), - Modified: time.Date(2020, 11, 29, 8, 20, 18, 0, time.UTC), - Checksum: "arstde", + Path: "log/2021-01-03.md", + Title: "January 3, 2021", + Lead: "A daily note", + Body: "A daily note\n\nWith lot of content", + RawContent: "# A daily note\nA daily note\n\nWith lot of content", + WordCount: 3, + Created: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC), + Modified: time.Date(2020, 11, 22, 16, 27, 45, 0, time.UTC), + Checksum: "qwfpgj", }, + Snippet: "A daily note\n\nWith lot of content", }, }, ) @@ -440,17 +456,17 @@ func testNoteDAO(t *testing.T, callback func(tx Transaction, dao *NoteDAO)) { } type noteRow struct { - Path, Title, Body, Checksum string - WordCount int - Created, Modified time.Time + Path, Title, Lead, Body, RawContent, Checksum string + WordCount int + Created, Modified time.Time } func queryNoteRow(tx Transaction, where string) (noteRow, error) { var row noteRow err := tx.QueryRow(fmt.Sprintf(` - SELECT path, title, body, word_count, checksum, created, modified + SELECT path, title, lead, body, raw_content, word_count, checksum, created, modified FROM notes WHERE %v - `, where)).Scan(&row.Path, &row.Title, &row.Body, &row.WordCount, &row.Checksum, &row.Created, &row.Modified) + `, where)).Scan(&row.Path, &row.Title, &row.Lead, &row.Body, &row.RawContent, &row.WordCount, &row.Checksum, &row.Created, &row.Modified) return row, err } diff --git a/core/note/index.go b/core/note/index.go index 6a16f1c..240bf8f 100644 --- a/core/note/index.go +++ b/core/note/index.go @@ -17,25 +17,15 @@ import ( // Metadata holds information about a particular note. type Metadata struct { - Path string - Title string - Body string - WordCount int - Created time.Time - Modified time.Time - Checksum string -} - -func (m Metadata) String() string { - return fmt.Sprintf(`note.Metadata{ - Path: "%v", - Title: "%v", - Body: "%v", - WordCount: %v, - Created: "%v", - Modified: "%v", - Checksum: "%v", -}`, m.Path, m.Title, m.Body, m.WordCount, m.Created.Format(time.RFC3339), m.Modified.Format(time.RFC3339), m.Checksum) + Path string + Title string + Lead string + Body string + RawContent string + WordCount int + Created time.Time + Modified time.Time + Checksum string } // Indexer persists the notes index. @@ -103,7 +93,9 @@ func metadata(path string, basePath string, parser Parser) (Metadata, error) { return metadata, err } metadata.Title = contentParts.Title.String() + metadata.Lead = contentParts.Lead.String() metadata.Body = contentParts.Body.String() + metadata.RawContent = contentStr metadata.WordCount = len(strings.Fields(contentStr)) metadata.Checksum = fmt.Sprintf("%x", sha256.Sum256(content)) diff --git a/core/note/list.go b/core/note/list.go index 0f9abce..5e4c923 100644 --- a/core/note/list.go +++ b/core/note/list.go @@ -113,22 +113,26 @@ func format(match Match, basePath string, templates templ.Loader) (*matchRenderC } return &matchRenderContext{ - Path: path, - Title: match.Title, - Body: match.Body, - WordCount: match.WordCount, - Snippet: snippet, - Created: match.Created, - Modified: match.Modified, + Path: path, + Title: match.Title, + Lead: match.Lead, + Body: match.Body, + RawContent: match.RawContent, + WordCount: match.WordCount, + Snippet: snippet, + Created: match.Created, + Modified: match.Modified, }, err } type matchRenderContext struct { - Path string - Title string - Body string - WordCount int - Snippet string - Created time.Time - Modified time.Time + Path string + Title string + Lead string + Body string + RawContent string + WordCount int + Snippet string + Created time.Time + Modified time.Time }