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 00c09c77..42deb99c 100644 --- a/lua/obsidian/client.lua +++ b/lua/obsidian/client.lua @@ -1183,15 +1183,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 @@ -1313,11 +1313,11 @@ Client.find_backlinks_async = function(self, note, callback, opts) local note_path = Path.new(note.path) for raw_ref in iter { tostring(note.id), note_path.name, note_path.stem, 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 @@ -1708,11 +1708,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 @@ -2054,7 +2054,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 4f3ae84d..c45712a4 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 follow_img_func fun(img: 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 @@ -277,6 +279,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 1db3d044..ff3d1cf5 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 @@ -596,11 +596,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 @@ -669,11 +669,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 @@ -954,10 +954,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 { @@ -1027,6 +1027,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) @@ -1035,12 +1037,16 @@ 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) + 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