Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lua/logseq_mode/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local M = {}

M.defaults = {
logseq_dir = vim.fn.expand("~/logseq-graph"),
-- Optional: Used for unified search if you have an obsidian vault too
obsidian_dir = vim.fn.expand("~/main-vault"),
-- Amount of extra space between lines (only works in GUI clients)
Expand Down
4 changes: 2 additions & 2 deletions lua/logseq_mode/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ M.awk_script = [[
}
]]

function M.get_config(logseq_dir)
function M.get_config()
return {
condition = function(self, ctx)
local bufname = vim.api.nvim_buf_get_name(ctx.buf)
return bufname:find(logseq_dir, 1, true) ~= nil
return require("logseq_mode.utils").get_logseq_root(bufname) ~= nil
end,
command = "awk",
args = { M.awk_script },
Expand Down
30 changes: 16 additions & 14 deletions lua/logseq_mode/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Config = require("logseq_mode.config")
local Formatter = require("logseq_mode.formatter")
local Utils = require("logseq_mode.utils")

local M = {}

Expand Down Expand Up @@ -52,7 +53,18 @@ end

function M.daily_note()
local date = os.date("%Y_%m_%d")
local path = Config.options.logseq_dir .. "/journals/" .. date .. ".md"
local root = Config.options.logseq_dir

if not root or root == "" then
root = Utils.get_logseq_root(vim.api.nvim_buf_get_name(0))
end

if not root then
vim.notify("Logseq directory not configured and not in a Logseq project", vim.log.levels.ERROR)
return
end

local path = root .. "/journals/" .. date .. ".md"
vim.cmd("edit " .. path)
end

Expand Down Expand Up @@ -87,7 +99,7 @@ function M.setup(opts)
-- Register Formatter if Conform is loaded
local has_conform, conform = pcall(require, "conform")
if has_conform then
conform.formatters.logseq_fixer = Formatter.get_config(Config.options.logseq_dir)
conform.formatters.logseq_fixer = Formatter.get_config()
end

-- FileType Autocommand
Expand All @@ -100,13 +112,8 @@ function M.setup(opts)
end

local bufname = ev.file
local logseq_dir = Config.options.logseq_dir

if not logseq_dir or type(logseq_dir) ~= "string" or not bufname or bufname == "" then
return
end

if bufname:find(logseq_dir, 1, true) then
if Utils.get_logseq_root(bufname) then
-- Set local options
vim.opt_local.foldmethod = "indent"
vim.opt_local.shiftwidth = 0 -- Use tabstop
Expand Down Expand Up @@ -180,14 +187,9 @@ function M.setup(opts)
end

local bufname = ev.file
local logseq_dir = Config.options.logseq_dir

if not logseq_dir or type(logseq_dir) ~= "string" or not bufname or bufname == "" then
return
end

-- Check if current buffer is in logseq dir
if bufname:find(logseq_dir, 1, true) then
if Utils.get_logseq_root(bufname) then
if Config.options.linespace and Config.options.linespace > 0 then
vim.opt.linespace = Config.options.linespace
end
Expand Down
33 changes: 33 additions & 0 deletions lua/logseq_mode/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local Config = require("logseq_mode.config")

local M = {}

---Returns the root directory of the Logseq graph for the given buffer path, or nil.
---@param bufpath string
---@return string|nil
function M.get_logseq_root(bufpath)
if not bufpath or bufpath == "" then
return nil
end

-- 1. Check configured logseq_dir
if Config.options.logseq_dir and Config.options.logseq_dir ~= "" then
-- Use plain string search to avoid pattern issues with paths
if bufpath:find(Config.options.logseq_dir, 1, true) then
return Config.options.logseq_dir
end
end

-- 2. Check for .logseqfolder marker
-- vim.fs.root returns the path to the directory containing the marker
if vim.fs and vim.fs.root then
local root = vim.fs.root(bufpath, ".logseqfolder")
if root then
return root
end
end

return nil
end

return M
56 changes: 56 additions & 0 deletions tests/test_detection.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
-- Minimal mock framework for testing logic without full Neovim
_G.vim = {
fn = {
expand = function(path) return path:gsub("~", "/home/user") end
},
fs = {
root = function(path, marker)
-- Simple mock: check if path starts with a known mock root containing the marker
if marker == ".logseqfolder" then
if path:find("/home/user/graph_with_marker") then
return "/home/user/graph_with_marker"
end
end
return nil
end
}
}

-- Adjust package path to find the lua module from repo root
package.path = "lua/?.lua;" .. package.path

local Utils = require("logseq_mode.utils")
local Config = require("logseq_mode.config")

-- Helper to run test
local function assert_eq(a, b, msg)
if a ~= b then
error("FAIL: " .. msg .. " | Expected: " .. tostring(b) .. ", Got: " .. tostring(a))
end
end

print("Running detection tests...")

-- Test 1: Configured logseq_dir
print("Test 1: Configured logseq_dir")
Config.options.logseq_dir = "/home/user/my_graph"
local res1 = Utils.get_logseq_root("/home/user/my_graph/pages/Page.md")
assert_eq(res1, "/home/user/my_graph", "Should match configured dir")

-- Test 2: Configured logseq_dir (mismatch)
print("Test 2: Configured logseq_dir mismatch")
local res2 = Utils.get_logseq_root("/home/user/other/file.md")
assert_eq(res2, nil, "Should return nil for outside file")

-- Test 3: Marker file (logseq_dir nil)
print("Test 3: Marker file detection")
Config.options.logseq_dir = nil
local res3 = Utils.get_logseq_root("/home/user/graph_with_marker/pages/A.md")
assert_eq(res3, "/home/user/graph_with_marker", "Should detect marker")

-- Test 4: No config, no marker
print("Test 4: No config, no marker")
local res4 = Utils.get_logseq_root("/home/user/random/file.md")
assert_eq(res4, nil, "Should return nil")

print("All tests passed!")