@@ -13,6 +13,7 @@ The `tangle` module currently provides a single command:
13
13
- `:Neorg tangle current-file` - performs all possible tangling operations on the current file
14
14
15
15
### Usage Tutorial
16
+
16
17
By default, *zero* code blocks are tangled. You must provide where you'd like to tangle each code
17
18
block manually (global configuration will be discussed later). To do so, add a `#tangle
18
19
<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!")
26
27
```
27
28
The above snippet will *only* tangle that single code block to the desired output file: `init.lua`.
28
29
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
+
29
34
#### Global Tangling for Single Files
30
35
Apart from tangling a single or a set of code blocks, you can declare a global output file in the document's metadata:
31
36
```norg
@@ -51,6 +56,17 @@ tangle: [
51
56
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`.
52
57
As always if any of the code blocks have a `#tangle` tag then that takes precedence.
53
58
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
+
54
70
#### Ignoring Code Blocks
55
71
Sometimes when tangling you may want to omit some code blocks. For this you may use the `#tangle.none` tag:
56
72
```norg
@@ -166,17 +182,21 @@ local lib, modules, utils, log = neorg.lib, neorg.modules, neorg.utils, neorg.lo
166
182
167
183
local module = modules .create (" core.tangle" )
168
184
local Path = require (" pathlib" )
185
+ --- @type core.dirman.utils
186
+ local dirman_utils
169
187
170
188
module .setup = function ()
171
189
return {
172
190
requires = {
173
191
" core.integrations.treesitter" ,
192
+ " core.dirman.utils" ,
174
193
" core.neorgcmd" ,
175
194
},
176
195
}
177
196
end
178
197
179
198
module .load = function ()
199
+ dirman_utils = module .required [" core.dirman.utils" ]
180
200
modules .await (" core.neorgcmd" , function (neorgcmd )
181
201
neorgcmd .add_commands_from_table ({
182
202
tangle = {
@@ -348,7 +368,7 @@ module.public = {
348
368
local path_lib_path = Path .new (file_to_tangle_to )
349
369
if path_lib_path :is_relative () then
350
370
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 ())
352
372
end
353
373
354
374
local delimiter_content
@@ -477,14 +497,7 @@ module.on_event = function(event)
477
497
local tangled_count = 0
478
498
479
499
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 )
488
501
assert (not err and fd , lib .lazy_string_concat (" Failed to open file '" , file , " ' for tangling: " , err ))
489
502
490
503
local write_content = table.concat (content , " \n " )
0 commit comments