mdvault_core/scripting/mod.rs
1//! Lua scripting support for mdvault.
2//!
3//! This module provides a sandboxed Lua environment with access to
4//! mdvault's date math and template rendering engines.
5//!
6//! # Overview
7//!
8//! The scripting layer allows users to define custom validation logic,
9//! type definitions, and automation rules in Lua while having access
10//! to mdvault's core functionality.
11//!
12//! # Example
13//!
14//! ```rust
15//! use mdvault_core::scripting::LuaEngine;
16//!
17//! let engine = LuaEngine::sandboxed().unwrap();
18//!
19//! // Use date math
20//! let date = engine.eval_string(r#"mdv.date("today + 7d")"#).unwrap();
21//! println!("One week from now: {}", date);
22//!
23//! // Render templates
24//! let greeting = engine.eval_string(
25//! r#"mdv.render("Hello {{name}}!", { name = "World" })"#
26//! ).unwrap();
27//! println!("{}", greeting);
28//! ```
29//!
30//! # Available Lua Functions
31//!
32//! The `mdv` global table provides:
33//!
34//! - `mdv.date(expr, format?)` - Evaluate date math expressions
35//! - `mdv.render(template, context)` - Render templates with variables
36//! - `mdv.is_date_expr(str)` - Check if a string is a date expression
37//!
38//! With vault context (via `LuaEngine::with_vault_context`):
39//! - `mdv.template(name, vars?)` - Render a template by name
40//! - `mdv.capture(name, vars?)` - Execute a capture workflow
41//! - `mdv.macro(name, vars?)` - Execute a macro workflow
42//! - `mdv.read_note(path)` - Read a note's content and frontmatter
43//! - `mdv.current_note()` - Get the current note being processed
44//! - `mdv.backlinks(path)` - Get notes linking to a path
45//! - `mdv.outlinks(path)` - Get notes a path links to
46//! - `mdv.query(opts)` - Query the vault index
47//! - `mdv.selector(opts)` - Show interactive fuzzy selector for notes of a type
48//!
49//! # Security
50//!
51//! By default, the Lua environment is sandboxed to prevent:
52//! - File system access (`io` library removed)
53//! - Shell command execution (`os` library removed)
54//! - Loading external modules (`require` removed)
55//! - Arbitrary code loading (`load`, `loadfile`, `dofile` removed)
56//! - Debug library access (`debug` removed)
57
58pub mod bindings;
59pub mod engine;
60pub mod hook_runner;
61pub mod hooks;
62pub mod index_bindings;
63pub mod selector;
64pub mod types;
65pub mod vault_bindings;
66pub mod vault_context;
67
68pub use engine::LuaEngine;
69pub use hook_runner::{
70 HookResult, UpdateHookResult, run_on_create_hook, run_on_update_hook,
71};
72pub use hooks::{HookError, NoteContext};
73pub use selector::{SelectorCallback, SelectorItem, SelectorOptions};
74pub use types::{SandboxConfig, ScriptingError};
75pub use vault_context::{CurrentNote, VaultContext};