From 2044864857c06a0a708d9e554fd8b043ab5cec0d Mon Sep 17 00:00:00 2001 From: Ankur Kumar <18713058+sirjager@users.noreply.github.com> Date: Fri, 3 Jan 2025 16:09:33 +0530 Subject: [PATCH] feat: added file patterns .mdx, .mdoc - added new utility file - obsidian/patterns.lua - added file extensions for: .mdx, .mdoc - more file extensions can be added via obsidian/patterns.lua --- lua/obsidian/client.lua | 12 +++++++++++- lua/obsidian/init.lua | 7 ++++--- lua/obsidian/patterns.lua | 12 ++++++++++++ lua/obsidian/search.lua | 13 ++++--------- lua/obsidian/ui.lua | 2 +- 5 files changed, 32 insertions(+), 14 deletions(-) create mode 100644 lua/obsidian/patterns.lua diff --git a/lua/obsidian/client.lua b/lua/obsidian/client.lua index 00c09c77f..9ca735b8e 100644 --- a/lua/obsidian/client.lua +++ b/lua/obsidian/client.lua @@ -189,8 +189,18 @@ end Client.path_is_note = function(self, path, workspace) path = Path.new(path):resolve() + local valid_extensions = require("obsidian.patterns").file_extensions + local is_valid = false + + for _, suffix in ipairs(valid_extensions) do + if path.suffix == suffix then + is_valid = true + break + end + end + -- Notes have to be markdown file. - if path.suffix ~= ".md" then + if not is_valid then return false end diff --git a/lua/obsidian/init.lua b/lua/obsidian/init.lua index b5e139103..11235cc0d 100644 --- a/lua/obsidian/init.lua +++ b/lua/obsidian/init.lua @@ -1,4 +1,5 @@ local log = require "obsidian.log" +local patterns = require("obsidian.patterns").file_patterns local module_lookups = { abc = "obsidian.abc", @@ -113,7 +114,7 @@ obsidian.setup = function(opts) -- Complete setup and update workspace (if needed) when entering a markdown buffer. vim.api.nvim_create_autocmd({ "BufEnter" }, { group = group, - pattern = "*.md", + pattern = patterns, callback = function(ev) -- Set the current directory of the buffer. local buf_dir = vim.fs.dirname(ev.match) @@ -166,7 +167,7 @@ obsidian.setup = function(opts) vim.api.nvim_create_autocmd({ "BufLeave" }, { group = group, - pattern = "*.md", + pattern = patterns, callback = function(ev) -- Check if we're in *any* workspace. local workspace = obsidian.Workspace.get_workspace_for_dir(vim.fs.dirname(ev.match), client.opts.workspaces) @@ -189,7 +190,7 @@ obsidian.setup = function(opts) -- Add/update frontmatter for notes before writing. vim.api.nvim_create_autocmd({ "BufWritePre" }, { group = group, - pattern = "*.md", + pattern = patterns, callback = function(ev) local buf_dir = vim.fs.dirname(ev.match) diff --git a/lua/obsidian/patterns.lua b/lua/obsidian/patterns.lua new file mode 100644 index 000000000..b2c429a08 --- /dev/null +++ b/lua/obsidian/patterns.lua @@ -0,0 +1,12 @@ +local M = {} + +-- used in obsidian/init.lua -> vim.api.nvim_create_autocmd +M.file_patterns = { "*.md", "*.mdx", "*.mdoc" } + +-- used in search.lua -> M.build_find_cmd +M.search_pattern = "*.md,*.mdx,*.mdoc" + +-- used in client.lua -> Client.path_is_note +M.file_extensions = { ".md", ".mdx", ".mdoc" } + +return M diff --git a/lua/obsidian/search.lua b/lua/obsidian/search.lua index 6b981c921..2fca209c9 100644 --- a/lua/obsidian/search.lua +++ b/lua/obsidian/search.lua @@ -392,19 +392,14 @@ end ---@return string[] M.build_find_cmd = function(path, term, opts) opts = SearchOpts.from_tbl(opts and opts or {}) - local additional_opts = {} + local pattern = require("obsidian.patterns").search_pattern + if term ~= nil then - if opts.include_non_markdown then - term = "*" .. term .. "*" - elseif not vim.endswith(term, ".md") then - term = "*" .. term .. "*.md" - else - term = "*" .. term - end + term = "*" .. term .. "*" additional_opts[#additional_opts + 1] = "-g" - additional_opts[#additional_opts + 1] = term + additional_opts[#additional_opts + 1] = pattern end if opts.ignore_case then diff --git a/lua/obsidian/ui.lua b/lua/obsidian/ui.lua index 04364dbf6..c30f15f44 100644 --- a/lua/obsidian/ui.lua +++ b/lua/obsidian/ui.lua @@ -617,7 +617,7 @@ M.setup = function(workspace, ui_opts) install_hl_groups(ui_opts) - local pattern = tostring(workspace.root) .. "/**.md" + local pattern = require("obsidian.patterns").file_patterns vim.api.nvim_create_autocmd({ "BufEnter" }, { group = group,