Skip to content

Commit 993f077

Browse files
authored
fix: weird tangle file path logic (#1604)
1 parent c7ada78 commit 993f077

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lua/neorg/modules/core/dirman/utils/module.lua

+3-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ local module = neorg.modules.create("core.dirman.utils")
1919
module.public = {
2020
---Resolve `$<workspace>/path/to/file` and return the real path
2121
---@param path string | PathlibPath # path
22-
---@param raw_path boolean? # If true, returns resolved path, otherwise, returns resolved path
23-
---and append ".norg"
24-
---@param host_file string | PathlibPath | nil file the link resides in, if the link is
25-
---relative, this file is used instead of the current file
26-
---@return PathlibPath?, boolean? # Resolved path. If path does not start with `$` or not absolute, adds
27-
---relative from current file.
22+
---@param raw_path boolean? # If true, returns resolved path, otherwise, returns resolved path and append ".norg"
23+
---@param host_file string | PathlibPath | nil file the link resides in, if the link is relative, this file is used instead of the current file
24+
---@return PathlibPath?, boolean? # Resolved path. If path does not start with `$` or not absolute, adds relative from current file.
2825
expand_pathlib = function(path, raw_path, host_file)
2926
local relative = false
3027
if not host_file then

lua/neorg/modules/core/tangle/module.lua

+22-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The `tangle` module currently provides a single command:
1313
- `:Neorg tangle current-file` - performs all possible tangling operations on the current file
1414
1515
### Usage Tutorial
16+
1617
By default, *zero* code blocks are tangled. You must provide where you'd like to tangle each code
1718
block manually (global configuration will be discussed later). To do so, add a `#tangle
1819
<output-file>` tag above the code block you'd wish to export, where <output-file> is relative to the
@@ -26,6 +27,10 @@ print("Hello World!")
2627
```
2728
The above snippet will *only* tangle that single code block to the desired output file: `init.lua`.
2829
30+
> [!WARNING]
31+
> Due to a bug in the norg treesitter parser, `#tangle ./init.lua` or `#tangle folder/init.lua` will not work
32+
> As a result, we recommend specifying files destinations in metadata
33+
2934
#### Global Tangling for Single Files
3035
Apart from tangling a single or a set of code blocks, you can declare a global output file in the document's metadata:
3136
```norg
@@ -51,6 +56,17 @@ tangle: [
5156
The above snippet tells the Neorg tangling engine to tangle all `lua` code blocks to `./init.lua` and all `haskell` code blocks to `./output.hs`.
5257
As always if any of the code blocks have a `#tangle` tag then that takes precedence.
5358
59+
If you want to be more verbose, or you're tangling to a file without an extension (perhaps you're
60+
writing a shell script that has a shebang) you can also do this:
61+
```norg
62+
@document.meta
63+
tangle: {
64+
lua: ./init.lua
65+
python: ./output
66+
}
67+
@end
68+
```
69+
5470
#### Ignoring Code Blocks
5571
Sometimes when tangling you may want to omit some code blocks. For this you may use the `#tangle.none` tag:
5672
```norg
@@ -166,17 +182,21 @@ local lib, modules, utils, log = neorg.lib, neorg.modules, neorg.utils, neorg.lo
166182

167183
local module = modules.create("core.tangle")
168184
local Path = require("pathlib")
185+
---@type core.dirman.utils
186+
local dirman_utils
169187

170188
module.setup = function()
171189
return {
172190
requires = {
173191
"core.integrations.treesitter",
192+
"core.dirman.utils",
174193
"core.neorgcmd",
175194
},
176195
}
177196
end
178197

179198
module.load = function()
199+
dirman_utils = module.required["core.dirman.utils"]
180200
modules.await("core.neorgcmd", function(neorgcmd)
181201
neorgcmd.add_commands_from_table({
182202
tangle = {
@@ -348,7 +368,7 @@ module.public = {
348368
local path_lib_path = Path.new(file_to_tangle_to)
349369
if path_lib_path:is_relative() then
350370
local buf_path = Path.new(buf_name)
351-
file_to_tangle_to = tostring(buf_path:parent():child(file_to_tangle_to):resolve())
371+
file_to_tangle_to = tostring(dirman_utils.expand_pathlib(file_to_tangle_to, true, buf_path):resolve())
352372
end
353373

354374
local delimiter_content
@@ -477,14 +497,7 @@ module.on_event = function(event)
477497
local tangled_count = 0
478498

479499
for file, content in pairs(tangles) do
480-
-- resolve upward relative path like `../../`
481-
local relative_file, upward_count = string.gsub(file, "%.%.[\\/]", "")
482-
if upward_count > 0 then
483-
local base_dir = vim.fn.expand("%:p" .. string.rep(":h", upward_count + 1)) --[[@as string]]
484-
file = vim.fs.joinpath(base_dir, relative_file)
485-
end
486-
487-
vim.loop.fs_open(vim.fn.expand(file) --[[@as string]], "w", 438, function(err, fd)
500+
vim.loop.fs_open(file, "w", 438, function(err, fd)
488501
assert(not err and fd, lib.lazy_string_concat("Failed to open file '", file, "' for tangling: ", err))
489502

490503
local write_content = table.concat(content, "\n")

0 commit comments

Comments
 (0)