Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdir in obsidian.path can't create a new folder in windows. #782

Open
azinsharaf opened this issue Nov 27, 2024 · 0 comments
Open

mkdir in obsidian.path can't create a new folder in windows. #782

azinsharaf opened this issue Nov 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@azinsharaf
Copy link

🐛 Describe the bug

mkdir in obsidian.path can't create a new folder in windows.
I am trying to create daily notes with a specific format (local function get_daily_notes_folder()) so folders need to be created if they don't exist.

Config

local Path = require("plenary.path")

-- Get the OS-specific vault path
local function get_vault_path()
	local os_name = vim.loop.os_uname().sysname
	if os_name == "Windows_NT" then
		return {
			work = Path:new(vim.env.USERPROFILE, "OneDrive - W Inc", "Obsidian", "Obsidian_work"):absolute(),
			personal = Path:new(vim.env.USERPROFILE, "azin_obsidian_personal"):absolute(),
		}
	else
		return {
			work = Path:new(vim.fn.expand("~"), "OneDrive - W Inc", "Obsidian", "Obsidian_work"):absolute(),
			personal = Path:new(vim.fn.expand("~"), "azin_obsidian_personal"):absolute(),
		}
	end
end
--
-- Get resolved paths and define workspaces
local vault_paths = get_vault_path()
print(vault_paths.work)

-- Function to get the current year, month, day, and day of the week
local function get_daily_notes_folder()
	-- Get current year, month number, month name, day, and day of the week
	local year = os.date("%Y")
	local month_number = os.date("%m") -- Month as two digits
	local month_name = os.date("%B") -- Full month name

	-- Dynamically define the base folder for daily notes based on the vault path
	-- local vault_paths = get_vault_path()
	local base_folder = vault_paths.work -- You can change this to `vault_paths.personal` if needed
	print(base_folder)
	--
	--
	--
	-- Check if the base_folder is valid
	if not base_folder or base_folder == "" then
		error("Invalid base folder path: " .. tostring(base_folder))
	end

	-- Construct the nested path for the current year, month, and day
	local folder_path = Path:new(base_folder, "daily_notes", year, month_number .. "-" .. month_name):absolute()

	-- Ensure the folder exists (create if it doesn't)

	-- if not folder_path:exists() then
	-- 	folder_path:mkdir({ parents = true })
	-- end

	return folder_path
end

-- Ensure workspaces are properly named and unique
local workspaces = {
	{ name = "work", path = vault_paths.work },
	{ name = "personal", path = vault_paths.personal },
}

-- Validate paths and create a table of valid workspaces
local valid_workspaces = {}
for _, ws in ipairs(workspaces) do
	if Path:new(ws.path):exists() then
		table.insert(valid_workspaces, ws)
	end
end
-- Define the plugin with dependencies and keybindings

return {
	"epwalsh/obsidian.nvim",
	enabled = true,
	version = "*", -- recommended, use latest release instead of latest commit
	lazy = false,
	ft = "markdown",
	dependencies = {
		-- Required.
		"nvim-lua/plenary.nvim",
		"nvim-telescope/telescope.nvim",
	},

	config = function()
		if #valid_workspaces > 0 then
			-- Configure obsidian.nvim with the valid workspace
			require("obsidian").setup({

				workspaces = valid_workspaces, -- provide all valid workspaces

				-- Optional, completion of wiki links, local markdown links, and tags using nvim-cmp.
				completion = {
					-- Set to false to disable completion.
					nvim_cmp = true,
					-- Trigger completion
					min_chars = 1,
				},

				daily_notes = {
					-- Optional, if you keep daily notes in a separate directory.
					folder = get_daily_notes_folder(),
					-- Optional, if you want to change the date format for the ID of daily notes.
					date_format = "%Y-%m-%d-%A",
					-- Optional, if you want to change the date format of the default alias of daily notes.
					-- alias_format = "%B %-d, %Y",
					-- Optional, default tags to add to each new daily note created.
					default_tags = { "daily-notes" },
					-- Optional, if you want to automatically insert a template from your template directory like 'daily.md'
					template = "template_daily_note.md",
				},
				-- Optional, for templates (see below).
				templates = {
					folder = "templates",
					date_format = "%Y-%m-%d",
					time_format = "%H:%M",
					-- A map for custom variables, the key should be the variable and the value a function
					substitutions = {},
				},
				-- Optional, sort search results by "path", "modified", "accessed", or "created".
				-- The recommend value is "modified" and `true` for `sort_reversed`, which means, for example,
				-- that `:ObsidianQuickSwitch` will show the notes sorted by latest modified time
				sort_by = "modified",
				sort_reversed = true,

				-- Optional, configure additional syntax highlighting / extmarks.
				-- This requires you have `conceallevel` set to 1 or 2. See `:help conceallevel` for more details.
				ui = {
					enable = true, -- set to false to disable all additional syntax features
					update_debounce = 200, -- update delay after a text change (in milliseconds)
					max_file_length = 5000, -- disable UI features for files with more than this many lines
					-- Define how various check-boxes are displayed
					checkboxes = {
						-- NOTE: the 'char' value has to be a single character, and the highlight groups are defined below.
						[" "] = { char = "󰄱", hl_group = "ObsidianTodo" },
						["x"] = { char = "", hl_group = "ObsidianDone" },
						[">"] = { char = "", hl_group = "ObsidianRightArrow" },
						["~"] = { char = "󰰱", hl_group = "ObsidianTilde" },
						["!"] = { char = "", hl_group = "ObsidianImportant" },
						-- Replace the above with this if you don't have a patched font:
						-- [" "] = { char = "☐", hl_group = "ObsidianTodo" },
						-- ["x"] = { char = "✔", hl_group = "ObsidianDone" },

						-- You can also add more custom ones...
					},
					-- Use bullet marks for non-checkbox lists.
					bullets = { char = "•", hl_group = "ObsidianBullet" },
					external_link_icon = { char = "", hl_group = "ObsidianExtLinkIcon" },
					-- Replace the above with this if you don't have a patched font:
					-- external_link_icon = { char = "", hl_group = "ObsidianExtLinkIcon" },
					reference_text = { hl_group = "ObsidianRefText" },
					highlight_text = { hl_group = "ObsidianHighlightText" },
					tags = { hl_group = "ObsidianTag" },
					block_ids = { hl_group = "ObsidianBlockID" },
					hl_groups = {
						-- The options are passed directly to `vim.api.nvim_set_hl()`. See `:help nvim_set_hl`.
						ObsidianTodo = { bold = true, fg = "#f78c6c" },
						ObsidianDone = { bold = true, fg = "#89ddff" },
						ObsidianRightArrow = { bold = true, fg = "#f78c6c" },
						ObsidianTilde = { bold = true, fg = "#ff5370" },
						ObsidianImportant = { bold = true, fg = "#d73128" },
						ObsidianBullet = { bold = true, fg = "#89ddff" },
						ObsidianRefText = { underline = true, fg = "#c792ea" },
						ObsidianExtLinkIcon = { fg = "#c792ea" },
						ObsidianTag = { italic = true, fg = "#89ddff" },
						ObsidianBlockID = { italic = true, fg = "#89ddff" },
						ObsidianHighlightText = { bg = "#75662e" },
					},
				},
			})
		else
			-- Provide a fallback message when no workspace is found
			print("No valid Obsidian workspace found. Obsidian.nvim is not fully configured.")
		end
	end,

	keys = {
		{ "<leader>oa", "<cmd>ObsidianOpen<cr>", desc = "Open in Obsidian App" },
		{ "<leader>on", "<cmd>ObsidianNew<cr>", desc = "Obsidian New" },
		{ "<leader>oo", "<cmd>ObsidianQuickSwitch<cr>", desc = "Obsidian Quick Switch" },
		{ "<leader>ol", "<cmd>ObsidianFollowLink<cr>", desc = "Obsidian Folow Link" },
		{ "<leader>og", "<cmd>ObsidianTags<cr>", desc = "Obsidian Tags" },
		{ "<leader>ot", "<cmd>ObsidianToday<cr>", desc = "Obsidian Today" },
		{ "<leader>of", "<cmd>ObsidianSearch<cr>", desc = "Obsidian Search Word" },
		{ "<leader>ow", "<cmd>ObsidianWorkspace<cr>", desc = "Obsidian Workspace" },
	},
}

it prints the vault path

C:\Users\azin\OneDrive - W Inc\Obsidian\Obsidian_work
Press ENTER or type command to continue


but get the error here:

Failed to run `config` for obsidian.nvim

...Local/nvim-data/lazy/obsidian.nvim/lua/obsidian/path.lua:486: FileNotFoundError: C:/Users/azin/OneDrive - W Inc/Obsidian/Obsidian_work

# stacktrace:
  - obsidian.nvim\lua\obsidian\path.lua:486 _in_ **mkdir**
  - obsidian.nvim\lua\obsidian\path.lua:492 _in_ **mkdir**
  - obsidian.nvim\lua\obsidian\client.lua:121 _in_ **set_workspace**
  - obsidian.nvim\lua\obsidian\client.lua:98 _in_ **new**
  - obsidian.nvim\lua\obsidian\init.lua:95 _in_ **setup**
  - ~/.config/nvim/lua/plugins/obsidian.lua:84 _in_ **config**
  - ~\.config\nvim/lua/config/lazy.lua:14
  - ~\.config\nvim\init.lua:2
Press ENTER or type command to continue

Environment

❯ nvim --version

NVIM v0.11.0-dev-1092+g0da4d89558
Build type: RelWithDebInfo
LuaJIT 2.1.1727870382
Run "nvim -V1 -v" for more info
❯ nvim --headless -c 'lua require("obsidian").info()' -c q

C:\Users\azin\OneDrive - W Inc\Obsidian\Obsidian_work
C:\Users\azin\OneDrive - W Inc\Obsidian\Obsidian_work
ERROR: it appears obsidian.nvim has not been setup.
Please ensure obsidian.nvim loads upfront (e.g. by setting 'lazy=false' with your plugin manager) and then run this again.
Error detected while processing command line:
Failed to run `config` for obsidian.nvim

...Local/nvim-data/lazy/obsidian.nvim/lua/obsidian/path.lua:486: FileNotFoundError: C:/Users/azin/OneDrive - W Inc/Obsidian/Obsidian_work

# stacktrace:
  - obsidian.nvim\lua\obsidian\path.lua:486 _in_ **mkdir**
  - obsidian.nvim\lua\obsidian\path.lua:492 _in_ **mkdir**
  - obsidian.nvim\lua\obsidian\client.lua:121 _in_ **set_workspace**
  - obsidian.nvim\lua\obsidian\client.lua:98 _in_ **new**
  - obsidian.nvim\lua\obsidian\init.lua:95 _in_ **setup**
  - ~/.config/nvim/lua/plugins/obsidian.lua:84 _in_ **config**
  - ~\.config\nvim/lua/config/lazy.lua:14
  - ~\.config\nvim\init.lua:2

installed Obsidian.nvim:

    ● obsidian.nvim 40.36ms  start
        dir     C:/Users/azin/AppData/Local/nvim-data/lazy/obsidian.nvim
        url     https://github.com/epwalsh/obsidian.nvim
        version 3.9.0
        tag     v3.9.0
        branch  main
        commit  ae1f76a
        readme  README.md
        help    |obsidian.nvim|
        help    |obsidian-api|
        ft       markdown 
        keys     <leader>of  <leader>ow  <leader>oa  <leader>on  <leader>oo  <leader>ol  <leader>og  <leader>ot 

@azinsharaf azinsharaf added the bug Something isn't working label Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant