lervag / wiki.vim

A wiki plugin for Vim
MIT License
654 stars 69 forks source link

How to link from journal to wiki pages #176

Closed alextes closed 3 years ago

alextes commented 3 years ago

I have a wiki much like this.

~/wiki
├── index.wiki
└── journal
    └── 2017-11-24.wiki

I like to use it as one big graph of connected notes. When I create a link in my journal I notice it doesn't link to the wiki in my root, but instead tries to find it in my journal.

How can I link from my journal to my root?

Thanks for the great plugin 👍 .

alextes commented 3 years ago

Found it!

[[/name-of-note]]

Plus, you can select and hit <CR> and it'll actually find and link it. Amazing!!

bybor commented 3 years ago

I solved the same issue with custom wiki resolver as suggested in the documentation, here is what I use.

My use case is similar to yours, my journal entries always have yyyy-MM-dd format, and it's under diary subfolder. I don't use <CR> mapping, but AFAIK it calls one of "open" or "follow" methods.

So the below code determines if the link belongs to diary or not. This allows to keep the same [[2021-07-22]] or [[227466515787 Current Status]] format.

As the very least you'll need to change diary to journal in code if you decide to try it.

HTH

let g:wiki_resolver = 'WikiResolver'

function! WikiResolver(fname, origin) abort " {{{1
  if empty(a:fname) | return a:origin | endif

  let l:f = fnamemodify(a:fname, ':t')

  if (match(l:f, '\d\{4}-\d\{2}-\d\{2}') == 0)
    let l:path = wiki#get_root() . '\diary\'. l:f
  else
    let l:path = wiki#get_root() . '\'. l:f
  endif

  " 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: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
lervag commented 3 years ago

... I like to use it as one big graph of connected notes. When I create a link in my journal I notice it doesn't link to the wiki in my root, but instead tries to find it in my journal.

How can I link from my journal to my root?

As you already figured out, the idea is to use / to "root" the link. The point is that, by default, all links are relative to the current file. To link to a file from the wiki root, one uses the [[/my link]] format with the /.

As @bybor explains, one can also customize the wiki resolver function, which allows quite a lot of flexibility in this behaviour. My goal is to have a more than decent default that should work for most people, while still allowing flexibility for those who want to change things.

@bybor: Just curious, what is "HTH"? A signature, i.e. your initials?

bybor commented 3 years ago

It's "Hope This Helps" 😉

lervag commented 3 years ago

Ah; thanks! :)