mdvault-core 0.7.2

Core library for mdvault - markdown vault management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Index query bindings for Lua.
//!
//! This module provides Lua bindings for querying the vault index:
//! - `mdv.current_note()` - Get the current note being processed
//! - `mdv.backlinks(path)` - Get notes linking to a path
//! - `mdv.outlinks(path)` - Get notes a path links to
//! - `mdv.query(opts)` - Query the vault index

use std::path::Path;

use mlua::{Function, Lua, Result as LuaResult, Table, Value};

use super::vault_context::VaultContext;
use crate::index::NoteQuery;
use crate::types::validation::yaml_to_lua_table;

/// Register index query bindings on an existing mdv table.
///
/// This adds `mdv.current_note()`, `mdv.backlinks()`, `mdv.outlinks()`, and
/// `mdv.query()` functions that have access to the vault index.
pub fn register_index_bindings(lua: &Lua) -> LuaResult<()> {
    let mdv: Table = lua.globals().get("mdv")?;

    mdv.set("current_note", create_current_note_fn(lua)?)?;
    mdv.set("backlinks", create_backlinks_fn(lua)?)?;
    mdv.set("outlinks", create_outlinks_fn(lua)?)?;
    mdv.set("query", create_query_fn(lua)?)?;
    mdv.set("find_project", create_find_project_fn(lua)?)?;

    Ok(())
}

/// Create the `mdv.current_note()` function.
///
/// Returns the current note being processed, or nil if not available.
///
/// # Examples (in Lua)
///
/// ```lua
/// local note = mdv.current_note()
/// if note then
///     print("Processing: " .. note.path)
///     print("Type: " .. note.type)
/// end
/// ```
fn create_current_note_fn(lua: &Lua) -> LuaResult<Function> {
    lua.create_function(|lua, ()| {
        let ctx = lua
            .app_data_ref::<VaultContext>()
            .ok_or_else(|| mlua::Error::runtime("VaultContext not available"))?;

        let current = match &ctx.current_note {
            Some(note) => note,
            None => return Ok(Value::Nil),
        };

        // Build note table
        let note_table = lua.create_table()?;
        note_table.set("path", current.path.as_str())?;
        note_table.set("type", current.note_type.as_str())?;
        note_table.set("content", current.content.as_str())?;

        if let Some(title) = &current.title {
            note_table.set("title", title.as_str())?;
        }

        if let Some(fm) = &current.frontmatter {
            let fm_table = yaml_to_lua_table(lua, fm)?;
            note_table.set("frontmatter", fm_table)?;
        }

        Ok(Value::Table(note_table))
    })
}

/// Create the `mdv.backlinks(path)` function.
///
/// Returns a list of notes that link to the specified path.
///
/// # Examples (in Lua)
///
/// ```lua
/// local links = mdv.backlinks("projects/my-project.md")
/// for _, link in ipairs(links) do
///     print(link.source_path .. " links to this note")
/// end
/// ```
fn create_backlinks_fn(lua: &Lua) -> LuaResult<Function> {
    lua.create_function(|lua, path: String| {
        let ctx = lua
            .app_data_ref::<VaultContext>()
            .ok_or_else(|| mlua::Error::runtime("VaultContext not available"))?;

        let db = match &ctx.index_db {
            Some(db) => db,
            None => {
                return Err(mlua::Error::runtime(
                    "Index database not available. Run 'mdv reindex' first.",
                ));
            }
        };

        // Resolve path
        let resolved_path = resolve_note_path(&ctx.vault_root, &path);

        // Get note ID
        let note = match db.get_note_by_path(Path::new(&resolved_path)) {
            Ok(Some(n)) => n,
            Ok(None) => {
                // Return empty table if note not found
                return Ok(Value::Table(lua.create_table()?));
            }
            Err(e) => return Err(mlua::Error::runtime(format!("Index error: {}", e))),
        };

        let note_id = match note.id {
            Some(id) => id,
            None => return Ok(Value::Table(lua.create_table()?)),
        };

        // Get backlinks
        let backlinks = db
            .get_backlinks(note_id)
            .map_err(|e| mlua::Error::runtime(format!("Index error: {}", e)))?;

        // Convert to Lua table
        let result = lua.create_table()?;
        for (i, link) in backlinks.iter().enumerate() {
            let link_table = lua.create_table()?;

            // Get source note path
            if let Ok(Some(source_note)) = db.get_note_by_id(link.source_id) {
                link_table
                    .set("source_path", source_note.path.to_string_lossy().to_string())?;
                link_table.set("source_title", source_note.title)?;
                link_table.set("source_type", source_note.note_type.as_str())?;
            }

            if let Some(text) = &link.link_text {
                link_table.set("link_text", text.as_str())?;
            }
            if let Some(context) = &link.context {
                link_table.set("context", context.as_str())?;
            }
            link_table.set("link_type", link.link_type.as_str())?;

            result.set(i + 1, link_table)?;
        }

        Ok(Value::Table(result))
    })
}

/// Create the `mdv.outlinks(path)` function.
///
/// Returns a list of notes that the specified path links to.
///
/// # Examples (in Lua)
///
/// ```lua
/// local links = mdv.outlinks("projects/my-project.md")
/// for _, link in ipairs(links) do
///     print("Links to: " .. link.target_path)
/// end
/// ```
fn create_outlinks_fn(lua: &Lua) -> LuaResult<Function> {
    lua.create_function(|lua, path: String| {
        let ctx = lua
            .app_data_ref::<VaultContext>()
            .ok_or_else(|| mlua::Error::runtime("VaultContext not available"))?;

        let db = match &ctx.index_db {
            Some(db) => db,
            None => {
                return Err(mlua::Error::runtime(
                    "Index database not available. Run 'mdv reindex' first.",
                ));
            }
        };

        // Resolve path
        let resolved_path = resolve_note_path(&ctx.vault_root, &path);

        // Get note ID
        let note = match db.get_note_by_path(Path::new(&resolved_path)) {
            Ok(Some(n)) => n,
            Ok(None) => {
                // Return empty table if note not found
                return Ok(Value::Table(lua.create_table()?));
            }
            Err(e) => return Err(mlua::Error::runtime(format!("Index error: {}", e))),
        };

        let note_id = match note.id {
            Some(id) => id,
            None => return Ok(Value::Table(lua.create_table()?)),
        };

        // Get outgoing links
        let outlinks = db
            .get_outgoing_links(note_id)
            .map_err(|e| mlua::Error::runtime(format!("Index error: {}", e)))?;

        // Convert to Lua table
        let result = lua.create_table()?;
        for (i, link) in outlinks.iter().enumerate() {
            let link_table = lua.create_table()?;

            link_table.set("target_path", link.target_path.as_str())?;

            // Get target note info if resolved
            if let Some(target_id) = link.target_id {
                if let Ok(Some(target_note)) = db.get_note_by_id(target_id) {
                    link_table.set("target_title", target_note.title)?;
                    link_table.set("target_type", target_note.note_type.as_str())?;
                    link_table.set("resolved", true)?;
                } else {
                    link_table.set("resolved", false)?;
                }
            } else {
                link_table.set("resolved", false)?;
            }

            if let Some(text) = &link.link_text {
                link_table.set("link_text", text.as_str())?;
            }
            link_table.set("link_type", link.link_type.as_str())?;

            result.set(i + 1, link_table)?;
        }

        Ok(Value::Table(result))
    })
}

/// Create the `mdv.query(opts)` function.
///
/// Query the vault index with filters.
///
/// # Examples (in Lua)
///
/// ```lua
/// -- Find all open tasks
/// local tasks = mdv.query({ type = "task" })
/// for _, note in ipairs(tasks) do
///     print(note.path .. ": " .. note.title)
/// end
///
/// -- Find recent notes
/// local recent = mdv.query({ limit = 10 })
/// ```
fn create_query_fn(lua: &Lua) -> LuaResult<Function> {
    lua.create_function(|lua, opts: Option<Table>| {
        let ctx = lua
            .app_data_ref::<VaultContext>()
            .ok_or_else(|| mlua::Error::runtime("VaultContext not available"))?;

        let db = match &ctx.index_db {
            Some(db) => db,
            None => {
                return Err(mlua::Error::runtime(
                    "Index database not available. Run 'mdv reindex' first.",
                ));
            }
        };

        // Build query from options
        let mut query = NoteQuery::default();

        if let Some(opts) = opts {
            // Type filter
            if let Ok(type_str) = opts.get::<String>("type") {
                query.note_type = Some(type_str.parse().unwrap_or_default());
            }

            // Path prefix filter
            if let Ok(prefix) = opts.get::<String>("path_prefix") {
                query.path_prefix = Some(std::path::PathBuf::from(prefix));
            }

            // Limit
            if let Ok(limit) = opts.get::<i64>("limit") {
                query.limit = Some(limit as u32);
            }

            // Offset
            if let Ok(offset) = opts.get::<i64>("offset") {
                query.offset = Some(offset as u32);
            }
        }

        // Execute query
        let notes = db
            .query_notes(&query)
            .map_err(|e| mlua::Error::runtime(format!("Query error: {}", e)))?;

        // Convert to Lua table
        let result = lua.create_table()?;
        for (i, note) in notes.iter().enumerate() {
            let note_table = lua.create_table()?;
            note_table.set("path", note.path.to_string_lossy().to_string())?;
            note_table.set("type", note.note_type.as_str())?;
            note_table.set("title", note.title.clone())?;
            note_table.set("modified", note.modified.to_rfc3339())?;

            if let Some(created) = note.created {
                note_table.set("created", created.to_rfc3339())?;
            }

            // Parse and include frontmatter if available
            if let Some(fm_json) = &note.frontmatter_json
                && let Ok(fm) = serde_json::from_str::<serde_json::Value>(fm_json)
            {
                let fm_yaml = json_to_yaml(&fm);
                let fm_lua = yaml_to_lua_table(lua, &fm_yaml)?;
                note_table.set("frontmatter", fm_lua)?;
            }

            result.set(i + 1, note_table)?;
        }

        Ok(Value::Table(result))
    })
}

/// Create the `mdv.find_project(id)` function.
///
/// Finds a project note by its 'project-id' field.
/// Returns the note table or nil if not found.
#[allow(clippy::collapsible_if)]
fn create_find_project_fn(lua: &Lua) -> LuaResult<Function> {
    lua.create_function(|lua, id: String| {
        let ctx = lua
            .app_data_ref::<VaultContext>()
            .ok_or_else(|| mlua::Error::runtime("VaultContext not available"))?;

        let db = match &ctx.index_db {
            Some(db) => db,
            None => {
                return Err(mlua::Error::runtime(
                    "Index database not available. Run 'mdv reindex' first.",
                ));
            }
        };

        // Query for projects
        let query = NoteQuery {
            note_type: Some(crate::index::NoteType::Project),
            ..Default::default()
        };

        let notes = db
            .query_notes(&query)
            .map_err(|e| mlua::Error::runtime(format!("Query error: {}", e)))?;

        // Find match
        for note in notes {
            if let Some(fm_json) = &note.frontmatter_json {
                if let Ok(fm) = serde_json::from_str::<serde_json::Value>(fm_json) {
                    if fm.get("project-id").and_then(|v| v.as_str()) == Some(id.as_str())
                    {
                        // Found it! Convert to note table.
                        let note_table = lua.create_table()?;
                        note_table
                            .set("path", note.path.to_string_lossy().to_string())?;
                        note_table.set("type", note.note_type.as_str())?;
                        note_table.set("title", note.title.clone())?;
                        note_table.set("modified", note.modified.to_rfc3339())?;

                        if let Some(created) = note.created {
                            note_table.set("created", created.to_rfc3339())?;
                        }

                        let fm_yaml = json_to_yaml(&fm);
                        let fm_lua = yaml_to_lua_table(lua, &fm_yaml)?;
                        note_table.set("frontmatter", fm_lua)?;

                        return Ok(Value::Table(note_table));
                    }
                }
            }
        }

        Ok(Value::Nil)
    })
}

/// Resolve a note path relative to vault root.
fn resolve_note_path(_vault_root: &std::path::Path, path: &str) -> String {
    // If path doesn't end with .md, append it
    if path.ends_with(".md") { path.to_string() } else { format!("{}.md", path) }
}

/// Convert serde_json::Value to serde_yaml::Value.
fn json_to_yaml(json: &serde_json::Value) -> serde_yaml::Value {
    match json {
        serde_json::Value::Null => serde_yaml::Value::Null,
        serde_json::Value::Bool(b) => serde_yaml::Value::Bool(*b),
        serde_json::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                serde_yaml::Value::Number(i.into())
            } else if let Some(f) = n.as_f64() {
                serde_yaml::Value::Number(serde_yaml::Number::from(f))
            } else {
                serde_yaml::Value::Null
            }
        }
        serde_json::Value::String(s) => serde_yaml::Value::String(s.clone()),
        serde_json::Value::Array(arr) => {
            serde_yaml::Value::Sequence(arr.iter().map(json_to_yaml).collect())
        }
        serde_json::Value::Object(obj) => {
            let mut map = serde_yaml::Mapping::new();
            for (k, v) in obj {
                map.insert(serde_yaml::Value::String(k.clone()), json_to_yaml(v));
            }
            serde_yaml::Value::Mapping(map)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_resolve_note_path_with_extension() {
        let vault_root = std::path::Path::new("/vault");
        let result = resolve_note_path(vault_root, "notes/test.md");
        assert_eq!(result, "notes/test.md");
    }

    #[test]
    fn test_resolve_note_path_without_extension() {
        let vault_root = std::path::Path::new("/vault");
        let result = resolve_note_path(vault_root, "notes/test");
        assert_eq!(result, "notes/test.md");
    }

    #[test]
    fn test_json_to_yaml() {
        let json = serde_json::json!({
            "string": "value",
            "number": 42,
            "bool": true,
            "array": [1, 2, 3]
        });

        let yaml = json_to_yaml(&json);

        if let serde_yaml::Value::Mapping(map) = yaml {
            assert!(map.contains_key(serde_yaml::Value::String("string".into())));
            assert!(map.contains_key(serde_yaml::Value::String("number".into())));
        } else {
            panic!("Expected mapping");
        }
    }
}