Skip to content

Commit

Permalink
feat: added file patterns .mdx, .mdoc
Browse files Browse the repository at this point in the history
- added new utility file - obsidian/patterns.lua
- added file extensions for: .mdx, .mdoc
- more file extensions can be added via obsidian/patterns.lua
  • Loading branch information
sirjager committed Jan 3, 2025
1 parent 14e0427 commit 2044864
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
12 changes: 11 additions & 1 deletion lua/obsidian/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions lua/obsidian/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local log = require "obsidian.log"
local patterns = require("obsidian.patterns").file_patterns

local module_lookups = {
abc = "obsidian.abc",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions lua/obsidian/patterns.lua
Original file line number Diff line number Diff line change
@@ -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
13 changes: 4 additions & 9 deletions lua/obsidian/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/obsidian/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2044864

Please sign in to comment.