Files
vim-denote/autoload/denote/meta.vim

46 lines
2.1 KiB
VimL

" Return the filename of the note with id `noteId`. If the file is note found,
" then v:false is returned.
function denote#meta#fileFromNoteId(noteId)
" According to the file-naming scheme, the note id is prefixed with '@@'. If
" the note id appears at the beginning of the filename, then this prefix is
" optional. There may exist another field (such as the signature, title, or
" keywords field) after the note id. These are prefixed with '==', '--', or
" '__'. If the note id is the last field, then the id is followed by the file
" extension, e.g., '.md'.
" (A) First, we get all files that contain the note id as substring.
" (B) Then we ensure that the note id is followed by another field or by the
" file extension.
" TODO: Fix the following (now, we have full paths!)
let files = glob(t:denote_directory .. "*" .. a:noteId .. "*", 0, v:true)
\ ->filter('v:val->split("/")[-1] =~ "' .. a:noteId .. '\\(==\\|--\\|__\\|\\.\\)"')
\ ->filter('v:val->split("/")[-1] =~ "^' .. a:noteId .. '\\|@@' .. a:noteId .. '"')
return empty(files) ? v:false : files[0]
endfunction
" Return the note id from the filename. On failure, v:false is returned.
function denote#meta#noteIdFromFile(file)
return a:file->fnamemodify(':t')->matchstr("@@\\zs.\\{-\\}\\ze\\(==\\|--\\|__\\|\\..\\)")
\ ?? a:file->fnamemodify(':t')->matchstr("^.\\{-\\}\\ze\\(==\\|--\\|__\\|\\..\\)")
\ ?? v:false
endfunction
" Return the note title from the filename.
function denote#meta#noteTitleFromFile(file)
return a:file->fnamemodify(':t')->matchstr("--\\zs.\\{-\\}\\ze\\(==\\|@@\\|__\\|\\..\\)")->substitute("-", " ", "g")
endfunction
" Return the note tags from the filename as a list.
function denote#meta#noteTagsFromFile(file)
return a:file->fnamemodify(':t')->matchstr("__\\zs.\\{-\\}\\ze\\(==\\|@@\\|--\\|\\..\\)")->split("_")
endfunction
" Identifier creation
function denote#meta#identifier_generate()
if g:denote_identifier_fun
return execute "call " .. g:denote_identifier_fun .. "()"
endif
return exists("*strftime")
\ ? strftime("%Y%m%dT%H%M%S")
\ : rand()
endfunction