diaryx_core 1.4.4

Core library for Diaryx - a tool to manage markdown files with YAML frontmatter
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Attachment operation command handlers.

use std::path::{Path, PathBuf};

use crate::yaml_value::YamlValue;

use crate::command::{ResolvedAttachmentRef, Response};
use crate::diaryx::Diaryx;
use crate::error::{DiaryxError, Result};
use crate::fs::AsyncFileSystem;

impl<FS: AsyncFileSystem + Clone> Diaryx<FS> {
    fn attachment_note_filename(filename: &str) -> String {
        format!("{filename}.md")
    }

    fn attachment_note_canonical_path(&self, entry_path: &str, filename: &str) -> String {
        let note_filename = Self::attachment_note_filename(filename);
        let entry_parent = Path::new(entry_path)
            .parent()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_default();
        if entry_parent.is_empty() {
            format!("_attachments/{note_filename}")
        } else {
            format!("{entry_parent}/_attachments/{note_filename}")
        }
    }

    async fn resolve_attachment_note(
        &self,
        entry_path: &str,
        attachment_ref: &str,
    ) -> Result<(String, String, Option<String>)> {
        let entry_canonical = self.get_canonical_path(entry_path);
        let note_canonical = self.resolve_frontmatter_link_target(attachment_ref, &entry_canonical);
        let note_fs_path = self.resolve_fs_path(&note_canonical);
        let note = match self
            .workspace()
            .inner()
            .parse_index_with_hint(&note_fs_path, Some(self.link_format()))
            .await
        {
            Ok(note) => note,
            Err(_) => {
                // Body HTML/media embeds still point directly at binary assets.
                // Allow those refs for read/preview flows while frontmatter
                // `attachments[]` remains note-backed.
                if self.fs().exists(&note_fs_path).await {
                    return Ok((note_canonical.clone(), note_canonical, None));
                }
                return Err(DiaryxError::Validation(format!(
                    "Attachment note not found: {attachment_ref}"
                )));
            }
        };
        let binary_ref = note.frontmatter.attachment.clone().ok_or_else(|| {
            DiaryxError::Validation(format!(
                "Attachment note '{}' is missing its attachment property",
                note_canonical
            ))
        })?;
        let binary_canonical = self.resolve_attachment_link_target_with_hint(
            &binary_ref,
            &self.get_canonical_path(&note_canonical),
            Some(crate::link_parser::LinkFormat::PlainCanonical),
        );
        Ok((
            note_canonical,
            binary_canonical,
            note.frontmatter.title.clone(),
        ))
    }

    async fn ensure_attachment_note(
        &self,
        entry_path: &str,
        filename: &str,
    ) -> Result<(String, String)> {
        let entry_canonical = self.get_canonical_path(entry_path);
        let binary_canonical = if let Some(parent) = Path::new(&entry_canonical).parent() {
            let parent = parent.to_string_lossy();
            if parent.is_empty() || parent == "." {
                format!("_attachments/{filename}")
            } else {
                format!("{parent}/_attachments/{filename}")
            }
        } else {
            format!("_attachments/{filename}")
        };
        let note_canonical = self.attachment_note_canonical_path(entry_path, filename);
        let note_fs_path = self.resolve_fs_path(&note_canonical);

        if !self.fs().exists(&note_fs_path).await {
            let title = filename.to_string();
            let note_link = self.format_link_for_file(&note_canonical, &note_canonical);
            let attachment_link =
                self.format_attachment_link_for_file(&binary_canonical, &note_canonical);
            let content = format!(
                "---\ntitle: {title}\nlink: \"{note_link}\"\nattachment: \"{attachment_link}\"\n---\n"
            );
            self.fs()
                .write_file(&note_fs_path, &content)
                .await
                .map_err(|e| DiaryxError::FileWrite {
                    path: note_fs_path.clone(),
                    source: e,
                })?;
        }

        Ok((note_canonical, binary_canonical))
    }

    async fn upsert_attachment_backlink(&self, note_path: &str, source_path: &str) -> Result<()> {
        let note_canonical = self.get_canonical_path(note_path);
        let source_canonical = self.get_canonical_path(source_path);
        let existing = self
            .entry()
            .get_frontmatter_property(note_path, "attachment_of")
            .await?;
        let mut items = match existing {
            Some(YamlValue::Sequence(items)) => items,
            _ => Vec::new(),
        };
        let exists = items.iter().any(|item| {
            item.as_str().is_some_and(|s| {
                self.resolve_frontmatter_link_target(s, &note_canonical) == source_canonical
            })
        });
        if !exists {
            let formatted = self.format_link_for_file(&source_canonical, &note_canonical);
            items.push(YamlValue::String(formatted));
            self.entry()
                .set_frontmatter_property(note_path, "attachment_of", YamlValue::Sequence(items))
                .await?;
        }
        Ok(())
    }

    async fn remove_attachment_backlink(
        &self,
        note_path: &str,
        source_path: &str,
    ) -> Result<usize> {
        let note_canonical = self.get_canonical_path(note_path);
        let source_canonical = self.get_canonical_path(source_path);
        let existing = self
            .entry()
            .get_frontmatter_property(note_path, "attachment_of")
            .await?;
        let Some(YamlValue::Sequence(items)) = existing else {
            return Ok(0);
        };
        let filtered: Vec<YamlValue> = items
            .into_iter()
            .filter(|item| {
                !item.as_str().is_some_and(|s| {
                    self.resolve_frontmatter_link_target(s, &note_canonical) == source_canonical
                })
            })
            .collect();
        let remaining = filtered.len();
        if remaining == 0 {
            self.entry()
                .remove_frontmatter_property(note_path, "attachment_of")
                .await?;
        } else {
            self.entry()
                .set_frontmatter_property(note_path, "attachment_of", YamlValue::Sequence(filtered))
                .await?;
        }
        Ok(remaining)
    }

    pub(crate) async fn cmd_get_attachments(&self, path: String) -> Result<Response> {
        let attachments = self.entry().get_attachments(&path).await?;
        Ok(Response::Strings(attachments))
    }

    pub(crate) async fn cmd_get_ancestor_attachments(&self, path: String) -> Result<Response> {
        use crate::command::{AncestorAttachmentEntry, AncestorAttachmentsResult};
        use std::collections::HashSet;

        let ws = self.workspace().inner();
        let mut entries = Vec::new();
        let mut visited = HashSet::new();
        let mut current_path = self.resolve_fs_path(&path);

        // Get workspace root for resolving workspace-relative paths
        let workspace_root = self.workspace_root().unwrap_or_else(|| {
            current_path
                .parent()
                .and_then(|p| p.parent())
                .unwrap_or(Path::new("."))
                .to_path_buf()
        });

        // Try to get link format from workspace config
        let link_format = ws
            .get_workspace_config(&current_path)
            .await
            .map(|c| c.link_format)
            .ok();

        // Maximum depth to prevent runaway traversal
        const MAX_DEPTH: usize = 100;

        // Traverse up the part_of chain
        for _ in 0..MAX_DEPTH {
            let path_str = current_path.to_string_lossy().to_string();
            if visited.contains(&path_str) {
                break; // Circular reference protection
            }
            visited.insert(path_str.clone());

            // Try to parse the file (with link format hint)
            if let Ok(index) = ws.parse_index_with_hint(&current_path, link_format).await {
                let mut attachments = Vec::new();
                for note_ref in index.frontmatter.attachments_list() {
                    if let Ok((_, binary_canonical, note_title)) =
                        self.resolve_attachment_note(&path_str, note_ref).await
                    {
                        attachments.push(ResolvedAttachmentRef {
                            note_path: note_ref.clone(),
                            attachment_path: binary_canonical,
                            note_title,
                        });
                    }
                }

                // Only add if there are attachments
                if !attachments.is_empty() {
                    entries.push(AncestorAttachmentEntry {
                        entry_path: path_str,
                        entry_title: index.frontmatter.title.clone(),
                        attachments,
                    });
                }

                // Move to parent via part_of
                if let Some(part_of) = &index.frontmatter.part_of {
                    let parent_path = index.resolve_path(part_of);
                    // Make path absolute if needed
                    current_path = if parent_path.is_absolute() {
                        parent_path
                    } else {
                        workspace_root.join(&parent_path)
                    };
                } else {
                    break; // Reached root
                }
            } else {
                break; // File doesn't exist or can't be parsed
            }
        }

        Ok(Response::AncestorAttachments(AncestorAttachmentsResult {
            entries,
        }))
    }

    pub(crate) async fn cmd_register_attachment(
        &self,
        entry_path: String,
        filename: String,
    ) -> Result<Response> {
        let (note_canonical, binary_canonical) =
            self.ensure_attachment_note(&entry_path, &filename).await?;
        let entry_canonical = self.get_canonical_path(&entry_path);
        let link = self.format_link_for_file(&note_canonical, &entry_canonical);

        self.entry().add_attachment(&entry_path, &link).await?;
        self.upsert_attachment_backlink(&note_canonical, &entry_path)
            .await?;

        let storage_path = self
            .resolve_fs_path(&binary_canonical)
            .to_string_lossy()
            .into_owned();
        Ok(Response::Strings(vec![link, storage_path]))
    }

    pub(crate) async fn cmd_delete_attachment(
        &self,
        entry_path: String,
        attachment_path: String,
    ) -> Result<Response> {
        let (note_canonical, binary_canonical, _) = self
            .resolve_attachment_note(&entry_path, &attachment_path)
            .await?;
        self.entry()
            .remove_attachment(&entry_path, &attachment_path)
            .await?;
        let remaining = self
            .remove_attachment_backlink(&note_canonical, &entry_path)
            .await?;

        if remaining == 0 {
            let binary_full_path = self.resolve_fs_path(&binary_canonical);
            if self.fs().exists(&binary_full_path).await {
                self.fs()
                    .delete_file(&binary_full_path)
                    .await
                    .map_err(|e| DiaryxError::FileWrite {
                        path: binary_full_path,
                        source: e,
                    })?;
            }
            let note_full_path = self.resolve_fs_path(&note_canonical);
            if self.fs().exists(&note_full_path).await {
                self.fs().delete_file(&note_full_path).await.map_err(|e| {
                    DiaryxError::FileWrite {
                        path: note_full_path,
                        source: e,
                    }
                })?;
            }
        }

        Ok(Response::Ok)
    }

    pub(crate) async fn cmd_get_attachment_data(
        &self,
        entry_path: String,
        attachment_path: String,
    ) -> Result<Response> {
        let (_, binary_canonical, _) = self
            .resolve_attachment_note(&entry_path, &attachment_path)
            .await?;
        let full_path = self.resolve_fs_path(&binary_canonical);

        let data = self
            .fs()
            .read_binary(&full_path)
            .await
            .map_err(|e| DiaryxError::FileRead {
                path: full_path,
                source: e,
            })?;

        Ok(Response::Bytes(data))
    }

    pub(crate) async fn cmd_resolve_attachment_path(
        &self,
        entry_path: String,
        attachment_path: String,
    ) -> Result<Response> {
        let (_, binary_canonical, _) = self
            .resolve_attachment_note(&entry_path, &attachment_path)
            .await?;
        let full_path = self.resolve_fs_path(&binary_canonical);
        Ok(Response::String(full_path.to_string_lossy().into_owned()))
    }

    pub(crate) async fn cmd_move_attachment(
        &self,
        source_entry_path: String,
        target_entry_path: String,
        attachment_path: String,
        new_filename: Option<String>,
    ) -> Result<Response> {
        let (note_canonical, binary_canonical, note_title) = self
            .resolve_attachment_note(&source_entry_path, &attachment_path)
            .await?;
        let source_attachment_path = self.resolve_fs_path(&binary_canonical);

        // Get the original filename
        let original_filename = Path::new(&binary_canonical)
            .file_name()
            .and_then(|n| n.to_str())
            .ok_or_else(|| DiaryxError::InvalidPath {
                path: PathBuf::from(&binary_canonical),
                message: "Could not extract filename".to_string(),
            })?;

        // Determine final filename (use new_filename if provided, otherwise original)
        let final_filename = new_filename.as_deref().unwrap_or(original_filename);

        // Resolve target paths
        let target_entry = PathBuf::from(&target_entry_path);
        let target_dir = target_entry.parent().unwrap_or_else(|| Path::new("."));
        let target_attachments_dir = target_dir.join("_attachments");
        let target_binary_canonical = self
            .attachment_note_canonical_path(&target_entry_path, final_filename)
            .trim_end_matches(".md")
            .to_string();
        let target_attachment_path = self.resolve_fs_path(&target_binary_canonical);

        // Check for collision at destination
        if self.fs().exists(&target_attachment_path).await {
            return Err(DiaryxError::InvalidPath {
                path: target_attachment_path,
                message: "File already exists at destination".to_string(),
            });
        }

        // Read the source file data
        let data = self
            .fs()
            .read_binary(&source_attachment_path)
            .await
            .map_err(|e| DiaryxError::FileRead {
                path: source_attachment_path.clone(),
                source: e,
            })?;

        // Create target _attachments directory if needed
        let resolved_target_attachments_dir = self.resolve_fs_path(&target_attachments_dir);
        self.fs()
            .create_dir_all(&resolved_target_attachments_dir)
            .await?;

        // Write to target location
        self.fs()
            .write_binary(&target_attachment_path, &data)
            .await
            .map_err(|e| DiaryxError::FileWrite {
                path: target_attachment_path.clone(),
                source: e,
            })?;

        let note_full_path = self.resolve_fs_path(&note_canonical);
        let target_note_canonical =
            self.attachment_note_canonical_path(&target_entry_path, final_filename);
        let target_note_full_path = self.resolve_fs_path(&target_note_canonical);
        let backlinks = self
            .entry()
            .get_frontmatter_property(&note_canonical, "attachment_of")
            .await?
            .and_then(|v| v.as_sequence().cloned())
            .unwrap_or_default();
        let note_title = note_title.unwrap_or_else(|| final_filename.to_string());
        let target_note_link =
            self.format_link_for_file(&target_note_canonical, &target_note_canonical);
        let target_attachment_link =
            self.format_attachment_link_for_file(&target_binary_canonical, &target_note_canonical);
        let attachment_of_lines = if backlinks.is_empty() {
            String::new()
        } else {
            let mut s = String::from("attachment_of:\n");
            for backlink in &backlinks {
                if let Some(backlink) = backlink.as_str() {
                    s.push_str(&format!("  - \"{backlink}\"\n"));
                }
            }
            s
        };
        let note_content = format!(
            "---\ntitle: {note_title}\nlink: \"{target_note_link}\"\nattachment: \"{target_attachment_link}\"\n{attachment_of_lines}---\n"
        );
        self.fs()
            .write_file(&target_note_full_path, &note_content)
            .await
            .map_err(|e| DiaryxError::FileWrite {
                path: target_note_full_path.clone(),
                source: e,
            })?;

        for backlink in backlinks.iter().filter_map(|v| v.as_str()) {
            let source_canonical =
                self.resolve_frontmatter_link_target(backlink, &target_note_canonical);
            let source_formatted =
                self.format_link_for_file(&target_note_canonical, &source_canonical);
            let old_formatted = self.format_link_for_file(&note_canonical, &source_canonical);
            let existing = self.entry().get_attachments(&source_canonical).await?;
            let rewritten: Vec<String> = existing
                .into_iter()
                .map(|item| {
                    let item_canonical =
                        self.resolve_frontmatter_link_target(&item, &source_canonical);
                    if item_canonical == note_canonical || item == old_formatted {
                        source_formatted.clone()
                    } else {
                        item
                    }
                })
                .collect();
            self.entry()
                .set_frontmatter_property(
                    &source_canonical,
                    "attachments",
                    YamlValue::Sequence(rewritten.into_iter().map(YamlValue::String).collect()),
                )
                .await?;
        }

        // Delete the original file
        self.fs()
            .delete_file(&source_attachment_path)
            .await
            .map_err(|e| DiaryxError::FileWrite {
                path: source_attachment_path,
                source: e,
            })?;

        if self.fs().exists(&note_full_path).await {
            self.fs()
                .delete_file(&note_full_path)
                .await
                .map_err(|e| DiaryxError::FileWrite {
                    path: note_full_path,
                    source: e,
                })?;
        }

        Ok(Response::String(target_note_canonical))
    }
}