@@ -162,7 +162,7 @@ The first code block will be tangled to `./output.lua`, the second code block wi
162
162
--]]
163
163
164
164
local neorg = require (" neorg.core" )
165
- local lib , modules , utils = neorg .lib , neorg .modules , neorg .utils
165
+ local lib , modules , utils , log = neorg .lib , neorg .modules , neorg .utils , neorg . log
166
166
167
167
local module = modules .create (" core.tangle" )
168
168
local Path = require (" pathlib" )
@@ -196,6 +196,16 @@ module.load = function()
196
196
},
197
197
})
198
198
end )
199
+
200
+ if module .config .public .tangle_on_write then
201
+ local augroup = vim .api .nvim_create_augroup (" norg_auto_tangle" , { clear = true })
202
+ vim .api .nvim_create_autocmd (" BufWritePost" , {
203
+ desc = " Tangle the current file on write" ,
204
+ pattern = " *.norg" ,
205
+ group = augroup ,
206
+ command = " Neorg tangle current-file" ,
207
+ })
208
+ end
199
209
end
200
210
201
211
local function get_comment_string (language )
211
221
212
222
module .public = {
213
223
tangle = function (buffer )
224
+ --- @type core.integrations.treesitter
214
225
local treesitter = module .required [" core.integrations.treesitter" ]
215
226
local parsed_document_metadata = treesitter .get_document_metadata (buffer ) or {}
216
227
local tangle_settings = parsed_document_metadata .tangle or {}
@@ -271,7 +282,15 @@ module.public = {
271
282
local capture = query .captures [id ]
272
283
273
284
if capture == " tag" then
274
- local parsed_tag = treesitter .get_tag_info (node )
285
+ local ok , parsed_tag = pcall (treesitter .get_tag_info , node , true )
286
+ if not ok then
287
+ if module .config .public .indent_errors == " print" then
288
+ print (parsed_tag )
289
+ else
290
+ log .error (parsed_tag )
291
+ end
292
+ goto skip_tag
293
+ end
275
294
276
295
if parsed_tag then
277
296
local declared_filetype = parsed_tag .parameters [1 ]
@@ -405,10 +424,9 @@ module.public = {
405
424
vim .list_extend (tangles [file_to_tangle_to ], delimiter_content )
406
425
end
407
426
vim .list_extend (tangles [file_to_tangle_to ], block_content )
408
-
409
- :: skip_tag::
410
427
end
411
428
end
429
+ :: skip_tag::
412
430
end
413
431
414
432
if options .delimiter == " file-content" then
@@ -431,6 +449,16 @@ module.public = {
431
449
module .config .public = {
432
450
-- Notify when there is nothing to tangle (INFO) or when the content is empty (WARN).
433
451
report_on_empty = true ,
452
+
453
+ -- Tangle all code blocks in the current norg file on file write.
454
+ tangle_on_write = false ,
455
+
456
+ -- When text in a code block is less indented than the block itself, Neorg will not tangle that
457
+ -- block to a file. Instead it can either print or vim.notify error. By default, vim.notify is
458
+ -- loud and is more likely to create a press enter message.
459
+ -- - "notify" - Throw a normal looking error
460
+ -- - "print" - print the error
461
+ indent_errors = " notify" ,
434
462
}
435
463
436
464
module .on_event = function (event )
0 commit comments