obsidian.nvim
obsidian.nvim copied to clipboard
Cant add links to autogenerated frontmatter
🐛 Describe the bug
What did I do:
- Using the auto generated frontmatter
- adding a field "links"
- adding a link to the field (e
- saving
What did I expect:
- everything gets saved
What happend:
- links and tags get deleted (tags becoming an empty array again
I parses the "[]" not correctly as they also seem to be used for list parsing in the frontmatter (e.g. tags).
Config
return {
"epwalsh/obsidian.nvim",
version = "*", -- recommended, use latest release instead of latest commit
lazy = true,
ft = "markdown",
-- Replace the above line with this if you only want to load obsidian.nvim for markdown files in your vault:
-- event = {
-- -- If you want to use the home shortcut '~' here you need to call 'vim.fn.expand'.
-- -- E.g. "BufReadPre " .. vim.fn.expand "~" .. "/my-vault/**.md"
-- "BufReadPre path/to/my-vault/**.md",
-- "BufNewFile path/to/my-vault/**.md",
-- },
dependencies = {
-- Required.
"nvim-lua/plenary.nvim",
-- see below for full list of optional dependencies 👇
},
opts = {
workspaces =
{
{
name = "personal",
path = function()
if vim.fn.hostname() == "RON-DESKTOP" then
return "I:/obsidian-brain/brain"
end
end
},
},
daily_notes = {
-- Optional, if you keep daily notes in a separate directory.
folder = "dailynotes",
-- Optional, if you want to change the date format for the ID of daily notes.
date_format = "%Y%m%d",
-- Optional, if you want to change the date format of the default alias of daily notes.
template = nil
},
templates = {
subdir = "templates",
date_format = "%Y-%m-%d",
time_format = "%H:%M",
-- A map for custom variables, the key should be the variable and the value a function
substitutions = {},
},
completion = {
-- Set to false to disable completion.
nvim_cmp = true,
-- Trigger completion at 2 chars.
min_chars = 1,
},
mappings = {
-- Overrides the 'gf' mapping to work on markdown/wiki links within your vault.
["<leader>ob"] = {
action = function()
return vim.cmd("ObsidianBacklinks")
end,
opts = { noremap = false, buffer = true, silent = true, desc = "Obsidian Backlinks" },
},
["<leader>ol"] = {
action = function()
return vim.cmd("ObsidianLinks")
end,
opts = { noremap = false, buffer = true, silent = true, desc = "Obsidian Links" },
},
["<leader>ot"] = {
action = function()
return vim.cmd("ObsidianTemplate")
end,
opts = { noremap = false, buffer = true, silent = true, desc = "Obsidian Template" },
},
["<leader>oe"] = {
action = function()
return vim.cmd("ObsidianExtractNote")
end,
opts = { noremap = false, buffer = true, silent = true, desc = "Obsidian Extract" },
},
["<leader>oq"] = {
action = function()
return vim.cmd("ObsidianQuickSwitch")
end,
opts = { noremap = false, buffer = true, silent = true, desc = "Obsidian QuickSwitch" },
},
["<leader>os"] = {
action = function()
return vim.cmd("ObsidianSearch")
end,
opts = { noremap = false, buffer = true, silent = true, desc = "Obsidian Search" },
},
["<leader>of"] = {
action = function()
return require("obsidian").util.gf_passthrough()
end,
opts = { noremap = false, expr = true, buffer = true, silent = true, desc = "Follow Note Link" },
},
-- Toggle check-boxes.
["<leader>oc"] = {
action = function()
return require("obsidian").util.toggle_checkbox()
end,
opts = { buffer = true, silent = true, desc = "Toggle Checkbox" },
},
},
note_id_func = function(title)
-- Create note IDs in a Zettelkasten format with a timestamp and a suffix.
-- In this case a note with the title 'My new note' will be given an ID that looks
-- like '1657296016-my-new-note', and therefore the file name '1657296016-my-new-note.md'
local suffix = ""
if title ~= nil then
-- If title is given, transform it into valid file name.
suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
else
-- If title is nil, just add 4 random uppercase letters to the suffix.
for _ = 1, 4 do
suffix = suffix .. string.char(math.random(65, 90))
end
end
return tostring(os.date("%Y%m%d")) .. "-" .. suffix
end,
-- Optional, boolean or a function that takes a filename and returns a boolean.
-- `true` indicates that you don't want obsidian.nvim to manage frontmatter.
disable_frontmatter = false,
-- Optional, alternatively you can customize the frontmatter data.
---@return table
note_frontmatter_func = function(note)
-- Add the title of the note as an alias.
-- if not note.metadata.links then
-- note:add_field("links", "")
-- end
if note.title then
note:add_alias(note.title)
note:add_field("title", 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,
},
}
Environment
NVIM v0.9.4
Build type: RelWithDebInfo
LuaJIT 2.1.1696883897
Obsidian.nvim v3.7.4 (68da7299505a74dcd0f45292c202197840615425)
Status:
? buffer directory: nil
? working directory: C:/
Workspaces:
? active workspace: Workspace(name='personal', path='I:/obsidian-brain/Brain', root='I:/obsidian-brain/Brain')
Dependencies:
? plenary.nvim: 4f71c0c4a196ceb656c824a70792f3df3ce6bb6d
? nvim-cmp: 04e0ca376d6abdbfc8b52180f8ea236cbfddf782
? telescope.nvim: 5f5fc3aa75e9fc824d4fbbb5de31f172b43f620d
Integrations:
? picker: TelescopePicker()
? completion: enabled (nvim-cmp)
Tools:
? rg: ripgrep 14.1.0 (rev e50df40a19)
Environment:
? operating system: Windows
Config:
? notes_subdir: nil
What does your frontmatter look like before you save the buffer?
I've the same issue, I've almost the provided function to insert the front matter :
note_frontmatter_func = function(note)
-- Add the title of the note as an alias.
-- if note.title then
-- note:add_alias(note.title)
-- end
-- Add a custom property as date
local currentDate = os.date("%Y/%m/%d %I:%M %p")
local out = {
id = note.id,
aliases = note.aliases,
tags = note.tags,
-- created = { currentDate }
}
-- `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,
this will create the following property table AKA frontmatter :
---
id: coredump-investigation
aliases: []
tags: []
---
now I can save the file and everything looks fine, now if I add a tag like :
---
id: coredump-investigation
aliases: []
tags: [#dd]
---
When I save the file, it will be nicly formatted like :
---
id: coredump-investigation
aliases: []
tags:
- #dd
---
Now if I save the file again, the tag will be removed, and I end up with
---
id: coredump-investigation
aliases: []
tags: []
---
I tried to investigate the cause, my understanding is when we havetags: [#dd], plugin detects the tag, but when plugin formats it like
tags:
- #dd
It cant parse the tag anymore and thinks there is no tag so the loop :
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
fails to keep #dd in the frontmatter
UPDATE :
I noticed that if I add tags like thisTag rather than #thisTag, it works as expected. Problem : without the hashtag sign there is no autocompletion on the tags.
But somehow there is still an issue with the way the frontmatter is handled,
this is my not_frontmatter_func :
note_frontmatter_func = function(note)
-- Add a custom property as date
local currentDate = os.date("%Y/%m/%d %I:%M %p")
local out = {
tags = note.tags,
created = currentDate,
}
-- `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,
For example, I don't want the plug-in to create the aliases property filed for me, BUT if I create that field on a note manually, whenever I save the note, the plug-in removes that field
Hey @sadtab, I just fixed the issue with the tags being erased in a7e2ec9.
For example, I don't want the plug-in to create the aliases property filed for me, BUT if I create that field on a note manually, whenever I save the note, the plug-in removes that field
Can you give me an example of an aliases field you've added manually that gets erased?
Here a newly created file:
---
id: 20240401-test
aliases:
- test
tags: []
links: []
title: test
---
# test
Here what I changes to the tags:
---
id: 20240401-test
aliases:
- test
tags:
- math
- concept
links: []
title: test
---
# test
After saving:
---
id: 20240401-test
aliases:
- test
tags:
- math
- concept
links: []
title: test
---
# test
Now adding some links:
---
id: 20240401-test
aliases:
- test
tags:
- math
- concept
links:
- [[Probability for Dice#findings#formulas-and-explanations]]
title: test
---
# test
And after saving it:
---
id: 20240401-test
aliases:
- test
tags:
- math
- concept
links: []
title: test
---
# test
And adding an external link:
---
id: 20240401-test
aliases:
- test
tags:
- math
- concept
links:
- [HelloLink](www.google.de)
title: test
---
# test
Results in:
---
id: 20240401-test
aliases:
- test
tags: []
links: []
title: test
---
# test
@dekaravanhoc,
3c3656b will fix - [HelloLink](www.google.de), but - [[Probability for Dice#findings#formulas-and-explanations]] is not valid YAML. Or, it is, but it's not parsed in the way you'd expect, since brackets indicate an inline list. If you quote that it will work as expected.
Ah yeah. Obsidian also saves them this way. But even quoted, it does not work. Same result. Tested it on an existing file reated and edited in Obsidian and a new one. Unfortunatly this keeps me from using you plugin, as I all my notes have this field for fast referencing and I cant edit/save existing notes that way. :(
But even quoted, it does not work.
Really? This works fine for me:
links:
- "[[Probability for Dice#findings#formulas-and-explanations]]"
You can also just disable frontmatter management completely by setting disable_frontmatter = true.
Hmm. Ok it works, when I remove the manual override of the frontmatter function. But than I need to manually add all the additional fields. :/ I could disable the frontmatter and use my template, but the title is always added at the top. The problem seems to be rooted in the frontmatter function override or the handling of the return probably?
But thanks for your help nevertheless. :)