Skip to content

Commit

Permalink
feat(snippets): add TODAY_OF_FILETREE to parse *nested* journal fil…
Browse files Browse the repository at this point in the history
…es (#13)
  • Loading branch information
pysan3 committed Aug 15, 2023
1 parent e8a8541 commit 09609b5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ vim.api.nvim_create_autocmd("BufNewFile", {
```


### Builtin Snippets

I will not list all builtin snippets but some useful ones that however might need a bit of explanation.
Please see [`default_snippets.lua`](./lua/neorg/modules/external/templates/default_snippets.lua) for the whole list.


#### `TITLE`, `TITLE_INPUT`

Inserts the file name. `TITLE_INPUT` would be an insert node with default text being `TITLE`.


#### `URL_TAG`

When you insert a URL, based on the domain, this snippet adds a `#tag` in front of the URL.

`https://youtu.be/NnmRVY22Lq8` -> `#YouTube {https://youtu.be/NnmRVY22Lq8}`


#### `TODAY`, `YESTERDAY`, `TOMORROW` + `_OF_FILENAME`, `_OF_FILETREE`

`TODAY_OF_FILENAME` will insert the date by parsing the filename.
`OF_FILENAME` is useful when `journal.strategy == "flat"`.
So, if the filename is `2023-01-01.norg`, `YESTERDAY_OF_FILENAME => 2022-12-31`.

Similarly, `TODAY_OF_FILETREE` is useful when `journal.strategy == "nested"`.
Ex: if the filename is `.../2023/01/01.norg`, `YESTERDAY_OF_FILETREE => 2022-12-31`.


## Useful Templates

- [pysan3](https://github.com/pysan3)
Expand Down
20 changes: 20 additions & 0 deletions README.norg
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ version: 1.0.0
})
@end

*** Builtin Snippets
I will not list all builtin snippets but some useful ones that however might need a bit of explanation.
Please see {./lua/neorg/modules/external/templates/default_snippets.lua}[`default_snippets.lua`] for the whole list.

**** `TITLE`, `TITLE_INPUT`
Inserts the file name. `TITLE_INPUT` would be an insert node with default text being `TITLE`.

**** `URL_TAG`
When you insert a URL, based on the domain, this snippet adds a `#tag` in front of the URL.

`https://youtu.be/NnmRVY22Lq8` -> `#YouTube {https://youtu.be/NnmRVY22Lq8}`

**** `TODAY`, `YESTERDAY`, `TOMORROW` + `_OF_FILENAME`, `_OF_FILETREE`
`TODAY_OF_FILENAME` will insert the date by parsing the filename.
`OF_FILENAME` is useful when `journal.strategy == "flat"`.
So, if the filename is `2023-01-01.norg`, `YESTERDAY_OF_FILENAME => 2022-12-31`.

Similarly, `TODAY_OF_FILETREE` is useful when `journal.strategy == "nested"`.
Ex: if the filename is `.../2023/01/01.norg`, `YESTERDAY_OF_FILETREE => 2022-12-31`.

** Useful Templates
- {https://github.com/pysan3}[pysan3]
-- {https://github.com/pysan3/dotfiles/blob/main/nvim/lua/plugins/60-neorg.lua}[Plugin Setup]
Expand Down
65 changes: 56 additions & 9 deletions lua/neorg/modules/external/templates/default_snippets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,42 @@ local M = {
},
}

M.parse_date = function(delta_date, str)
local year, month, day = string.match(str, [[^(%d%d%d%d)-(%d%d)-(%d%d)$]])
M.parse_date = function(delta_date, str_or_date)
if type(str_or_date) ~= "string" then
str_or_date = os.date(M.date_format, str_or_date)
end
local year, month, day = string.match(str_or_date, [[^(%d%d%d%d)-(%d%d)-(%d%d)$]])
return os.date(M.date_format, os.time({ year = year, month = month, day = day }) + 86400 * delta_date)
end

M.current_date_f = function(delta_day)
return function()
return os.date(M.date_format, os.time() + 86400 * delta_day)
end
M.current_date = function(delta_day)
return os.date(M.date_format, os.time() + 86400 * delta_day)
end

M.file_title = function()
return vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":t:r")
end

M.ostime = function(year, month, day)
if year == nil or month == nil or day == nil then
return os.time()
end
return os.time({ year = year, month = month, day = day })
end

M.file_tree_date = function()
local f_name = vim.api.nvim_buf_get_name(0)
local grandparent = vim.fn.fnamemodify(f_name, ":p:h:h:h") or ""
local date_path = vim.fn.fnamemodify(f_name:sub(grandparent:len() + 2):gsub([[\]], "/"), ":r") or ""
local year, month, day = string.match(date_path, [[^(%d%d%d%d)/(%d%d)/(%d%d)$]])
return M.ostime(year, month, day)
end

M.file_name_date = function()
local year, month, day = string.match(M.file_title() or "", [[^(%d%d%d%d)-(%d%d)-(%d%d)$]])
return M.ostime(year, month, day)
end

---Parse url and return service name based on domain
---@param link string # url in shape of http(s)://domain.name/xxx
---@return string # Name of service
Expand All @@ -88,9 +109,6 @@ M.default_keywords = {
INSERT = function()
return i(1)
end,
TODAY = f(M.current_date_f(0)),
TOMORROW = f(M.current_date_f(1)),
YESTERDAY = f(M.current_date_f(-1)),
WEATHER = c(1, { t("Sunny "), t("Cloudy "), t("Rainy ") }),
AUTHOR = f(utils.get_username),
URL_TAG = fmt([[#{url_type} {{{url}}}]], {
Expand All @@ -99,6 +117,35 @@ M.default_keywords = {
return M.link_type(args[1][1]):lower()
end, { 1 }),
}),
YESTERDAY = function()
return t(M.current_date(-1))
end,
TODAY = function()
return t(M.current_date(0))
end,
TOMORROW = function()
return t(M.current_date(1))
end,
-- When journal.strategy == "flat"
YESTERDAY_OF_FILENAME = function()
return t(M.parse_date(-1, M.file_name_date()))
end,
TODAY_OF_FILENAME = function()
return t(M.parse_date(0, M.file_name_date()))
end,
TOMORROW_OF_FILENAME = function()
return t(M.parse_date(1, M.file_name_date()))
end,
-- When journal.strategy == "nested"
YESTERDAY_OF_FILETREE = function()
return t(M.parse_date(-1, M.file_tree_date()))
end,
TODAY_OF_FILETREE = function()
return t(M.parse_date(0, M.file_tree_date()))
end,
TOMORROW_OF_FILETREE = function()
return t(M.parse_date(1, M.file_tree_date()))
end,
}

return M

0 comments on commit 09609b5

Please sign in to comment.