From a8643062881d05ce611122c9c9251ebd791acdf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wiecz=C3=B3r-Retman?= Date: Sat, 24 Jun 2023 21:57:42 +0200 Subject: [PATCH 1/3] Update JsSnippets.md Vimwiki style link navigation --- JsSnippets.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/JsSnippets.md b/JsSnippets.md index b27173d..912a391 100644 --- a/JsSnippets.md +++ b/JsSnippets.md @@ -59,3 +59,39 @@ exmap prevHeading jsfile mdHelpers.js {jumpHeading(false)} nmap ]] :nextHeading nmap [[ :prevHeading ``` + +## Vimwiki-like link navigation + +This snippet allows to navigate next/previous links with Tab/Shift+Tab. + +It mimicks the behaviour of vimwiki keybindings where you would press Tab several times to get to the link you want. + +Append this function to the file mentioned above (after jumpHeading). + +```js +function jumpNextLink(isForward) { + const editor = view.editor; + let posToSearchFrom = editor.getCursor(); + posToSearchFrom.line += isForward ? 1 : -1; + const cursorOffset = editor.posToOffset(posToSearchFrom); + const lookupToUse = isForward ? regexIndexOf : regexLastIndexOf; + let headingOffset = lookupToUse(editor.getValue(), /\[\[/g, cursorOffset); + // If not found from the cursor position, try again from the document beginning (or reverse beginning) + if (headingOffset === -1) + headingOffset = lookupToUse(editor.getValue(), /\[\[/g); + const newPos = editor.offsetToPos(headingOffset+2); + editor.setCursor(newPos); +} +``` + +Then put these lines in vimrc file to set the keybindings. Optionally (as in vimwiki) you can have ENTER bound to follow the link. + +``` +exmap nextLink jsfile mdHelpers.js {jumpNextLink(true)} +exmap prevLink jsfile mdHelpers.js {jumpNextLink(false)} +nmap :nextLink +nmap :prevLink + +exmap followlink obcommand editor:follow-link +nmap :followlink +``` From dd1838a17592e08f5513c2375f92ca7cddac21f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wiecz=C3=B3r-Retman?= Date: Thu, 29 Jun 2023 12:45:50 +0200 Subject: [PATCH 2/3] Update JsSnippets.md --- JsSnippets.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JsSnippets.md b/JsSnippets.md index 912a391..7608eda 100644 --- a/JsSnippets.md +++ b/JsSnippets.md @@ -39,7 +39,7 @@ function regexLastIndexOf(string, regex, startpos) { function jumpHeading(isForward) { const editor = view.editor; let posToSearchFrom = editor.getCursor(); - posToSearchFrom.line += isForward ? 1 : -1; + posToSearchFrom.line += isForward ? 0 : -1; const cursorOffset = editor.posToOffset(posToSearchFrom); const lookupToUse = isForward ? regexIndexOf : regexLastIndexOf; let headingOffset = lookupToUse(editor.getValue(), /^#(#*) /gm, cursorOffset); From 0895ca4d0749877b250efb0f5a3adb0752d89837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Wiecz=C3=B3r-Retman?= Date: Thu, 29 Jun 2023 12:46:35 +0200 Subject: [PATCH 3/3] Update JsSnippets.md --- JsSnippets.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JsSnippets.md b/JsSnippets.md index 7608eda..e42f3da 100644 --- a/JsSnippets.md +++ b/JsSnippets.md @@ -39,7 +39,7 @@ function regexLastIndexOf(string, regex, startpos) { function jumpHeading(isForward) { const editor = view.editor; let posToSearchFrom = editor.getCursor(); - posToSearchFrom.line += isForward ? 0 : -1; + posToSearchFrom.line += isForward ? 1 : -1; const cursorOffset = editor.posToOffset(posToSearchFrom); const lookupToUse = isForward ? regexIndexOf : regexLastIndexOf; let headingOffset = lookupToUse(editor.getValue(), /^#(#*) /gm, cursorOffset); @@ -72,7 +72,7 @@ Append this function to the file mentioned above (after jumpHeading). function jumpNextLink(isForward) { const editor = view.editor; let posToSearchFrom = editor.getCursor(); - posToSearchFrom.line += isForward ? 1 : -1; + posToSearchFrom.line += isForward ? 0 : -1; const cursorOffset = editor.posToOffset(posToSearchFrom); const lookupToUse = isForward ? regexIndexOf : regexLastIndexOf; let headingOffset = lookupToUse(editor.getValue(), /\[\[/g, cursorOffset);