From 611d2923d335e7782bec6041f90cab27c2e4a104 Mon Sep 17 00:00:00 2001 From: Ilia Ilinykh <18665585+IlyasYOY@users.noreply.github.com> Date: Mon, 14 Nov 2022 22:34:52 +0300 Subject: [PATCH] WikiLink aliases (#181) * Added ability to remove link alias * Added spaces regex 'note | alias' will be displayed correctly. * show_backlinks * find_friends * Fixed simple link processing * updated style hide link body when it's aliased * Review fix * syntax review fix * more fixeS * Documentation * format fix * Added documentation --- doc/telekasten.txt | 11 +++++++++++ lua/telekasten.lua | 14 ++++++++++++-- syntax/telekasten.vim | 17 +++++++++++------ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 5ee6b14..df2dea6 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -475,10 +475,18 @@ groups: - `tkBrackets` : the brackets surrounding the link title - `tkHighlight` : ==highlighted== text (non-standard markdown) - `tkTag` : well, tags +- `tkAliasedLink` : link with alias +- `tkHighlightedAliasedLink` : link with alias in `tkAliasedLink` +- `tkLinkAlias` : body of the link (left part of `[[aaa|bbb]]`) +- `tkLinkBody` : alias of the link (right part of `[[aaa|bbb]]`) `tkHighlight`, has nothing to do with links but I added it anyway, since I like highlighting text when taking notes. +Plugin supports wiki-link aliases: `[[value|alias]]`. These links will be +concealed to `[[alias]]` if `conceallevel=2`. + + I also like the navigation buttons of the calendar to appear less prevalent, so I redefine the `CalNavi` class as well. @@ -736,6 +744,9 @@ Note links~ `[[A cool title#^xxxxxxxx]]` ....... links to the paragraph with id ^xxxxxxxx within the note named 'A cool title' +`[[A cool title|xxxxxxxx]]` ........ links note 'A cool title' with using + name. + `[[#Heading 27]]` .................. links to the heading 'Heading 27' within all notes diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 712fb8c..0c1efa0 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -173,6 +173,14 @@ local function escape(s) return string.gsub(s, "[%%%]%^%-$().[*+?]", "%%%1") end +local function remove_alias(link) + local split_index = string.find(link, "%s*|") + if split_index ~= nil and type(split_index) == "number" then + return string.sub(link, 0, split_index - 1) + end + return link +end + local function file_exists(fname) if fname == nil then return false @@ -1629,12 +1637,13 @@ local function FindFriends(opts) vim.cmd("normal yi]") local title = vim.fn.getreg('"0') + title = remove_alias(title) title = title:gsub("^(%[)(.+)(%])$", "%2") builtin.live_grep({ prompt_title = "Notes referencing `" .. title .. "`", cwd = M.Cfg.home, - default_text = "\\[\\[" .. title .. "\\]\\]", + default_text = "\\[\\[" .. title .. "([#|].+)?\\]\\]", find_command = M.Cfg.find_command, attach_mappings = function(_, map) actions.select_default:replace(picker_actions.select_default) @@ -1965,7 +1974,7 @@ local function ShowBacklinks(opts) prompt_title = "Search", cwd = M.Cfg.home, search_dirs = { M.Cfg.home }, - default_text = "\\[\\[" .. title .. "(#.+)*\\]\\]", + default_text = "\\[\\[" .. title .. "([#|].+)?\\]\\]", find_command = M.Cfg.find_command, attach_mappings = function(_, map) actions.select_default:replace(picker_actions.select_default) @@ -2181,6 +2190,7 @@ local function FollowLink(opts) -- we are in a link vim.cmd("normal yi]") title = vim.fn.getreg('"0') + title = remove_alias(title) title = title:gsub("^(%[)(.+)(%])$", "%2") else -- we are in an external [link] diff --git a/syntax/telekasten.vim b/syntax/telekasten.vim index 057d6fb..600e486 100644 --- a/syntax/telekasten.vim +++ b/syntax/telekasten.vim @@ -8,8 +8,8 @@ unlet b:current_syntax syn region Comment matchgroup=Comment start="" contains=tkTag keepend -syntax region tkLink matchgroup=tkBrackets start=/\[\[/ end=/\]\]/ display oneline -syntax region tkHighlight matchgroup=tkBrackets start=/==/ end=/==/ display oneline +syntax region tkLink matchgroup=tkBrackets start=/\[\[/ end=/\]\]/ display oneline +syntax region tkHighlight matchgroup=tkBrackets start=/==/ end=/==/ display oneline contains=tkHighlightedAliasedLink syntax match tkTag "\v#[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\-_]*" syntax match tkTag "\v:[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\-_]*:" @@ -17,20 +17,25 @@ syntax match tkTag "\v:[a-zA-ZÀ-ÿ]+[a-zA-ZÀ-ÿ0-9/\-_]*:" syntax match tkTagSep "\v\s*,\s*" contained syntax region tkTag matchgroup=tkBrackets start=/^tags\s*:\s*\[\s*/ end=/\s*\]\s*$/ contains=tkTagSep display oneline +syntax region tkAliasedLink start="\[\[[^\]]\+|" end="\]\]" keepend oneline contains=tkLinkAlias,tkLinkBody +syntax region tkHighlightedAliasedLink start="\[\[[^\]]\+|" end="\]\]" keepend oneline contained contains=tkLinkAlias,tkLinkBody +syntax region tkLinkAlias start="|"ms=s+1 end=".+\]\]"me=e-2 keepend contained +syntax region tkLinkBody start="\[\["ms=s+2 end="|" keepend contained conceal let b:current_syntax = 'telekasten' " " just blue -" hi tklink ctermfg=Blue cterm=bold,underline +" hi tkLink ctermfg=Blue cterm=bold,underline " hi tkBrackets ctermfg=gray " " for gruvbox -" hi tklink ctermfg=72 cterm=bold,underline +" hi tkLink ctermfg=72 cterm=bold,underline " hi tkBrackets ctermfg=gray " " Highlight ==highlighted== text " hi tkHighlight ctermbg=yellow ctermfg=darkred cterm=bold + " " " Tags -" hi tkTagSep ctermfg=gray -" hi tkTag ctermfg=magenta +" hi tkTagSep ctermfg=gray +" hi tkTag ctermfg=magenta