local preparetext = function(inputtext) local formatted = inputtext formatted =…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
local prepare_text = function(input_text) local formatted = input_text formatted = formatted:gsub(" ", "") formatted = formatted:gsub("Ü", "Ue") formatted = formatted:gsub("Ä", "Ae") formatted = formatted:gsub("Ö", "Oe") formatted = formatted:gsub("ü", "ue") formatted = formatted:gsub("ä", "ae") formatted = formatted:gsub("ö", "oe") formatted = formatted:gsub("ß", "ss") formatted = formatted:gsub("'", "") formatted = formatted:gsub(",", "") formatted = formatted:gsub("-", "") formatted = formatted:gsub(";", "") formatted = formatted:gsub("&", "") formatted = formatted:lower() return formatted end
M.create_markdown_link = function(bool) local cursor_col = vim.fn.col(".") - 1 local current_line = vim.fn.getline(".") local text_before_cursor = current_line:sub(1, cursor_col) local text_after_cursor = current_line:sub(cursor_col + 1) local reversed_text = text_before_cursor:reverse()
if reversed_text:find("^%s") then return end
if reversed_text:find("^[^%)]") then local word_to_link = reversed_text:match("[%wäÄüÜöÖß'&%-%.]*") local formatted_link = word_to_link:reverse() formatted_link = prepare_text(formatted_link) formatted_link = formatted_link:reverse() local markdown_link = ")dm." .. formatted_link .. "(]" .. word_to_link .. "[" word_to_link = word_to_link:gsub("%-", "%%-") reversed_text = reversed_text:gsub(word_to_link, markdown_link) local new_line = reversed_text:reverse() vim.fn.setline(".", new_line .. text_after_cursor) vim.cmd("norm f)") vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, true, true), "i", true) return end
local link_pattern = "%)dm%.([^%(]-)%(%]([^%[]-)%[" local word_pattern = " (%S*)" local existing_link, link_text, new_word = reversed_text:match(link_pattern .. word_pattern)
if new_word == nil then return end if new_word:match("%)[^%(]-%(%].-") then return end
if new_word then local formatted_word = new_word:reverse() formatted_word = prepare_text(formatted_word) formatted_word = formatted_word:reverse() local old_pattern = "%)dm%." .. existing_link .. "%(%]" .. link_text .. "%[ " .. new_word local new_link = "" if bool then new_link = ")dm." .. existing_link .. "_" .. formatted_word .. "(]" .. link_text .. " " .. new_word .. "[" else new_link = ")dm." .. existing_link .. "(]" .. link_text .. " " .. new_word .. "[" end reversed_text = reversed_text:gsub(old_pattern, new_link) end local final_line = reversed_text:reverse() vim.fn.setline(".", final_line .. text_after_cursor) vim.fn.cursor(0, #vim.fn.getline(".") - #text_after_cursor + 1) end
This Lua code defines a function create_markdown_link that helps create and modify markdown-style links in Neovim. Let me break down what it does:
prepare_text function:
create_markdown_link function:
First mode (when not in a link already):
prepare_text)[formatted_word](wordSecond mode (when already in a link):
Looks for existing markdown links in the format )[link](text[ word
If a new word is found after the link, it either:
bool is true)Updates the current line with the modified content
Moves the cursor to the appropriate position after modification
The function appears to be designed for creating and extending markdown links in a German-language context, with special handling for umlauts and other German characters. The bool parameter determines whether to append new words to the link or not.
This would be useful for quickly creating wiki-style links in markdown files while maintaining consistent formatting of German text.
can you rewrite this to work without reversing the text by finding the positions of spaces and use this to create or append markdown links. That should be siimpler, at least in my imagination.
You're right - working with string positions directly would be simpler than reversing the text. Here's a rewritten version that avoids string reversal and instead works with character positions:
local prepare_text = function(input_text)
local formatted = input_text
formatted = formatted:gsub(" ", "_")
formatted = formatted:gsub("Ü", "Ue")
formatted = formatted:gsub("Ä", "Ae")
formatted = formatted:gsub("Ö", "Oe")
formatted = formatted:gsub("ü", "ue")
formatted = formatted:gsub("ä", "ae")
formatted = formatted:gsub("ö", "oe")
formatted = formatted:gsub("ß", "ss")
formatted = formatted:gsub("'", "")
formatted = formatted:gsub(",", "")
formatted = formatted:gsub("-", "")
formatted = formatted:gsub(";", "")
formatted = formatted:gsub("&", "_")
formatted = formatted:lower()
return formatted
end
M.create_markdown_link = function(bool)
local cursor_col = vim.fn.col(".") - 1 -- 0-based index
local current_line = vim.fn.getline(".")
local line_length = #current_line
-- Find the start of the current word
local word_start = cursor_col
while word_start > 0 and current_line:sub(word_start, word_start):match("[%wäÄüÜöÖß'&%-]") do
word_start = word_start - 1
end
word_start = word_start + 1 -- Adjust to point to first character of word
-- Find the end of the current word
local word_end = cursor_col
while word_end <= line_length and current_line:sub(word_end, word_end):match("[%wäÄüÜöÖß'&%-]") do
word_end = word_end + 1
end
word_end = word_end - 1 -- Adjust to point to last character of word
-- Get the current word
local current_word = current_line:sub(word_start, word_end)
-- Check if we're inside a markdown link
local link_start, link_end, link_text, link_url = current_line:sub(1, word_start - 1):find("()dm%.([^%(]-)%(%]([^%[]-)%[")
if link_start then
-- We're inside or after a markdown link
local space_pos = current_line:find(" ", word_end + 1) or (line_length + 1)
local next_word = current_line:sub(word_end + 1, space_pos - 1)
if next_word ~= "" and not next_word:match("%)[^%(]-%(%]") then
local formatted_word = prepare_text(next_word)
local new_link
if bool then
-- Append to existing link
new_link = string.format(")[%s_%s](%s[%s %s[",
link_url, formatted_word, link_text, link_text, next_word)
else
-- Keep existing link but include new word
new_link = string.format(")[%s](%s[%s %s[",
link_url, link_text, link_text, next_word)
end
-- Replace the old pattern with the new link
local old_pattern = string.format("()dm%%.%s%%(%%%]%s%%[%s",
link_url:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1"),
link_text:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1"),
next_word:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1"))
local new_line = current_line:gsub(old_pattern, new_link)
vim.fn.setline(".", new_line)
vim.fn.cursor(0, word_start + #new_link - 1)
return
end
else
-- Not in a link - create a new one
if current_word ~= "" then
local formatted_word = prepare_text(current_word)
local new_link = string.format(")[%s](%s[", formatted_word, current_word)
-- Replace the current word with the new link
local new_line = current_line:sub(1, word_start - 1)
.. new_link
.. current_line:sub(word_end + 1)
vim.fn.setline(".", new_line)
vim.fn.cursor(0, word_start + #new_link - 1)
return
end
end
end
Key improvements in this version:
The function still maintains all the original functionality:
bool is trueYou might want to test this with various edge cases (words at start/end of line, multiple spaces, etc.) to ensure it handles all scenarios correctly.