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 option to generate Obsidian-compatible wiki links #728

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added `opts.follow_img_func` option for customizing how to handle image paths.
- Added better handling for undefined template fields, which will now be prompted for.
- You can now set `wiki_link_func` to `use_name_only` to generate wiki links in the format `[[Foo]]`, ensuring compatibility with the Obsidian desktop app.

### Changed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ This is a complete list of all of the options that can be passed to `require("ob
-- * "prepend_note_id", e.g. '[[foo-bar|Foo Bar]]'
-- * "prepend_note_path", e.g. '[[foo-bar.md|Foo Bar]]'
-- * "use_path_only", e.g. '[[foo-bar.md]]'
-- * "use_name_only", e.g, '[[foo-bar]]'
-- Or you can set it to a function that takes a table of options and returns a string, like this:
wiki_link_func = function(opts)
return require("obsidian.util").wiki_link_id_prefix(opts)
Expand Down
2 changes: 2 additions & 0 deletions lua/obsidian/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ config.ClientOpts.normalize = function(opts, defaults)
opts.wiki_link_func = util.wiki_link_path_only
elseif opts.wiki_link_func == "use_alias_only" then
opts.wiki_link_func = util.wiki_link_alias_only
elseif opts.wiki_link_func == "use_name_only" then
opts.wiki_link_func = util.wiki_link_name_only
elseif type(opts.wiki_link_func) == "string" then
error(string.format("invalid option '%s' for 'wiki_link_func'", opts.wiki_link_func))
end
Expand Down
17 changes: 17 additions & 0 deletions lua/obsidian/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,23 @@ util.wiki_link_path_prefix = function(opts)
end
end

---@param opts { path: string, label: string, id: string|integer|?, anchor: obsidian.note.HeaderAnchor|?, block: obsidian.note.Block|? }
---@return string
util.wiki_link_name_only = function(opts)
local header_or_block = ""
if opts.anchor then
header_or_block = string.format("#%s", opts.anchor.header)
elseif opts.block then
header_or_block = string.format("#%s", opts.block.id)
end
local name = opts.path:gsub("%.md", "")
if opts.label ~= name then
return string.format("[[%s%s|%s]]", name, header_or_block, opts.label)
else
return string.format("[[%s%s]]", name, header_or_block)
end
end

---@param opts { path: string, label: string, id: string|integer|?, anchor: obsidian.note.HeaderAnchor|?, block: obsidian.note.Block|? }
---@return string
util.wiki_link_id_prefix = function(opts)
Expand Down
18 changes: 18 additions & 0 deletions test/obsidian/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,24 @@ describe("util.wiki_link_path_only()", function()
end)
end)

describe("util.wiki_link_name_only()", function()
it("should work without an anchor link", function()
assert.equals("[[123-foo|Foo]]", util.wiki_link_name_only { path = "123-foo.md", id = "123-foo", label = "Foo" })
end)

it("should work with an anchor link", function()
assert.equals(
"[[123-foo#heading]]",
util.wiki_link_name_only {
path = "123-foo.md",
id = "123-foo",
label = "Foo",
anchor = { anchor = "#heading", header = "Heading", level = 1, line = 1 },
}
)
end)
end)

describe("util.markdown_link()", function()
it("should work without an anchor link", function()
assert.equals("[Foo](123-foo.md)", util.markdown_link { path = "123-foo.md", id = "123-foo", label = "Foo" })
Expand Down
Loading