epwalsh / obsidian.nvim

Obsidian 🀝 Neovim
Apache License 2.0
3.3k stars 150 forks source link

If a markdown is in a git repository, this plugin conflicts with git DiffView #576

Closed sadtab closed 1 month ago

sadtab commented 1 month ago

πŸ› Describe the bug

UPDATE:

I noticed any markdown in a given repository will cause a conflict between Obsidian and DiffView,

How to reproduce: Have DiffView.nvim and obsidian.nvim installed modify any given md file in any given git repository and launch the DiffView for the modified md file

Original bug report

This is my vault structure

~/vaults/
β”œβ”€β”€ .git
β”œβ”€β”€ dev
β”œβ”€β”€ job
└── vie

Where each folder : job, dev and vie are a obsidian vault :

        workspaces = {
            -- Default vault 
            {
                name = "dev",
                path = "~/vaults/dev",
            },
            -- Other vaults 
            {
                name = "vie",
                path = "~/vaults/vie",
            },
            {
                name = "job",
                path = "~/vaults/job",
            },

When something is changed in the repo, if I open the plugin with diffView I got these errors :

...re/nvim/lazy/diffview.nvim/lua/diffview/scene/window.lua:150: BufEnter Autocommands for "*.md": Vim(append):Error executing lua callback: ...ocal/share/nvim/lazy/obsidian.nvim/lua/obsidian/path.lua:402: FileNotFoundError: diffview:/home/stabei/vaults/.git/:0:/vie
stack traceback:
    [C]: in function 'error'
    ...ocal/share/nvim/lazy/obsidian.nvim/lua/obsidian/path.lua:402: in function 'resolve'
    ...share/nvim/lazy/obsidian.nvim/lua/obsidian/workspace.lua:159: in function 'get_workspace_for_dir'
    ...ocal/share/nvim/lazy/obsidian.nvim/lua/obsidian/init.lua:125: in function <...ocal/share/nvim/lazy/obsidian.nvim/lua/obsidian/init.lua:117>

It seems this function in the path.lua

---@return obsidian.Path
Path.resolve = function(self, opts)
  opts = opts or {}

  local realpath = self:fs_realpath()
  if realpath then
    return Path.new(realpath)
  elseif opts.strict then
    error("FileNotFoundError: " .. self.filename)
  end

  -- File doesn't exist, but some parents might. Traverse up until we find a parent that
  -- does exist, and then put the path back together from there.
  local parents = self:parents()
  for _, parent in ipairs(parents) do
    local parent_realpath = parent:fs_realpath()
    if parent_realpath then
      return Path.new(parent_realpath) / self:relative_to(parent)
    end
  end

  return self
end

is trying to find the vault under .git folder

Config

return {
    'epwalsh/obsidian.nvim',
    lazy = false,
    dependencies = {
        "hrsh7th/nvim-cmp",
        "preservim/vim-markdown",
    },
    opts = {
        workspaces = {
            -- Default vault 
            {
                name = "dev",
                path = "~/vaults/dev",
            },
            -- Other vault 
            {
                name = "vie",
                path = "~/vaults/vie",
            },
            {
                name = "job",
                path = "~/vaults/job",
            },
        },

        -- Optional, completion.
        completion = {
            nvim_cmp = true, -- if using nvim-cmp, otherwise set to false
        },

        wiki_link_func = "use_alias_only",
        preferred_link_style = "wiki",

        -- Optional, customize how names/IDs for new notes are created.
        note_id_func = function(title)
            -- Create note IDs in a Zettelkasten format with a timestamp and a suffix.
            -- In this case a note with the title 'My new note' will given an ID that looks
            -- like '1657296016-my-new-note', and therefore the file name '1657296016-my-new-note.md'
            local suffix = ""
            if title ~= nil then
                -- If title is given, transform it into valid file name
                -- replace spaces with hyphens and all lower case.
                suffix = title:gsub(" ", "-"):gsub("[^A-Za-z0-9-]", ""):lower()
            else
                -- If title is nil, just add 4 random uppercase letters to the suffix.
                for _ = 1, 4 do
                    suffix = suffix .. string.char(math.random(65, 90))
                end
                suffix = tostring(os.time()) .. "-" .. suffix
            end
            return suffix
        end,

        -- Optional, by default when you use `:ObsidianFollowLink` on a link to an external
        follow_url_func = function(url)
            vim.fn.jobstart({ "xdg-open", url }) -- linux
        end,

        -- Optional, set to true to force ':ObsidianOpen' to bring the app to the foreground.
        open_app_foreground = true,

        -- Optional, set to true if you don't want Obsidian to manage frontmatter.
        disable_frontmatter = false,

        note_frontmatter_func = function(note)
            -- Add a custom property as date
            local currentDate = os.date("%Y/%m/%d %I:%M %p")

            local out = {
                tags = note.tags,
                created = currentDate,
            }

            -- `note.metadata` contains any manually added fields in the frontmatter.
            -- So here we just make sure those fields are kept in the frontmatter.
            if note.metadata ~= nil and not vim.tbl_isempty(note.metadata) then
                for k, v in pairs(note.metadata) do
                    out[k] = v
                end
            end
            return out
        end,

        -- templates = {
        --     subdir = "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 = {},
        -- },
        -- Mapping to be available in a obsidian note file
        mappings = {
            -- Overrides the 'gf' mapping to work on markdown/wiki links within your vault.
            ["gf"] = {
                action = function()
                    return require("obsidian").util.gf_passthrough()
                end,
                opts = { noremap = false, expr = true, buffer = true },
            },
            -- Toggle check-boxes.
            ["<space>oc"] = {
                action = function()
                    return require("obsidian").util.toggle_checkbox()
                end,
                opts = { buffer = true, silent = true, desc = "Toggle Checkbox" },
            },
        }

    }
}

Environment

❯ nvim --version
NVIM v0.9.5
Build type: Debug
LuaJIT 2.1.1692716794
Compilation: /usr/bin/cc -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wvla -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -fno-common -Wimplicit-fallthrough -fdiagnostics-color=always -fstack-protector-strong -DNVIM_LOG_DEBUG -DUNIT_TESTING -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -I/home/stabei/.local/neovim/.deps/usr/include/luajit-2.1 -I/usr/include -I/home/stabei/.local/neovim/.deps/usr/include -I/home/stabei/.local/neovim/build/src/nvim/auto -I/home/stabei/.local/neovim/build/include -I/home/stabei/.local/neovim/build/cmake.config -I/home/stabei/.local/neovim/src -I/usr/include -I/home/stabei/.local/neovim/.deps/usr/include -I/home/stabei/.local/neovim/.deps/usr/include -I/home/stabei/.local/neovim/.deps/usr/include -I/home/stabei/.local/neovim/.deps/usr/include -I/home/stabei/.local/neovim/.deps/usr/include -I/home/stabei/.local/neovim/.deps/usr/include

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info

❯ nvim --headless -c 'lua require("obsidian").info()' -c q

Obsidian.nvim v3.7.12 (db41b1f20459293436fab510bec58c82a73bd1f7)
Status:
  β€’ buffer directory: nil
  β€’ working directory: /home/stabei
Workspaces:
  βœ“ active workspace: Workspace(name='dev', path='/home/stabei/vaults/dev', root='/home/stabei/vaults/dev')
  βœ— inactive workspace: Workspace(name='vie', path='/home/stabei/vaults/vie', root='/home/stabei/vaults/vie')
  βœ— inactive workspace: Workspace(name='job', path='/home/stabei/vaults/job', root='/home/stabei/vaults/job')
Dependencies:
  βœ“ plenary.nvim: 08e301982b9a057110ede7a735dd1b5285eb341f
  βœ“ nvim-cmp: 8f3c541407e691af6163e2447f3af1bd6e17f9a3
  βœ“ telescope.nvim: fac83a556e7b710dc31433dec727361ca062dbe9
Integrations:
  βœ“ picker: TelescopePicker()
  βœ“ completion: enabled (nvim-cmp) βœ— refs, βœ— tags, βœ— new
    all sources:
      β€’ luasnip
      β€’ nvim_lsp
      β€’ neorg
      β€’ nvim_lua
      β€’ buffer
      β€’ path
      β€’ doxygen
      β€’ spell
Tools:
  βœ“ rg: ripgrep 11.0.2
Environment:
  β€’ operating system: Linux
Config:
  β€’ notes_subdir: nil%
epwalsh commented 1 month ago

Hey @sadtab thanks for all the details. I believe #578 should fix.

sadtab commented 1 month ago

You're awesome @epwalsh, I just pulled it and its alright