Skip to content

Commit

Permalink
Don't automatically insert title as an alias
Browse files Browse the repository at this point in the history
Closes #371. If you still want the old behavior, see the
`note_frontmatter_func` example in the config in the README.
  • Loading branch information
epwalsh committed Feb 9, 2024
1 parent 6b9f27f commit f6e3e65
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,21 @@ 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
for k, v in pairs(note.metadata) do
out[k] = v
end
end

return out
end,

Expand Down
5 changes: 0 additions & 5 deletions lua/obsidian/note.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions test/obsidian/note_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -81,8 +81,7 @@ describe("Note", function()
table.concat({
"---",
"id: note_with_additional_metadata",
"aliases:",
" - Note with additional metadata",
"aliases: []",
"tags: []",
"foo: bar",
"---",
Expand All @@ -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)
3 changes: 1 addition & 2 deletions test_fixtures/notes/note_with_additional_metadata_saved.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
id: note_with_additional_metadata
aliases:
- Note with additional metadata
aliases: []
tags: []
foo: bar
---
Expand Down
3 changes: 1 addition & 2 deletions test_fixtures/notes/note_without_frontmatter_saved.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
id: note_without_frontmatter
aliases:
- Hey there
aliases: []
tags: []
---

Expand Down

0 comments on commit f6e3e65

Please sign in to comment.