From 89ae5fd0aabc483c7fba1c3401846ef7be8d74d3 Mon Sep 17 00:00:00 2001 From: jsrozner <1113285+jsrozner@users.noreply.github.com> Date: Thu, 21 Mar 2024 16:42:30 -0700 Subject: [PATCH] Add a jssnipped to skip folds --- JsSnippets.md | 20 ++++++++++++++++++++ README.md | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/JsSnippets.md b/JsSnippets.md index e42f3da..c23ba00 100644 --- a/JsSnippets.md +++ b/JsSnippets.md @@ -60,6 +60,26 @@ nmap ]] :nextHeading nmap [[ :prevHeading ``` + +## Avoid unfolding folded sections +```javascript +function moveUpSkipFold() { + view.editor.exec('goUp'); +} +function moveDownSkipFold() { + view.editor.exec('goDown'); +} +``` +Then in your `.obsidian.vimrc` file add the following: + +``` +exmap upSkipFold jsfile mdHelpers.js {moveUpSkipFold()} +exmap downSkipFold jsfile mdHelpers.js {moveDownSkipFold()} +nmap k :upSkipFold +nmap j :downSkipFold +``` + + ## Vimwiki-like link navigation This snippet allows to navigate next/previous links with Tab/Shift+Tab. diff --git a/README.md b/README.md index bd0a1da..b87cb02 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,7 @@ If you understand the risks and choose to use this feature, turn on "Support JS There are two ways to define JS-based commands. +#### JSCommand - JSFunction **The `jscommand` Ex command** defines a JS function that has an `editor: Editor`, a `view: MarkdownView` and a `selection: EditorSelection` arguments (see the [Obsidian API](https://github.com/obsidianmd/obsidian-api/blob/master/obsidian.d.ts) if you're not sure what these are). You define only the body of the function, in a single line wrapped by curly braces, e.g.: @@ -276,8 +277,11 @@ exmap logCursor jscommand { console.log(editor.getCursor()); } nmap :logCursor ``` +#### JSCommand - JSFile Another version of the same functionality is **the `jsfile` Ex command**, which executes code from a file you give as a parameter, then appends another optional piece of code to it (e.g. in case you want to store several helper methods in a file and launch different ones as part of different commands). +The `jsfile` should be placed in your vault (alongside, e.g., your markdown files). + As above, the code running as part of `jsfile` has the arguments `editor: Editor`, `view: MarkdownView` and `selection: EditorSelection`. Here's an example from my own `.obsidian.vimrc` that maps `]]` and `[[` to jump to the next/previous Markdown header: