Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add #tag in Note automatically to frontmatter tags #747

Open
eist76 opened this issue Oct 10, 2024 · 0 comments
Open

add #tag in Note automatically to frontmatter tags #747

eist76 opened this issue Oct 10, 2024 · 0 comments

Comments

@eist76
Copy link

eist76 commented Oct 10, 2024

🚀 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:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant