image link insertion, plus img preview via extension

This commit is contained in:
Rene Schallner
2021-11-30 14:16:01 +01:00
parent 853412aca6
commit 88aa0e4d62
4 changed files with 138 additions and 5 deletions

View File

@@ -3,6 +3,8 @@
- [ ] yt video - [ ] yt video
## Dones ## Dones
- [x] insert link to image, with preview
- [x] sorting file picker: devicons, ...
- [x] dailies and weeklies: don't display whole paths: fix entries: display values - [x] dailies and weeklies: don't display whole paths: fix entries: display values
- [x] added option parameter `i` to `toggle_todo(opts)` and `insert_link(opts)` to enter insert mode. - [x] added option parameter `i` to `toggle_todo(opts)` and `insert_link(opts)` to enter insert mode.
- [x] find_friends() - [x] find_friends()

View File

@@ -14,6 +14,7 @@ A Neovim (lua) plugin for working with a text-based, markdown [zettelkasten](htt
- calendar support - calendar support
- paste images from clipboard - paste images from clipboard
- toggle [ ] todo status of line - toggle [ ] todo status of line
- insert links to images, with **image previews**, via extension _(Linux only)_
##### New features are being announced in the [discussions](https://github.com/renerocksai/telekasten.nvim/discussions)! ##### New features are being announced in the [discussions](https://github.com/renerocksai/telekasten.nvim/discussions)!
@@ -53,6 +54,7 @@ I find that pressing the enter key to confirm the search does not interrupt my f
* [Telescope](#telescope) * [Telescope](#telescope)
* [calendar-vim Plugin (optional)](#calendar-vim-plugin-optional) * [calendar-vim Plugin (optional)](#calendar-vim-plugin-optional)
* [For pasting images: xclip (optional)](#for-pasting-images-xclip-optional) * [For pasting images: xclip (optional)](#for-pasting-images-xclip-optional)
* [For image previews: telescope-media-files.nvim (optional)](#for-image-previews-telescope-media-filesnvim-optional)
* [1. Install the plugin](#1-install-the-plugin) * [1. Install the plugin](#1-install-the-plugin)
* [Other useful plugins](#other-useful-plugins) * [Other useful plugins](#other-useful-plugins)
* [2. Configure telekasten.nvim](#2-configure-telekastennvim) * [2. Configure telekasten.nvim](#2-configure-telekastennvim)
@@ -94,6 +96,26 @@ On Ubuntu/Debian like systems:
$ sudo apt-get install xclip $ sudo apt-get install xclip
``` ```
#### For image previews: telescope-media-files.nvim (optional)
**ONLY SUPPORTED ON LINUX**
To preview images, PDFs, etc. in Telescope while searching for an image to insert a link to, you need to install the
Telescope extension [telescope-media-files.nvim](https://github.com/nvim-telescope/telescope-media-files.nvim).
This extension has its own list of prerequisites, of which I recommend (and use) the following:
* [Überzug](https://github.com/seebye/ueberzug) (required for image support)
* [ffmpegthumbnailer](https://github.com/dirkvdb/ffmpegthumbnailer) (optional, for video preview support)
* [pdftoppm](https://linux.die.net/man/1/pdftoppm) (optional, for pdf preview support. Available in the AUR as **poppler** package.)
Here is a demo from the original authors of
[telescope-media-files.nvim](https://github.com/nvim-telescope/telescope-media-files.nvim):
![Demo](https://i.imgur.com/wEO04TK.gif)
### 1. Install the plugin ### 1. Install the plugin
Install with your plugin manager of choice. Mine is [Vundle](https://github.com/VundleVim/Vundle.vim). Install with your plugin manager of choice. Mine is [Vundle](https://github.com/VundleVim/Vundle.vim).
@@ -270,6 +292,11 @@ The plugin defines the following functions.
- example: `toggle_todo({ i=true })` - example: `toggle_todo({ i=true })`
- `show_backlinks()` : opens a telescope search for notes that `[[link]]` back to the current note. - `show_backlinks()` : opens a telescope search for notes that `[[link]]` back to the current note.
- `find_friends()` : opens a telescope search for notes that also `[[link]]` to the link under the cursor. - `find_friends()` : opens a telescope search for notes that also `[[link]]` to the link under the cursor.
- `insert_img_link()` : opens a telescope search for all media (PDFs, images, videos (MP4, webm)) and places a markdown image link to the picked one at the cursor position.
- **note**:
- if the `telescope-media-files.nvim` plugin is installed, **a preview of images / media files will be given** during the search.
- this function accepts a parameter `{i}`. If `true`, it will enter input mode by pressing the 'A' key. This is useful for being able to continue to type after link insertion. See also: [Bind it](#bind-it).
- example: `insert_link({ i=true })`
- `setup(opts)`: used for configuring paths, file extension, etc. - `setup(opts)`: used for configuring paths, file extension, etc.
To use one of the functions above, just run them with the `:lua ...` command. To use one of the functions above, just run them with the `:lua ...` command.
@@ -386,6 +413,7 @@ nnoremap <leader>zi :lua require('telekasten').paste_img_and_link()<CR>
nnoremap <leader>zt :lua require('telekasten').toggle_todo()<CR> nnoremap <leader>zt :lua require('telekasten').toggle_todo()<CR>
nnoremap <leader>zb :lua require('telekasten').show_backlinks()<CR> nnoremap <leader>zb :lua require('telekasten').show_backlinks()<CR>
nnoremap <leader>zF :lua require('telekasten').find_friends()<CR> nnoremap <leader>zF :lua require('telekasten').find_friends()<CR>
nnoremap <leader>zI <ESC>:lua require('telekasten').insert_img_link({ i=true })<CR>
" we could define [[ in **insert mode** to call insert link " we could define [[ in **insert mode** to call insert link
" inoremap [[ <ESC>:lua require('telekasten').insert_link()<CR> " inoremap [[ <ESC>:lua require('telekasten').insert_link()<CR>

View File

@@ -436,6 +436,24 @@ telekasten.find_friends()~
Opens a telescope search for notes that also `[[link]]` to the link under the Opens a telescope search for notes that also `[[link]]` to the link under the
cursor. cursor.
*telekasten.insert_img_link()*
telekasten.insert_img_link({opts})~
Opens a telescope search for all media (PDFs, images, videos (MP4, webm)) and
places a markdown image link to the picked one at the cursor position.
Note:~
If the `telescope-media-files.nvim` plugin is installed, a preview of
images / media files will be given during the search.
Valid keys for {opts}
i:~
If `true`, it will enter input mode by pressing the <A> key. This is useful
for being able to continue typing after the link has been inserted.
See also: |telekasten.mappings|
Default: `nil`
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
Section 3.1: Templates *telekasten.templates* Section 3.1: Templates *telekasten.templates*
@@ -565,6 +583,7 @@ However, here are some suggestions:
nnoremap <leader>zt :lua require('telekasten').toggle_todo()<CR> nnoremap <leader>zt :lua require('telekasten').toggle_todo()<CR>
nnoremap <leader>zb :lua require('telekasten').show_backlinks()<CR> nnoremap <leader>zb :lua require('telekasten').show_backlinks()<CR>
nnoremap <leader>zF :lua require('telekasten').find_friends()<CR> nnoremap <leader>zF :lua require('telekasten').find_friends()<CR>
nnoremap <leader>zI <ESC>:lua require('telekasten').insert_img_link({ i=true })<CR>
" we could define [[ in **insert mode** to call insert link " we could define [[ in **insert mode** to call insert link
" inoremap [[ <ESC>:lua require('telekasten').insert_link()<CR> " inoremap [[ <ESC>:lua require('telekasten').insert_link()<CR>

View File

@@ -6,6 +6,7 @@ local finders = require("telescope.finders")
local conf = require("telescope.config").values local conf = require("telescope.config").values
local scan = require("plenary.scandir") local scan = require("plenary.scandir")
local utils = require("telescope.utils") local utils = require("telescope.utils")
local previewers = require("telescope.previewers")
-- declare locals for the nvim api stuff to avoid more lsp warnings -- declare locals for the nvim api stuff to avoid more lsp warnings
local vim = vim local vim = vim
@@ -256,6 +257,33 @@ local function filter_filetypes(flist, ftypes)
return new_fl return new_fl
end end
local media_files_base_directory = "../telescope-media-files.nvim"
local defaulter = utils.make_default_callable
local media_preview = defaulter(function(opts)
local preview_cmd = media_files_base_directory .. "/scripts/vimg"
if vim.fn.executable(preview_cmd) == 0 then
return conf.file_previewer(opts)
end
return previewers.new_termopen_previewer({
get_command = opts.get_command or function(entry)
local tmp_table = vim.split(entry.value, "\t")
local preview = opts.get_preview_window()
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or vim.loop.cwd()
if vim.tbl_isempty(tmp_table) then
return { "echo", "" }
end
return {
preview_cmd,
tmp_table[1],
preview.col,
preview.line + 1,
preview.width,
preview.height,
}
end,
})
end, {})
-- find_files_sorted(opts) -- find_files_sorted(opts)
-- like builtin.find_files, but: -- like builtin.find_files, but:
-- - uses plenary.scan_dir synchronously instead of external jobs -- - uses plenary.scan_dir synchronously instead of external jobs
@@ -265,11 +293,14 @@ end
-- - displays subdirs if necessary -- - displays subdirs if necessary
-- - e.g. when searching for daily notes, no subdirs are displayed -- - e.g. when searching for daily notes, no subdirs are displayed
-- - but when entering a date in find_notes, the daily/ and weekly/ subdirs are displayed -- - but when entering a date in find_notes, the daily/ and weekly/ subdirs are displayed
-- - optionally previews media (pdf, images, mp4, webm)
-- - this requires the telescope-media-files.nvim extension
local function find_files_sorted(opts) local function find_files_sorted(opts)
opts = opts or {} opts = opts or {}
local file_list = scan.scan_dir(opts.cwd, {}) local file_list = scan.scan_dir(opts.cwd, {})
file_list = filter_filetypes(file_list, M.Cfg.filter_extensions) local filter_extensions = opts.filter_extensions or M.Cfg.filter_extensions
file_list = filter_filetypes(file_list, filter_extensions)
table.sort(file_list, order_numeric) table.sort(file_list, order_numeric)
-- display with devicons -- display with devicons
@@ -292,6 +323,12 @@ local function find_files_sorted(opts)
end end
end end
-- for media_files
local popup_opts = {}
opts.get_preview_window = function()
return popup_opts.preview
end
local function make_entry(entry) local function make_entry(entry)
local iconic_entry = {} local iconic_entry = {}
iconic_entry.value = entry iconic_entry.value = entry
@@ -300,14 +337,29 @@ local function find_files_sorted(opts)
return iconic_entry return iconic_entry
end end
pickers.new(opts, { local previewer = conf.file_previewer(opts)
if opts.preview_type == "media" then
previewer = media_preview.new(opts)
end
local picker = pickers.new(opts, {
finder = finders.new_table({ finder = finders.new_table({
results = file_list, results = file_list,
entry_maker = make_entry, entry_maker = make_entry,
}), }),
sorter = conf.generic_sorter(opts), sorter = conf.generic_sorter(opts),
previewer = conf.file_previewer(opts),
}):find() previewer = previewer,
})
-- for media_files:
local line_count = vim.o.lines - vim.o.cmdheight
if vim.o.laststatus ~= 0 then
line_count = line_count - 1
end
popup_opts = picker:get_window_options(vim.o.columns, line_count)
picker:find()
end end
-- --
@@ -501,6 +553,37 @@ local function FindNotes(_)
}) })
end end
--
-- InsertImgLink:
-- --------------
--
-- Select from notes
--
local function InsertImgLink(opts)
opts = opts or {}
find_files_sorted({
prompt_title = "Find image/media",
cwd = M.Cfg.home,
find_command = M.Cfg.find_command,
filter_extensions = { ".png", ".jpg", ".bmp", ".gif", ".pdf", ".mp4", ".webm" },
preview_type = "media",
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
local fn = selection.value
fn = fn:gsub(M.Cfg.home .. "/", "")
vim.api.nvim_put({ "![](" .. fn .. ")" }, "", false, true)
if opts.i then
vim.api.nvim_feedkeys("A", "m", false)
end
end)
return true
end,
})
end
-- --
-- SearchNotes: -- SearchNotes:
-- ------------ -- ------------
@@ -774,7 +857,7 @@ local function Setup(cfg)
end end
-- TODO: this is obsolete: -- TODO: this is obsolete:
if vim.fn.executable("rg") then if vim.fn.executable("rg") == 1 then
M.Cfg.find_command = { "rg", "--files", "--sortr", "created" } M.Cfg.find_command = { "rg", "--files", "--sortr", "created" }
else else
M.Cfg.find_command = nil M.Cfg.find_command = nil
@@ -831,5 +914,6 @@ M.paste_img_and_link = imgFromClipboard
M.toggle_todo = ToggleTodo M.toggle_todo = ToggleTodo
M.show_backlinks = ShowBacklinks M.show_backlinks = ShowBacklinks
M.find_friends = FindFriends M.find_friends = FindFriends
M.insert_img_link = InsertImgLink
return M return M