更改了host和token的获取方式和路径

This commit is contained in:
Elflare
2025-07-06 10:04:20 +08:00
parent 1488b2f96b
commit 753959ccd7
2 changed files with 129 additions and 47 deletions

View File

@@ -10,6 +10,13 @@ A Neovim plugin to interact with [Memos](https://github.com/usememos/memos) righ
- **Create & Edit**: Create new memos or edit existing ones in a dedicated buffer with `markdown` filetype support. - **Create & Edit**: Create new memos or edit existing ones in a dedicated buffer with `markdown` filetype support.
- **Delete Memos**: Delete memos directly from the list. - **Delete Memos**: Delete memos directly from the list.
- **Customizable**: Configure API endpoints, keymaps, and more. - **Customizable**: Configure API endpoints, keymaps, and more.
- **First-time Setup**: On first launch, you will be prompted to enter your Memos host and token. You can choose to save these permanently.
> **Config Save Path**:
> If you choose to save your host and token, the config file will be stored at:
> - **macOS**: `~/.config/nvim/memos.nvim/config.json`
> - **Linux**: `~/.config/nvim/memos.nvim/config.json`
> - **Windows**: `%USERPROFILE%\AppData\Local\nvim\memos.nvim\config.json`
## 📦 Installation ## 📦 Installation
@@ -60,14 +67,15 @@ Install with [lazy.nvim](https://github.com/folke/lazy.nvim):
You can override the default settings by passing a table to the `setup()` function. You can override the default settings by passing a table to the `setup()` function.
> **Note:** On first use, you will be prompted to enter your Memos host and token. You can choose to save these permanently.
> The config file will be stored at:
> - **macOS**: `~/.config/nvim/memos.nvim/config.json`
> - **Linux**: `~/.config/nvim/memos.nvim/config.json`
> - **Windows**: `%USERPROFILE%\AppData\Local\nvim\memos.nvim\config.json`
```lua ```lua
-- lua/plugins/memos.lua -- lua/plugins/memos.lua
require("memos").setup({ require("memos").setup({
-- REQUIRED: Your Memos host URL
host = "https://your-memos-host.com",
-- REQUIRED: Your Memos API token
token = "your-super-secret-token",
-- Number of memos to fetch per page -- Number of memos to fetch per page
pageSize = 50, pageSize = 50,
@@ -111,6 +119,13 @@ require("memos").setup({
- **创建与编辑**: 在专用的、支持 `markdown` 文件类型的缓冲区中创建新 memo 或编辑现有 memo。 - **创建与编辑**: 在专用的、支持 `markdown` 文件类型的缓冲区中创建新 memo 或编辑现有 memo。
- **删除 Memos**: 直接从列表中删除 memo。 - **删除 Memos**: 直接从列表中删除 memo。
- **可定制**: 可配置 API 地址、快捷键等。 - **可定制**: 可配置 API 地址、快捷键等。
- **首次启动引导**: 首次启动时会提示输入 Memos 的 host 和 token并询问是否永久保存。
> **配置保存路径**
> 如果选择永久保存,配置文件将存储在:
> - **macOS**: `~/.config/nvim/memos.nvim/config.json`
> - **Linux**: `~/.config/nvim/memos.nvim/config.json`
> - **Windows**: `%USERPROFILE%\AppData\Local\nvim\memos.nvim\config.json`
## 📦 安装 ## 📦 安装
@@ -161,14 +176,15 @@ require("memos").setup({
你可以通过向 `setup()` 函数传递一个 table 来覆盖默认设置。 你可以通过向 `setup()` 函数传递一个 table 来覆盖默认设置。
> **注意:** 首次使用时会提示输入 Memos 的 host 和 token并询问是否永久保存。
> 配置文件将存储在:
> - **macOS**: `~/.config/nvim/memos.nvim/config.json`
> - **Linux**: `~/.config/nvim/memos.nvim/config.json`
> - **Windows**: `%USERPROFILE%\AppData\Local\nvim\memos.nvim\config.json`
```lua ```lua
-- lua/plugins/memos.lua -- lua/plugins/memos.lua
require("memos").setup({ require("memos").setup({
-- 必填: 你的 Memos 服务地址
host = "https://your-memos-host.com",
-- 必填: 你的 Memos API 令牌
token = "your-super-secret-token",
-- 每页获取的 memo 数量 -- 每页获取的 memo 数量
pageSize = 50, pageSize = 50,
@@ -196,3 +212,4 @@ require("memos").setup({
}, },
}, },
}) })
```

View File

@@ -8,46 +8,111 @@ M.config = {
start_memos = "<leader>mm", start_memos = "<leader>mm",
-- 在列表窗口中的快捷键 -- 在列表窗口中的快捷键
list = { list = {
add_memo = "a", add_memo = 'a',
delete_memo = "d", delete_memo = 'd',
delete_memo_visual = "dd", -- 和 d 功能一样,为了符合习惯 delete_memo_visual = 'dd',
edit_memo = "<CR>", edit_memo = '<CR>',
vsplit_edit_memo = "<Tab>", vsplit_edit_memo = '<Tab>',
search_memos = "s", search_memos = 's',
refresh_list = "r", refresh_list = 'r',
next_page = ".", next_page = '.',
quit = "q", quit = 'q'
}, },
-- 在编辑和创建窗口中的快捷键 -- 在编辑和创建窗口中的快捷键
buffer = { buffer = {
save = "<leader>ms", save = '<leader>ms'
}, }
}, }
} }
function M.setup(opts) local config_dir = vim.fn.stdpath("config") .. "/memos.nvim"
-- 使用 "force" 策略,并将默认配置放在前面,用户的配置在后面 local config_file_path = config_dir .. "/config.json"
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
if not M.config.host or not M.config.token then -- 【修改】全新的、能处理 host 和 token 的交互式配置函数
vim.notify("Memos: `host` and `token` must be configured.", vim.log.levels.ERROR) local function prompt_for_config()
local function prompt_for_token()
vim.ui.input({ prompt = "Memos Access Token:", hide = true }, function(token)
if token and token ~= "" then
M.config.token = token
local choice = vim.fn.confirm("Save host and token for future sessions?", "&Yes\n&No", 2)
if choice == 1 then
vim.fn.mkdir(config_dir, "p")
-- 将 host 和 token 一起存入 JSON 文件
local config_to_save = { host = M.config.host, token = M.config.token }
vim.fn.writefile({ vim.json.encode(config_to_save) }, config_file_path)
vim.notify("Host and token saved permanently.", vim.log.levels.INFO)
end
else
vim.notify("No token entered. Memos plugin will not work.", vim.log.levels.ERROR)
end
end)
end end
if M.config.keymaps.start_memos then if not M.config.host then
vim.api.nvim_set_keymap( vim.ui.input({ prompt = "Memos Host URL (e.g., http://127.0.0.1:5230):" }, function(host)
"n", if host and host ~= "" then
M.config.keymaps.start_memos, M.config.host = host
"<Cmd>Memos<CR>", -- 获取到 host 后,接着获取 token
{ noremap = true, silent = true, desc = "Open Memos list" } prompt_for_token()
) else
vim.notify("No host entered. Memos plugin will not work.", vim.log.levels.ERROR)
end
end)
else
-- 如果 host 已存在,只获取 token
prompt_for_token()
end
end
function M.setup(opts)
-- 1. 先加载默认配置
local final_config = vim.deepcopy(M.config)
-- 2. 加载文件中的配置
if vim.fn.filereadable(config_file_path) == 1 then
local file_content = vim.fn.readfile(config_file_path)
if file_content and #file_content > 0 and file_content[1] ~= "" then
local saved_config = vim.json.decode(file_content[1])
final_config = vim.tbl_deep_extend("force", final_config, saved_config)
end
end
-- 3. 加载环境变量 (优先级高于文件)
local token_from_env = os.getenv('MEMOS_TOKEN')
if token_from_env and token_from_env ~= "" then
final_config.token = token_from_env
end
local host_from_env = os.getenv('MEMOS_HOST')
if host_from_env and host_from_env ~= "" then
final_config.host = host_from_env
end
-- 4. 加载用户在 setup() 中直接提供的配置 (优先级最高)
final_config = vim.tbl_deep_extend("force", final_config, opts or {})
M.config = final_config
end
-- 【修改】确保在执行任何操作前,配置是完整的
local function ensure_config(callback)
if M.config.host and M.config.token then
callback()
else
prompt_for_config()
end end
end end
function M.create_memo() function M.create_memo()
require("memos.ui").create_memo_in_buffer() ensure_config(function()
require('memos.ui').create_memo_in_buffer()
end)
end end
function M.show_list() function M.show_list()
require("memos.ui").show_memos_list() ensure_config(function()
require('memos.ui').show_memos_list()
end)
end end
return M return M