Skip to content

Commit 5a18cd9

Browse files
author
neo451
committed
test: for ab and shortest link
types: less tables
1 parent 4aceb71 commit 5a18cd9

File tree

9 files changed

+72
-56
lines changed

9 files changed

+72
-56
lines changed

lua/obsidian/builtin.lua

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ local M = {}
33
local util = require "obsidian.util"
44

55
---@class obsidian.link.LinkCreationOpts
6-
---@field path obsidian.Path
76
---@field label string
8-
---@field id string|integer|?
9-
---@field anchor obsidian.note.HeaderAnchor|?
10-
---@field block obsidian.note.Block|?
11-
---@field style "wiki" | "markdown"
7+
---@field path? obsidian.Path|string|?
8+
---@field id? string|integer|?
9+
---@field anchor? obsidian.note.HeaderAnchor|?
10+
---@field block? obsidian.note.Block|?
11+
---@field style? "wiki" | "markdown"
1212

1313
---Create a new unique Zettel ID.
1414
---
@@ -34,6 +34,7 @@ M.wiki_link_alias_only = function(opts)
3434
return string.format("[[%s%s]]", opts.label, header_or_block)
3535
end
3636

37+
---NOTE: more close to what should be default
3738
---@param opts obsidian.link.LinkCreationOpts
3839
---@return string
3940
M.wiki_link_path_only = function(opts)
@@ -89,19 +90,6 @@ M.wiki_link_id_prefix = function(opts)
8990
end
9091
end
9192

92-
---@param path obsidian.Path vault-relative-path
93-
---@param style "shortest" | "relative" | "absolute"
94-
---@return string foramted_path
95-
local function format_path(path, style)
96-
if style == "absolute" then
97-
return assert(path:vault_relative_path {})
98-
elseif style == "relative" then
99-
return assert(tostring(path:relative_to(Obsidian.buf_dir)), "failed to resolve link path against current note")
100-
else
101-
return vim.fs.basename(tostring(path))
102-
end
103-
end
104-
10593
---@param opts obsidian.link.LinkCreationOpts
10694
---@return string
10795
M.markdown_link = function(opts)
@@ -115,10 +103,7 @@ M.markdown_link = function(opts)
115103
header = "#" .. opts.block.id
116104
end
117105

118-
local path = format_path(opts.path, Obsidian.opts.link.format)
119-
path = util.urlencode(path, { keep_path_sep = true })
120-
121-
return string.format("[%s%s](%s%s)", opts.label, header, path, anchor)
106+
return string.format("[%s%s](%s%s)", opts.label, header, opts.path, anchor)
122107
end
123108

124109
---@param path string

lua/obsidian/completion/sources/base/new.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local completion = require "obsidian.completion.refs"
22
local util = require "obsidian.util"
3-
local LinkStyle = require("obsidian.config").LinkStyle
43
local Note = require "obsidian.note"
54
local Path = require "obsidian.path"
65

lua/obsidian/completion/sources/base/refs.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local completion = require "obsidian.completion.refs"
2-
local LinkStyle = require("obsidian.config").LinkStyle
32
local util = require "obsidian.util"
43
local api = require "obsidian.api"
54
local search = require "obsidian.search"

lua/obsidian/config/default.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ return {
2929
link = {
3030
style = "wiki",
3131
markdown = require("obsidian.builtin").markdown_link,
32-
wiki = require("obsidian.builtin").wiki_link_id_prefix,
33-
fromat = "shortest",
32+
wiki = require("obsidian.builtin").wiki_link_path_only,
33+
format = "shortest",
3434
},
3535

3636
workspaces = {},

lua/obsidian/config/init.lua

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ config.SortBy = {
2020
created = "created",
2121
}
2222

23-
---@enum obsidian.config.NewNotesLocation
24-
config.NewNotesLocation = {
25-
current_dir = "current_dir",
26-
notes_subdir = "notes_subdir",
27-
}
28-
29-
---@enum obsidian.config.LinkStyle
30-
config.LinkStyle = {
31-
wiki = "wiki",
32-
markdown = "markdown",
33-
}
34-
3523
---@enum obsidian.config.Picker
3624
config.Picker = {
3725
telescope = "telescope.nvim",

lua/obsidian/note.lua

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ local search = require "obsidian.search"
1616
local iter = vim.iter
1717
local compat = require "obsidian.compat"
1818
local api = require "obsidian.api"
19-
local config = require "obsidian.config"
2019
local Frontmatter = require "obsidian.frontmatter"
2120

2221
local SKIP_UPDATING_FRONTMATTER = { "README.md", "CONTRIBUTING.md", "CHANGELOG.md" }
@@ -117,7 +116,7 @@ end
117116
---@private
118117
Note._get_creation_opts = function(opts)
119118
--- @type obsidian.note.NoteCreationOpts
120-
local default = {
119+
local ret = {
121120
notes_subdir = Obsidian.opts.notes_subdir,
122121
note_id_func = Obsidian.opts.note_id_func,
123122
new_notes_location = Obsidian.opts.new_notes_location,
@@ -127,22 +126,23 @@ Note._get_creation_opts = function(opts)
127126
local success, template_path = pcall(resolve_template, opts.template, api.templates_dir())
128127

129128
if not success then
130-
return default
129+
return ret
131130
end
132131

133132
local stem = template_path.stem:lower()
134133

135134
-- Check if the configuration has a custom key for this template
136135
for key, cfg in pairs(Obsidian.opts.templates.customizations) do
137136
if key:lower() == stem then
138-
return {
137+
ret = {
139138
notes_subdir = cfg.notes_subdir,
140139
note_id_func = cfg.note_id_func,
141-
new_notes_location = config.NewNotesLocation.notes_subdir,
140+
new_notes_location = "notes_subdir",
142141
}
142+
break
143143
end
144144
end
145-
return default
145+
return ret
146146
end
147147

148148
--- Resolves the title, ID, and path for a new note.
@@ -224,7 +224,7 @@ Note._resolve_title_id_path = function(title, id, dir, strategy)
224224
else
225225
local bufpath = Path.buffer(0):resolve()
226226
if
227-
strategy.new_notes_location == config.NewNotesLocation.current_dir
227+
strategy.new_notes_location == "current_dir"
228228
-- note is actually in the workspace.
229229
and Obsidian.dir:is_parent_of(bufpath)
230230
-- note is not in dailies folder
@@ -1129,27 +1129,46 @@ Note.load_contents = function(self)
11291129
end
11301130
end
11311131

1132+
---@param path obsidian.Path vault-relative-path
1133+
---@param style obsidian.link.LinkFormat
1134+
---@return string foramted_path
1135+
local function format_path(path, style)
1136+
if style == "absolute" then
1137+
return assert(path:vault_relative_path {})
1138+
elseif style == "relative" then
1139+
return assert(tostring(path:relative_to(Obsidian.buf_dir)), "failed to resolve link path against current note")
1140+
else
1141+
return vim.fs.basename(tostring(path))
1142+
end
1143+
end
1144+
11321145
--- Create a formatted markdown / wiki link for a note.
11331146
---
1134-
---@param opts obsidian.link.LinkCreationOpts
1147+
---@param opts obsidian.link.LinkCreationOpts?
11351148
---@return string
11361149
Note.format_link = function(self, opts)
11371150
opts = opts or {}
11381151
local label = opts.label or self:display_name()
11391152
local note_id = opts.id or self.id
11401153
local link_style = opts.style or Obsidian.opts.link.style
11411154

1155+
local formatted_path = format_path(self.path, Obsidian.opts.link.format)
1156+
formatted_path = util.urlencode(formatted_path, { keep_path_sep = true })
1157+
if link_style == "wiki" then
1158+
formatted_path = formatted_path:gsub(".md", "")
1159+
end
1160+
11421161
local new_opts = {
1143-
path = self.path,
1162+
path = formatted_path,
11441163
label = label,
11451164
id = note_id,
11461165
anchor = opts.anchor,
11471166
block = opts.block,
11481167
}
11491168

1150-
if link_style == config.LinkStyle.markdown then
1169+
if link_style == "markdown" then
11511170
return Obsidian.opts.link.markdown(new_opts)
1152-
elseif link_style == config.LinkStyle.wiki or link_style == nil then
1171+
elseif link_style == "wiki" or link_style == nil then
11531172
return Obsidian.opts.link.wiki(new_opts)
11541173
else
11551174
error(string.format("Invalid link style '%s'", link_style))
@@ -1178,7 +1197,7 @@ end
11781197
---@class obsidian.note.NoteCreationOpts
11791198
---@field notes_subdir string
11801199
---@field note_id_func fun()
1181-
---@field new_notes_location string
1200+
---@field new_notes_location "current_dir" | "notes_subdir"
11821201

11831202
---@class obsidian.note.NoteOpts
11841203
---@field title string|? The note's title

lua/obsidian/types.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
---@field log_level? integer
3838
---@field notes_subdir? string
3939
---@field templates? obsidian.config.TemplateOpts
40-
---@field new_notes_location? obsidian.config.NewNotesLocation
40+
---@field new_notes_location? obsidian.note.NewNotesLocation
4141
---@field note_id_func? (fun(title: string|?, path: obsidian.Path|?): string)|?
4242
---@field note_path_func? fun(spec: { id: string, dir: obsidian.Path, title: string|? }): string|obsidian.Path
4343
----@field wiki_link_func? fun(opts: {path: string, label: string, id: string|?}): string
@@ -71,7 +71,7 @@
7171
---@field log_level integer
7272
---@field notes_subdir string|?
7373
---@field templates obsidian.config.TemplateOpts
74-
---@field new_notes_location obsidian.config.NewNotesLocation
74+
---@field new_notes_location obsidian.note.NewNotesLocation
7575
---@field note_id_func (fun(title: string|?, path: obsidian.Path|?): string)|?
7676
---@field note_path_func (fun(spec: { id: string, dir: obsidian.Path, title: string|? }): string|obsidian.Path)|?
7777
----@field wiki_link_func (fun(opts: {path: string, label: string, id: string|?}): string)
@@ -96,3 +96,6 @@
9696
---@field comment obsidian.config.CommentOpts
9797
---@field search obsidian.config.SearchOpts
9898
---@field link obsidian.config.LinkOpts
99+
100+
---@alias obsidian.link.LinkFormat "shortest" | "relative" | "absolute"
101+
---@alias obsidian.note.NewNotesLocation "current_dir" | "notes_subdir"

tests/test_builtin.lua

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,4 @@ T["markdown_link"]["should work with an anchor link"] = function()
7676
)
7777
end
7878

79-
T["markdown_link"]["should URL-encode paths"] = function()
80-
eq("[Foo](notes/123%20foo.md)", builtin.markdown_link { path = "notes/123 foo.md", id = "123-foo", label = "Foo" })
81-
end
82-
8379
return T

tests/test_note.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,33 @@ T["resolve_title_id_path"]["should ensure result of 'note_path_func' is always a
395395
eq(Path.new(Obsidian.dir) / "notes" / "foo-bar-123.md", path)
396396
end
397397

398+
T["format_link"] = new_set()
399+
400+
T["format_link"]["should respect link.style"] = function() end
401+
402+
T["format_link"]["wiki should respect link.format"] = function()
403+
-- default to link.style = "shortest"
404+
(Obsidian.dir / "notes"):mkdir { exist_ok = true }
405+
local note = from_str(foo, Obsidian.dir / "notes/foo.md")
406+
eq("[[foo]]", note:format_link())
407+
Obsidian.opts.link.format = "absolute"
408+
eq("[[notes/foo]]", note:format_link())
409+
-- Obsidian.opts.link.format = "relative"
410+
-- eq("[[foo]]", note:format_link())
411+
end
412+
413+
T["format_link"]["markdown should respect link.format"] = function()
414+
-- default to link.style = "shortest"
415+
Obsidian.opts.link.style = "markdown"
416+
(Obsidian.dir / "notes"):mkdir { exist_ok = true }
417+
local note = from_str(foo, Obsidian.dir / "notes/foo.md")
418+
eq("[foo](foo.md)", note:format_link())
419+
Obsidian.opts.link.format = "absolute"
420+
eq("[foo](notes/foo.md)", note:format_link())
421+
-- Obsidian.opts.link.format = "relative"
422+
-- eq("[[foo]]", note:format_link())
423+
end
424+
398425
-- T["reference_paths"] = new_set()
399426
--
400427
-- T["reference_paths"]["do four basic paths"] = function()

0 commit comments

Comments
 (0)