You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
similar to the functionality to add the title to the frontmatter alias (see below), i would like to automatically add all '#tag' tags in the note itself to the frontmatter tags automatically:
note_frontmatter_func = function(note)
-- Add the title of the note as an alias.
if note.title then
note:add_alias(note.title)
end
I was able to make this functionality work by adding the following to my obsidian.lua file for my lazy config - this is probably not very beautiful but shows the intention i guess:
-- Function to extract tags from the text
local function extract_tags(text)
local tags = {}
for tag in text:gmatch("#([%w-]+)") do
table.insert(tags, tag)
end
return tags
end
-- Function to add tags to frontmatter
local function add_tags_to_frontmatter(note)
local bufnr = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local tags = extract_tags(table.concat(lines, "\n"))
-- Add the title of the note as an alias.
if note.title then
note:add_alias(note.title)
end
-- Merge existing tags with new tags, ensuring existing tags are prioritized
local all_tags = note.tags or {}
local unique_tags = {}
local final_tags = {}
-- Add existing frontmatter tags first
for _, tag in ipairs(all_tags) do
table.insert(final_tags, tag)
unique_tags[tag] = true
end
-- Add new tags from the text, ensuring no duplicates
for _, tag in ipairs(tags) do
if not unique_tags[tag] then
table.insert(final_tags, tag)
unique_tags[tag] = true
end
end
local out = { id = note.id, aliases = note.aliases, tags = final_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
and then changed the following line in the opts block: note_frontmatter_func = function(note)
to note_frontmatter_func = add_tags_to_frontmatter,
i was wondering if this functionality could be added to the plugin and then just be enabled through a configuration setting.
Alternatives
No response
Additional context
No response
The text was updated successfully, but these errors were encountered:
🚀 The feature, motivation and pitch
similar to the functionality to add the title to the frontmatter alias (see below), i would like to automatically add all '#tag' tags in the note itself to the frontmatter tags automatically:
I was able to make this functionality work by adding the following to my obsidian.lua file for my lazy config - this is probably not very beautiful but shows the intention i guess:
and then changed the following line in the opts block:
note_frontmatter_func = function(note)
to
note_frontmatter_func = add_tags_to_frontmatter,
i was wondering if this functionality could be added to the plugin and then just be enabled through a configuration setting.
Alternatives
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: