diff --git a/CHANGELOG.md b/CHANGELOG.md index 10a89dd11..21acb6867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Changed behavior of workspace detection. When you have multiple workspaces configured, obsidian.nvim will now automatically switch workspaces when you open a buffer in a different workspace. See [PR #366](https://github.com/epwalsh/obsidian.nvim/pull/366). - Various changes to the `Workspace` Lua API. See [PR #366](https://github.com/epwalsh/obsidian.nvim/pull/366). +- Changed the behavior of the default frontmatter function so it no longer automatically adds the title of the note as an alias. See the `note_frontmatter_func` example in the README if you want the old behavior. ### Removed diff --git a/README.md b/README.md index a7ff913f3..fb39676f8 100644 --- a/README.md +++ b/README.md @@ -332,8 +332,13 @@ This is a complete list of all of the options that can be passed to `require("ob -- Optional, alternatively you can customize the frontmatter data. note_frontmatter_func = function(note) - -- This is equivalent to the default frontmatter function. + -- Add the title of the note as an alias. + if note.title then + note:add_alias(note.title) + end + local out = { id = note.id, aliases = note.aliases, tags = note.tags } + -- `note.metadata` contains any manually added fields in the frontmatter. -- So here we just make sure those fields are kept in the frontmatter. if note.metadata ~= nil and not vim.tbl_isempty(note.metadata) then @@ -341,6 +346,7 @@ This is a complete list of all of the options that can be passed to `require("ob out[k] = v end end + return out end, diff --git a/lua/obsidian/note.lua b/lua/obsidian/note.lua index 4a2cb2f88..0c7c321f9 100644 --- a/lua/obsidian/note.lua +++ b/lua/obsidian/note.lua @@ -366,11 +366,6 @@ Note.from_lines = function(lines, path, root) end end - -- Use title as an alias. - if title ~= nil and not util.tbl_contains(aliases, title) then - table.insert(aliases, title) - end - -- The ID should match the filename with or without the extension. local relative_path = tostring(Path:new(tostring(path)):make_relative(cwd)) local relative_path_no_ext = relative_path diff --git a/test/obsidian/note_spec.lua b/test/obsidian/note_spec.lua index 46bad8f9a..b1e94ab07 100644 --- a/test/obsidian/note_spec.lua +++ b/test/obsidian/note_spec.lua @@ -31,8 +31,8 @@ describe("Note", function() it("should be able to be initialized from a note w/o frontmatter", function() local note = Note.from_file "test_fixtures/notes/note_without_frontmatter.md" assert.equals(note.id, "note_without_frontmatter") - assert.equals(#note.aliases, 1) - assert.equals(note.aliases[1], "Hey there") + assert.equals(note.title, "Hey there") + assert.equals(#note.aliases, 0) assert.equals(#note.tags, 0) assert.is_not(note:fname(), nil) assert.is_false(note.has_frontmatter) @@ -81,8 +81,7 @@ describe("Note", function() table.concat({ "---", "id: note_with_additional_metadata", - "aliases:", - " - Note with additional metadata", + "aliases: []", "tags: []", "foo: bar", "---", @@ -95,10 +94,10 @@ describe("Note", function() local note = Note.from_file "test_fixtures/notes/note_with_different_frontmatter_format.md" assert.equals(note.id, "note_with_different_frontmatter_format") assert.is_not(note.metadata, nil) - assert.equals(#note.aliases, 4) + assert.equals(#note.aliases, 3) assert.equals(note.aliases[1], "Amanda Green") assert.equals(note.aliases[2], "Detective Green") assert.equals(note.aliases[3], "Mandy") - assert.equals(note.aliases[4], "Detective") + assert.equals(note.title, "Detective") end) end) diff --git a/test_fixtures/notes/note_with_additional_metadata_saved.md b/test_fixtures/notes/note_with_additional_metadata_saved.md index 189b16963..848c850be 100644 --- a/test_fixtures/notes/note_with_additional_metadata_saved.md +++ b/test_fixtures/notes/note_with_additional_metadata_saved.md @@ -1,7 +1,6 @@ --- id: note_with_additional_metadata -aliases: - - Note with additional metadata +aliases: [] tags: [] foo: bar --- diff --git a/test_fixtures/notes/note_without_frontmatter_saved.md b/test_fixtures/notes/note_without_frontmatter_saved.md index 6f7c2a5c0..1b05d9c3b 100644 --- a/test_fixtures/notes/note_without_frontmatter_saved.md +++ b/test_fixtures/notes/note_without_frontmatter_saved.md @@ -1,7 +1,6 @@ --- id: note_without_frontmatter -aliases: - - Hey there +aliases: [] tags: [] ---