From 7fa778ced2ca21aa15ac6ed3440a31f9e36aad30 Mon Sep 17 00:00:00 2001 From: RokPre <117595693+RokPre@users.noreply.github.com> Date: Tue, 13 May 2025 15:13:10 +0200 Subject: [PATCH 1/4] Update cmp_obsidian.lua Made it so that if a file has an id, it can still be found in the cmp dropdown by its name. --- lua/cmp_obsidian.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lua/cmp_obsidian.lua b/lua/cmp_obsidian.lua index 9d940585..ef932fdc 100644 --- a/lua/cmp_obsidian.lua +++ b/lua/cmp_obsidian.lua @@ -60,6 +60,7 @@ source.complete = function(_, request, callback) ---@param results obsidian.Note[] local function search_callback(results) + vim.print(vim.inspect(results)) -- Completion items. local items = {} @@ -150,10 +151,17 @@ source.complete = function(_, request, callback) ---@type string, string, string, table|? local final_label, sort_text, new_text, documentation if option.label then + -- new_text = client:format_link( + -- note, + -- { label = option.label, link_style = link_style, anchor = option.anchor, block = option.block } + -- ) + vim.print("----------") + vim.print(vim.inspect(option)) new_text = client:format_link( note, { label = option.label, link_style = link_style, anchor = option.anchor, block = option.block } - ) + )-- new_text is used by cmp to replace the text, should be link in the form of [[filename]] + -- vim.print("new_text: " .. new_text) final_label = assert(option.alt_label or option.label) if option.anchor then @@ -211,6 +219,7 @@ source.complete = function(_, request, callback) error "should not happen" end + -- new_text_to_option is a list of options that cmp will show to the user. The text that gets inserted is new_text. if new_text_to_option[new_text] then new_text_to_option[new_text].sort_text = new_text_to_option[new_text].sort_text .. " " .. sort_text else @@ -231,6 +240,9 @@ source.complete = function(_, request, callback) if note.title ~= nil then table.insert(aliases, note.title) end + if note.path.filename then + table.insert(aliases, note.path.filename:match("([^/]+)%.%w+$")) + end end for alias in iter(aliases) do @@ -270,7 +282,7 @@ source.complete = function(_, request, callback) label = label, kind = 18, -- "Reference" textEdit = { - newText = option.new_text, + newText = option.new_text, -- This line tells cmp what to replace the current text with. range = { start = { line = request.context.cursor.row - 1, From 0c4b4a422f7c6e2a1bf5893f4762663564d73bf9 Mon Sep 17 00:00:00 2001 From: RokPre <117595693+RokPre@users.noreply.github.com> Date: Tue, 13 May 2025 15:14:38 +0200 Subject: [PATCH 2/4] Update cmp_obsidian.lua --- lua/cmp_obsidian.lua | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/lua/cmp_obsidian.lua b/lua/cmp_obsidian.lua index ef932fdc..bf061394 100644 --- a/lua/cmp_obsidian.lua +++ b/lua/cmp_obsidian.lua @@ -60,7 +60,6 @@ source.complete = function(_, request, callback) ---@param results obsidian.Note[] local function search_callback(results) - vim.print(vim.inspect(results)) -- Completion items. local items = {} @@ -151,17 +150,10 @@ source.complete = function(_, request, callback) ---@type string, string, string, table|? local final_label, sort_text, new_text, documentation if option.label then - -- new_text = client:format_link( - -- note, - -- { label = option.label, link_style = link_style, anchor = option.anchor, block = option.block } - -- ) - vim.print("----------") - vim.print(vim.inspect(option)) new_text = client:format_link( note, { label = option.label, link_style = link_style, anchor = option.anchor, block = option.block } - )-- new_text is used by cmp to replace the text, should be link in the form of [[filename]] - -- vim.print("new_text: " .. new_text) + ) final_label = assert(option.alt_label or option.label) if option.anchor then @@ -219,7 +211,6 @@ source.complete = function(_, request, callback) error "should not happen" end - -- new_text_to_option is a list of options that cmp will show to the user. The text that gets inserted is new_text. if new_text_to_option[new_text] then new_text_to_option[new_text].sort_text = new_text_to_option[new_text].sort_text .. " " .. sort_text else @@ -282,7 +273,7 @@ source.complete = function(_, request, callback) label = label, kind = 18, -- "Reference" textEdit = { - newText = option.new_text, -- This line tells cmp what to replace the current text with. + newText = option.new_text, range = { start = { line = request.context.cursor.row - 1, From ab4a787057c3947220930bc9518ecdd69d8d96c4 Mon Sep 17 00:00:00 2001 From: Rok Prezelj Date: Tue, 13 May 2025 22:28:28 +0200 Subject: [PATCH 3/4] Added support for changing what type of links you want. Either use id or filename --- lua/cmp_obsidian.lua | 18 ++++++++++----- lua/obsidian/client.lua | 44 ++++++++++++++++++++--------------- lua/obsidian/config.lua | 18 +++++++++++---- lua/obsidian/util.lua | 51 +++++++++++++++++++++++------------------ 4 files changed, 80 insertions(+), 51 deletions(-) diff --git a/lua/cmp_obsidian.lua b/lua/cmp_obsidian.lua index 9d940585..f4909cf3 100644 --- a/lua/cmp_obsidian.lua +++ b/lua/cmp_obsidian.lua @@ -152,7 +152,13 @@ source.complete = function(_, request, callback) if option.label then new_text = client:format_link( note, - { label = option.label, link_style = link_style, anchor = option.anchor, block = option.block } + { + label = option.label, + link_style = link_style, + anchor = option.anchor, + block = option.block, + linkContent = client.opts.preferred_link_content + } ) final_label = assert(option.alt_label or option.label) @@ -215,7 +221,7 @@ source.complete = function(_, request, callback) new_text_to_option[new_text].sort_text = new_text_to_option[new_text].sort_text .. " " .. sort_text else new_text_to_option[new_text] = - { label = final_label, new_text = new_text, sort_text = sort_text, documentation = documentation } + { label = final_label, new_text = new_text, sort_text = sort_text, documentation = documentation } end end end @@ -231,16 +237,16 @@ source.complete = function(_, request, callback) if note.title ~= nil then table.insert(aliases, note.title) end + if note.path.filename then + table.insert(aliases, note.path.filename:match("([^/]+)%.%w+$")) + end end for alias in iter(aliases) do update_completion_options(alias) local alias_case_matched = util.match_case(search, alias) - if - alias_case_matched ~= nil - and alias_case_matched ~= alias - and not util.tbl_contains(note.aliases, alias_case_matched) + if alias_case_matched ~= nil and alias_case_matched ~= alias and not util.tbl_contains(note.aliases, alias_case_matched) then update_completion_options(alias_case_matched) end diff --git a/lua/obsidian/client.lua b/lua/obsidian/client.lua index ff6e500c..fa507526 100644 --- a/lua/obsidian/client.lua +++ b/lua/obsidian/client.lua @@ -1174,15 +1174,15 @@ Client.find_tags_async = function(self, term, callback, opts) search_terms[#search_terms + 1] = "#" .. search.Patterns.TagCharsOptional .. t .. search.Patterns.TagCharsOptional -- frontmatter tag in multiline list search_terms[#search_terms + 1] = "\\s*- " - .. search.Patterns.TagCharsOptional - .. t - .. search.Patterns.TagCharsOptional - .. "$" + .. search.Patterns.TagCharsOptional + .. t + .. search.Patterns.TagCharsOptional + .. "$" -- frontmatter tag in inline list search_terms[#search_terms + 1] = "tags: .*" - .. search.Patterns.TagCharsOptional - .. t - .. search.Patterns.TagCharsOptional + .. search.Patterns.TagCharsOptional + .. t + .. search.Patterns.TagCharsOptional else -- tag in the wild search_terms[#search_terms + 1] = "#" .. search.Patterns.TagCharsRequired @@ -1303,11 +1303,11 @@ Client.find_backlinks_async = function(self, note, callback, opts) local search_terms = {} for raw_ref in iter { tostring(note.id), note:fname(), self:vault_relative_path(note.path) } do for ref in - iter(util.tbl_unique { - raw_ref, - util.urlencode(tostring(raw_ref)), - util.urlencode(tostring(raw_ref), { keep_path_sep = true }), - }) + iter(util.tbl_unique { + raw_ref, + util.urlencode(tostring(raw_ref)), + util.urlencode(tostring(raw_ref), { keep_path_sep = true }), + }) do if ref ~= nil then if anchor == nil and block == nil then @@ -1698,11 +1698,11 @@ Client.parse_title_id_path = function(self, title, id, dir) else local bufpath = Path.buffer(0):resolve() if - self.opts.new_notes_location == config.NewNotesLocation.current_dir - -- note is actually in the workspace. - and self.dir:is_parent_of(bufpath) - -- note is not in dailies folder - and (self.opts.daily_notes.folder == nil or not (self.dir / self.opts.daily_notes.folder):is_parent_of(bufpath)) + self.opts.new_notes_location == config.NewNotesLocation.current_dir + -- note is actually in the workspace. + and self.dir:is_parent_of(bufpath) + -- note is not in dailies folder + and (self.opts.daily_notes.folder == nil or not (self.dir / self.opts.daily_notes.folder):is_parent_of(bufpath)) then base_dir = self.buf_dir or assert(bufpath:parent()) else @@ -2044,7 +2044,15 @@ Client.format_link = function(self, note, opts) link_style = self.opts.preferred_link_style end - local new_opts = { path = rel_path, label = label, id = note_id, anchor = opts.anchor, block = opts.block } + local new_opts = { + path = rel_path, + label = label, + id = note_id, + anchor = opts.anchor, + block = opts.block, + linkContent = + opts.linkContent + } if link_style == config.LinkStyle.markdown then return self.opts.markdown_link_func(new_opts) diff --git a/lua/obsidian/config.lua b/lua/obsidian/config.lua index 81903842..8f4d7498 100644 --- a/lua/obsidian/config.lua +++ b/lua/obsidian/config.lua @@ -15,6 +15,7 @@ local config = {} ---@field wiki_link_func (fun(opts: {path: string, label: string, id: string|?}): string) ---@field markdown_link_func (fun(opts: {path: string, label: string, id: string|?}): string) ---@field preferred_link_style obsidian.config.LinkStyle +---@field preferred_link_content obsidian.config.LinkContent ---@field follow_url_func fun(url: string)|? ---@field image_name_func (fun(): string)|? ---@field note_frontmatter_func (fun(note: obsidian.Note): table)|? @@ -49,6 +50,7 @@ config.ClientOpts.default = function() wiki_link_func = util.wiki_link_id_prefix, markdown_link_func = util.markdown_link, preferred_link_style = config.LinkStyle.wiki, + preferred_link_content = config.LinkContent.id, follow_url_func = nil, note_frontmatter_func = nil, disable_frontmatter = false, @@ -134,8 +136,8 @@ config.ClientOpts.normalize = function(opts, defaults) if warn then log.warn_once( "The config options 'completion.prepend_note_id', 'completion.prepend_note_path', and 'completion.use_path_only' " - .. "are deprecated. Please use 'wiki_link_func' instead.\n" - .. "See https://github.com/epwalsh/obsidian.nvim/pull/406" + .. "are deprecated. Please use 'wiki_link_func' instead.\n" + .. "See https://github.com/epwalsh/obsidian.nvim/pull/406" ) end end @@ -157,7 +159,7 @@ config.ClientOpts.normalize = function(opts, defaults) opts.completion.preferred_link_style = nil log.warn_once( "The config option 'completion.preferred_link_style' is deprecated, please use the top-level " - .. "'preferred_link_style' instead." + .. "'preferred_link_style' instead." ) end @@ -166,7 +168,7 @@ config.ClientOpts.normalize = function(opts, defaults) opts.completion.new_notes_location = nil log.warn_once( "The config option 'completion.new_notes_location' is deprecated, please use the top-level " - .. "'new_notes_location' instead." + .. "'new_notes_location' instead." ) end @@ -174,7 +176,7 @@ config.ClientOpts.normalize = function(opts, defaults) opts.detect_cwd = nil log.warn_once( "The 'detect_cwd' field is deprecated and no longer has any affect.\n" - .. "See https://github.com/epwalsh/obsidian.nvim/pull/366 for more details." + .. "See https://github.com/epwalsh/obsidian.nvim/pull/366 for more details." ) end @@ -269,6 +271,12 @@ config.LinkStyle = { markdown = "markdown", } +---@enum obsidian.config.LinkContent +config.LinkContent = { + id = "id", + filename = "filename", +} + ---@class obsidian.config.CompletionOpts --- ---@field nvim_cmp boolean diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index d0dc5b15..a9286f74 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -183,9 +183,9 @@ util.is_url = function(s) local search = require "obsidian.search" if - string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.NakedUrl] .. "$") - or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.FileUrl] .. "$") - or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.MailtoUrl] .. "$") + string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.NakedUrl] .. "$") + or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.FileUrl] .. "$") + or string.match(util.strip_whitespace(s), "^" .. search.Patterns[search.RefTypes.MailtoUrl] .. "$") then return true else @@ -587,11 +587,11 @@ util.cursor_on_markdown_link = function(line, col, include_naked_urls, include_f cur_col = col or cur_col + 1 -- nvim_win_get_cursor returns 0-indexed column for match in - iter(search.find_refs(current_line, { - include_naked_urls = include_naked_urls, - include_file_urls = include_file_urls, - include_block_ids = include_block_ids, - })) + iter(search.find_refs(current_line, { + include_naked_urls = include_naked_urls, + include_file_urls = include_file_urls, + include_block_ids = include_block_ids, + })) do local open, close, m_type = unpack(match) if open <= cur_col and cur_col <= close then @@ -660,11 +660,11 @@ util.parse_link = function(link, opts) local link_type = opts.link_type if link_type == nil then for match in - iter(search.find_refs(link, { - include_naked_urls = opts.include_naked_urls, - include_file_urls = opts.include_file_urls, - include_block_ids = opts.include_block_ids, - })) + iter(search.find_refs(link, { + include_naked_urls = opts.include_naked_urls, + include_file_urls = opts.include_file_urls, + include_block_ids = opts.include_block_ids, + })) do local _, _, m_type = unpack(match) if m_type then @@ -945,10 +945,10 @@ util.get_visual_selection = function(opts) selection = string.sub(lines[1], cscol) .. "\n" .. string.sub(lines[n], 1, cecol) else selection = string.sub(lines[1], cscol) - .. "\n" - .. table.concat(lines, "\n", 2, n - 1) - .. "\n" - .. string.sub(lines[n], 1, cecol) + .. "\n" + .. table.concat(lines, "\n", 2, n - 1) + .. "\n" + .. string.sub(lines[n], 1, cecol) end return { @@ -1018,6 +1018,8 @@ end util.wiki_link_id_prefix = function(opts) local anchor = "" local header = "" + local base = "" + local filepath = opts.path:match("([^/]+)%.%w+$") if opts.anchor then anchor = opts.anchor.anchor header = util.format_anchor_label(opts.anchor) @@ -1026,12 +1028,17 @@ util.wiki_link_id_prefix = function(opts) header = "#" .. opts.block.id end - if opts.id == nil then - return string.format("[[%s%s]]", opts.label, anchor) - elseif opts.label ~= opts.id then - return string.format("[[%s%s|%s%s]]", opts.id, anchor, opts.label, header) + vim.print(opts.linkContent) + if opts.linkContent == "filename" or opts.id == nil then + base = filepath + else + base = tostring(opts.id) + end + + if base ~= opts.label then + return string.format("[[%s%s|%s%s]]", base, anchor, opts.label, header) else - return string.format("[[%s%s]]", opts.id, anchor) + return string.format("[[%s%s]]", base, anchor) end end From f01e32b4546fcecdea8d41a8c62ddd181ac3dfce Mon Sep 17 00:00:00 2001 From: RokPre <117595693+RokPre@users.noreply.github.com> Date: Tue, 13 May 2025 22:47:47 +0200 Subject: [PATCH 4/4] Update util.lua Removed vim.print --- lua/obsidian/util.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/obsidian/util.lua b/lua/obsidian/util.lua index 67cefb38..ff3d1cf5 100644 --- a/lua/obsidian/util.lua +++ b/lua/obsidian/util.lua @@ -1037,7 +1037,6 @@ util.wiki_link_id_prefix = function(opts) header = "#" .. opts.block.id end - vim.print(opts.linkContent) if opts.linkContent == "filename" or opts.id == nil then base = filepath else