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

wiki:index is linking me to the index of the directory (journal) and not index of the wiki #160

Closed
anshulxyz opened this issue Jun 10, 2021 · 7 comments

Comments

@anshulxyz
Copy link

anshulxyz commented Jun 10, 2021

Description

Hi Karl 👋

wiki:index is linking me to the index of the directory (journal) and not index of the wiki. Is this the expected behavior.

  1. expected behaviour: I expect that clicking wiki:index from my ~/wiki/journal/2021-06-10.md file would take me to ~/wiki/index.md
  2. observed behaviour: it linked me to the ~/wiki/jounral/index.md instead
  3. The steps required to reproduce the issue
    3.1 Open Vim, <leader>ww to go to wiki index
    3.2 Open today's journal, <leader>w<leader>w
    3.3 Write in the file wiki:index
    3.4 In normal mode, <cr> on wiki:index

my wiki directory currently looks like this

wiki
├── journal
│   └── 2021-06-10.md
└── index.md

my vimrc settings

let g:wiki_root = '~/wiki'
let g:wiki_filetypes = ['md']
let g:wiki_global_load = 0
let g:wiki_link_extension = '.md'
Screen.Recording.2021-06-10.at.1.44.59.PM.mov

PS

I was able to link to the root index.md file using [[/index.md|index]] link.

@lervag
Copy link
Owner

lervag commented Jun 10, 2021

Hi Karl 👋

Hi, Anshul 👋🏻

wiki:index is linking me to the index of the directory (journal) and not index of the wiki. Is this the expected behavior.

Yes, actually it is. I'll try to explain why.

The anatomy of an url is, basically, scheme:target. For both wiki links ([[url|text]]) and Markdown links ([text](url)), the default scheme is wiki. Thus, the link [[wiki:index]] and [[index]] are equivalent.

I believe the confusion is caused by the fact that the wiki links are by default relative to the current file. So, [[index]] within ~/wiki/journal/2021-06-10.md would link to ~/wiki/journal/index.md. Thus, the same behaviour is expected for [[wiki:index]].

Obviously, it would be possible to instead make the link targets relative to the wiki root. But, in that case, directory structures becomes much more "entangled". That is, say you have a subdirectory in your wiki, sort of a subwiki. By using relative paths, you could essentially move that subdirectory out of your main wiki, and all interwiki links within the subwiki will still just work. But it would not work if the links are relative to the wiki root, since we changed the wiki root when moving the subwiki out.

This is also why I've implemented the syntax [[/target]], where / is used to root the target to the wiki root. So, inside your journal, you should use [[/index]] or [[wiki:/index]] to link to the root index.

I hope this makes sense. If you feel strongly about this, then I'm not opposed to discussing what is the best solution. Also, you should note that it is possible to customize the behaviour. In fact, you can change the entire wiki link resolver, see :help g:wiki_resolver. The default resolver is here:

function! wiki#url#wiki#resolver(fname, origin) abort " {{{1
if empty(a:fname) | return a:origin | endif
" Extract the full path
let l:path = a:fname[0] ==# '/'
\ ? wiki#get_root() . a:fname
\ : (empty(a:origin)
\ ? wiki#get_root()
\ : fnamemodify(a:origin, ':p:h')) . '/' . a:fname
let l:path = wiki#paths#s(l:path)
" Determine the proper extension (if necessary)
let l:extensions = wiki#u#uniq_unsorted(
\ (exists('b:wiki.extension') ? [b:wiki.extension] : [])
\ + g:wiki_filetypes)
if index(l:extensions, fnamemodify(a:fname, ':e')) < 0
let l:path = l:path
let l:path .= '.' . l:extensions[0]
if !filereadable(l:path) && len(l:extensions) > 1
for l:ext in l:extensions[1:]
let l:newpath = l:path . '.' . l:ext
if filereadable(l:newpath)
let l:path = l:newpath
break
endif
endfor
endif
endif
return l:path
endfunction

@anshulxyz
Copy link
Author

Thank you Karl for explanation.

I hope this makes sense

It does make sense.

I am going to stick to the default behaviour. Because I do plan to have subwikis for different fields.

@lervag
Copy link
Owner

lervag commented Jun 11, 2021

Great! Feel free to open new issues/questions/requests :)

@anshul-march
Copy link

Hi Karl @lervag

I was looking for a feature where when I open a new journal entry, it should have the date as the first line of the file.
So I wrote this little function in Lua for NewVim v0.5

function file_exists(name)
   local f=io.open(name,"r")
   if f~=nil then io.close(f) return true else return false end
end

local api = vim.api
local M = {}
function M.currentEntry()

  -- set the filename based on the current date
  local filepath = vim.g['wiki_root']..'journal/'..os.date('%Y-%m-%d')..'.md'
  
  -- if the file doesn't exist
  -- then created file and write date to the top of the file
  if not file_exists(filepath) then
    file = io.open(filepath, 'a')
    io.output(file)
    io.write(os.date("# %a, %d %B '%y"))
    io.close(file)
  end
  api.nvim_command('edit '..filepath)
end
return M

What do you think? Have other people also mention interest in such a thing for your wiki plugin? If yes, I would love to contribute.

@lervag
Copy link
Owner

lervag commented Jun 28, 2021

Hi! (Are you the same Anshul as the OP?)

Are you aware of #83? I'm currently working on exactly this feature. There's no code yet, but I've made a specification that I think I'm happy with. Feel free to comment on it.

Please note, I (still) want to keep wiki.vim "Vim agnostic", i.e. compatible with both neovim and Vim. So I don't want lua code within the repo. Yet - I might change my mind in the future. But I would not mind making the user customization flexible enough for you to use Lua for the template feature.

@anshul-march
Copy link

Are you the same Anshul as the OP?

Yes, using my work account right now.

Are you aware of #83?

I was not, that looks exactly like what I wanted, this is great

I (still) want to keep wiki.vim "Vim agnostic"

Yeah, understandable. I have been scared of VimL so when I saw that I could use Lua in nvim v0.5, I just wrote the script. I'll go through the #83 and see if I can add value.

@lervag
Copy link
Owner

lervag commented Jun 28, 2021

Yeah, understandable. I have been scared of VimL so when I saw that I could use Lua in nvim v0.5, I just wrote the script. I'll go through the #83 and see if I can add value.

Great! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants