From c11a4c19ec85df500c76e5aa32cb36d1c4be52a7 Mon Sep 17 00:00:00 2001 From: Erez Shermer Date: Sun, 1 Nov 2020 13:17:27 +0200 Subject: [PATCH] Initial commit --- .gitignore | 13 +++++++++++++ README.md | 39 +++++++++++++++++++++++++++++++++++++++ main.ts | 36 ++++++++++++++++++++++++++++++++++++ manifest.json | 9 +++++++++ package.json | 23 +++++++++++++++++++++++ rollup.config.js | 19 +++++++++++++++++++ tsconfig.json | 22 ++++++++++++++++++++++ 7 files changed, 161 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 main.ts create mode 100644 manifest.json create mode 100644 package.json create mode 100644 rollup.config.js create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de9609f --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Intellij +*.iml +.idea + +# npm +node_modules +package-lock.json + +# build +main.js +*.js.map + +obsidian.d.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..be95f3f --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Obsidian Vimrc Support Plugin + +This plugin loads a file of Vim commands from `VAULT_ROOT/.obsidian.vimrc`. +For users of the Obsidian.md Vim mode, this is very useful for making various settings (most notably keymaps) persist. + +## Usage + +Just put a file named `.obsidian.vimrc` in your vault root. +If you're using multiple vaults, you'll need this file on each one. + +Here's a simple & useful `.obsidian.vimrc` that I'm using: + +``` +nmap j gj +nmap k gk +nmap H ^ +nmap L $ +nmap :nohl +``` + +## Supported Commands + +The commands that can be used are whatever CodeMirror supports. +I couldn't find a formal list anywhere but you can look for `defaultExCommandMap` in [the source code](https://github.com/codemirror/CodeMirror/blob/master/keymap/vim.js), or play around with trying commands in Obsidian's Vim mode. + +Note that the file parsing is as simple as it gets -- it just sends each line to the CodeMirror Ex-command parser. Therefore comments or other features you may expect don't work. + +Also, commands that fail don't generate any visible error for now. + + +## Wishlist + +There are many things that I wish the CodeMirror implementation would allow. +Many of these can be added using the CodeMirror [API for extending its Vim mode](https://codemirror.net/doc/manual.html#vimapi_extending) and maybe I'll work on these at some point. + +Things I'd love to add: +- Implement some standard `vim-markdown` motions for Obsidian, e.g. `[[`, or implement for CodeMirror the 1-2 missing Ex commands required to define these keymaps in the Vimrc. +- Relative line numbers. + diff --git a/main.ts b/main.ts new file mode 100644 index 0000000..2150104 --- /dev/null +++ b/main.ts @@ -0,0 +1,36 @@ +import { App, Plugin, TFile, MarkdownView } from 'obsidian'; + +export default class MyPlugin extends Plugin { + onload() { + console.log('loading Vimrc plugin'); + + this.registerEvent(this.app.workspace.on('file-open', (file: TFile) => { + const VIMRC_FILE_NAME = '.obsidian.vimrc'; + this.app.vault.adapter.read(VIMRC_FILE_NAME). + then((lines) => this.readVimInit(lines)). + catch(error => { console.log('Error loading vimrc file', VIMRC_FILE_NAME, 'from the vault root') }); + })); + } + + onunload() { + console.log('unloading Vimrc plugin (but Vim commands that were already loaded will still work)'); + } + + readVimInit(vimCommands: string) { + var view = this.app.workspace.activeLeaf.view; + if (view.getViewType() == 'markdown') { + var markdownView = view as MarkdownView; + var cmEditor = markdownView.sourceMode.cmEditor; + if (cmEditor) { + vimCommands.split("\n").forEach( + function(line, index, arr) { + if (line.length > 0) { + CodeMirror.Vim.handleEx(cmEditor, line) + } + } + ) + } + } + } +} + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..8ff5f4b --- /dev/null +++ b/manifest.json @@ -0,0 +1,9 @@ +{ + "id": "obsidian-vimrc-support", + "name": "Obsidian Vimrc Support", + "version": "0.1.0", + "description": "Auto-load a startup file with Obsidian Vim commands.", + "author": "esm", + "authorUrl": "", + "isDesktopOnly": false +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b56183c --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "obsidian-vimrc-support", + "version": "0.1.0", + "description": "Auto-load a startup file with Obsidian Vim commands.", + "main": "main.js", + "scripts": { + "dev": "rollup --config rollup.config.js -w", + "build": "rollup --config rollup.config.js" + }, + "keywords": [], + "author": "", + "license": "MIT", + "devDependencies": { + "@rollup/plugin-commonjs": "^15.1.0", + "@rollup/plugin-node-resolve": "^9.0.0", + "@rollup/plugin-typescript": "^6.1.0", + "@types/node": "^14.14.6", + "obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master", + "rollup": "^2.33.0", + "tslib": "^2.0.3", + "typescript": "^4.0.5" + } +} diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..4f6107e --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,19 @@ +import typescript from '@rollup/plugin-typescript'; +import {nodeResolve} from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; + +export default { + input: 'main.ts', + output: { + dir: '.', + sourcemap: 'inline', + format: 'cjs', + exports: 'default' + }, + external: ['obsidian'], + plugins: [ + typescript(), + nodeResolve({browser: true}), + commonjs(), + ] +}; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d148fe0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "inlineSourceMap": true, + "inlineSources": true, + "module": "ESNext", + "target": "es5", + "allowJs": true, + "noImplicitAny": true, + "moduleResolution": "node", + "importHelpers": true, + "lib": [ + "dom", + "es5", + "scripthost", + "es2015" + ] + }, + "include": [ + "**/*.ts" + ] +}