From 47f5a32ce8a968eeff78fba0ea83dbc21097f855 Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Fri, 21 Nov 2025 13:29:19 -0500 Subject: [PATCH] fix: Fixes readme. --- README.md | 27 +++++++++++++- lua/shortenurl/init.lua | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 lua/shortenurl/init.lua diff --git a/README.md b/README.md index fa2a268..43874c0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,28 @@ # shortenurl.nvim -Small neovim plugin to shorten urls, using shlink and the shorten-url scripts in my dotfiles. \ No newline at end of file +This is a small neovim plugin that can convert urls to short urls, using the `shoren-url` bash script found in my +dotfiles repo. + +## Usage + +```lua +vim.pack.add({ + { src = "https://git.housh.dev/michael/shortenurl.nvm" }, +}) +``` + +### Create keybinds + +```lua +vim.keymap.set({ 'n', 'v', 'x' }, 'lc', function() + require('shortenurl').convertUrl() +end, { desc = "[L]ink [c]onvert to short url." }) + +vim.keymap.set('n', 'ls', function() + require('shortenurl').prompt() +end, { desc = "[L]ink [s]horten, prompting for url." }) + +vim.keymap.set('n', 'lv', function() + require('shortenurl').convertFromClipboard() +end, { desc = "[L]ink convert url from cliboard to short url." }) +``` diff --git a/lua/shortenurl/init.lua b/lua/shortenurl/init.lua new file mode 100644 index 0000000..23e9049 --- /dev/null +++ b/lua/shortenurl/init.lua @@ -0,0 +1,79 @@ +local shortenurl = {} + +-- Trim whitespace from string. +local function trim(s) + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + +-- Get url on the current line. +local function get_url() + local line = vim.api.nvim_get_current_line() + local url = string.match(line, 'http?s://[^ ,;)]*') + return url +end + +-- Remove trailing newline if present +local function trimNewline(s) + return s:gsub("\n$", "") +end + +local function trimQuotes(s) + return s:gsub("\"", "") +end + +-- Helper function that prompts for user input to generate a short link. +shortenurl.prompt = function(s) + local url = s + if not url or string.len(url) == 0 then + url = vim.fn.input("URL: ") + end + + local tags = vim.fn.input("[Optional] Tag(s) (seperated by commas): ") + local expires = vim.fn.input("[Optional] Expire: ") + local shortCode = vim.fn.input("[Optional] Short Code: ") + local cmd = "shorten-url --no-spin create" + + for tag in tags:gmatch('([^,]+)') do + cmd = cmd .. " --tag " .. trim(tag) + end + + if string.len(expires) > 0 then + cmd = cmd .. " --expire " .. expires + end + + if string.len(shortCode) > 0 then + cmd = cmd .. " --code " .. shortCode + end + + cmd = cmd .. " " .. url + return cmd +end + +-- Convert url on the current line. +shortenurl.convertUrl = function() + local url = get_url() + local cmd = shortenurl.prompt(url) + local output = trimNewline(vim.fn.system(cmd)) + if output == "" then + print("Got empty output") + return + end + output = trimQuotes(output) + -- Replace occurence of the url with the shortened url. + vim.cmd(":%s/" .. vim.fn.escape(url, "/") .. "/" .. vim.fn.escape(output, "/")) +end + +-- Convert a url in the system clipboard, and insert at cursor postion. +shortenurl.convertFromClipboard = function() + local url = trimNewline(vim.fn.system("wl-paste")) + local cmd = shortenurl.prompt(url) + local output = trimNewline(vim.fn.system(cmd)) + if not output or output == "" then + print("Got empty output") + return + end + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + vim.api.nvim_buf_set_text(0, row - 1, col, row - 1, col, { output }) +end + +return shortenurl