@@ -16,7 +16,6 @@ local search = require "obsidian.search"
1616local iter = vim .iter
1717local compat = require " obsidian.compat"
1818local api = require " obsidian.api"
19- local config = require " obsidian.config"
2019local Frontmatter = require " obsidian.frontmatter"
2120
2221local SKIP_UPDATING_FRONTMATTER = { " README.md" , " CONTRIBUTING.md" , " CHANGELOG.md" }
117116--- @private
118117Note ._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
146146end
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
11301130end
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
11361149Note .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
0 commit comments