From d858ee8e8e811adef5c4579c4a802f07d673d7d5 Mon Sep 17 00:00:00 2001 From: Michel Date: Thu, 2 Jan 2025 19:25:01 +0100 Subject: [PATCH] Adds optolith addon --- .config/nvim-notes/init.lua | 3 + .config/nvim-notes/lua/addons/optolith.lua | 77 +++++++++++++++++++ .../lua/plugins/render-markdown.lua | 9 ++- 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 .config/nvim-notes/lua/addons/optolith.lua diff --git a/.config/nvim-notes/init.lua b/.config/nvim-notes/init.lua index 2514f9e..4134e44 100644 --- a/.config/nvim-notes/init.lua +++ b/.config/nvim-notes/init.lua @@ -1,2 +1,5 @@ -- bootstrap lazy.nvim, LazyVim and your plugins require("config.lazy") + +package.path = package.path .. ";./lua" +require("addons.optolith") diff --git a/.config/nvim-notes/lua/addons/optolith.lua b/.config/nvim-notes/lua/addons/optolith.lua new file mode 100644 index 0000000..af909be --- /dev/null +++ b/.config/nvim-notes/lua/addons/optolith.lua @@ -0,0 +1,77 @@ +local nio = require("nio") + +vim.api.nvim_create_user_command("Optolith", function() + local LAUNCH_COMMAND = vim.fs.normalize("~/.local/share/appimages/OptolithInsider.AppImage") + local OPTOLITH_DIRECTORY = "~/.config/Optolith Insider" + + local absOptolithDirectory = vim.fs.normalize(OPTOLITH_DIRECTORY) + local directoryExists = vim.fn.isdirectory(absOptolithDirectory) + + local currentHeroFile = vim.fn.expand("%") + + if currentHeroFile:find(".json$") == nil then + vim.notify("Invalid hero", "error", { + title = "Optolith", + }) + return + end + + if directoryExists == 0 then + vim.notify("Can't find folder. Please start the application once.\nFolder: " .. OPTOLITH_DIRECTORY, "error", { + title = "Optolith", + }) + return + end + + local heroesPath = vim.fs.joinpath(absOptolithDirectory, "heroes.json") + local heroesGlob = vim.fn.glob(heroesPath) + local heroesFileExists = vim.fn.empty(heroesGlob) == 0 + if heroesFileExists and vim.fn.filewritable(heroesPath) == 0 then + vim.notify("Can't modify heroes file... Can't inject hero.", "error", { + title = "Optolith", + }) + return + end + + if heroesFileExists then + local mvCommand = os.execute(string.format('mv "%s" "%s.swp"', heroesPath, heroesPath)) + if mvCommand ~= 0 then + vim.notify("Can not move heroes file.", "error", { + title = "Optolith", + }) + return + end + end + + local linkCommand = os.execute(string.format('ln -s "%s" "%s"', currentHeroFile, heroesPath)) + if linkCommand ~= 0 then + vim.notify("Can't create link...", "error", { + title = "Optolith", + }) + return + end + + local file = table.concat(vim.fn.readfile(currentHeroFile)) + local fileData = vim.fn.json_decode(file) + if fileData["id"] ~= nil then + local newData = {} + newData[fileData["id"]] = fileData + vim.fn.writefile({ vim.fn.json_encode(newData) }, currentHeroFile) + end + + local swapFileExists = vim.fn.empty(vim.fn.glob(heroesPath .. ".swp")) == 0 + + local task = nio.run(function() + local process = nio.process.run({ + cmd = LAUNCH_COMMAND, + }) + + process.result() + end, function() + print("Execute") + os.remove(heroesPath) + if swapFileExists then + os.execute(string.format('mv "%s.swp" "%s"', heroesPath, heroesPath)) + end + end) +end, {}) diff --git a/.config/nvim-notes/lua/plugins/render-markdown.lua b/.config/nvim-notes/lua/plugins/render-markdown.lua index 24543f7..6ef2410 100644 --- a/.config/nvim-notes/lua/plugins/render-markdown.lua +++ b/.config/nvim-notes/lua/plugins/render-markdown.lua @@ -4,8 +4,13 @@ return { opts = {}, config = function() require("render-markdown").setup({ - bullet = { - right_pad = 1, + bullet = {}, + anti_conceal = { + enabled = false, + }, + preset = "obsidian", + heading = { + position = "inline", }, }) end,