Skip to main content

kimun_notes/cli/
json_output.rs

1use std::collections::HashMap;
2use chrono::Utc;
3use kimun_core::nfs::NoteEntryData;
4use kimun_core::note::NoteContentData;
5use kimun_core::nfs::VaultPath;
6use kimun_core::NoteVault;
7use serde::{Deserialize, Serialize};
8use crate::cli::metadata_extractor::{extract_tags, extract_links, extract_headers};
9
10#[derive(Debug, Serialize, Deserialize, Clone)]
11pub struct JsonHeader {
12    pub level: u32,
13    pub text: String,
14}
15
16#[derive(Debug, Serialize, Deserialize)]
17pub struct JsonOutputMetadata {
18    pub workspace: String,
19    pub workspace_path: String,
20    pub total_results: usize,
21    pub query: Option<String>,
22    pub is_listing: bool,
23    pub generated_at: String,
24}
25
26/// Nested note-level metadata extracted from content
27#[derive(Debug, Serialize, Deserialize)]
28pub struct JsonNoteMetadata {
29    pub tags: Vec<String>,
30    pub links: Vec<String>,
31    pub headers: Vec<JsonHeader>,
32}
33
34#[derive(Debug, Serialize, Deserialize)]
35pub struct JsonNoteEntry {
36    pub path: String,
37    pub title: String,
38    pub content: String,
39    pub size: u64,
40    pub modified: u64,
41    pub created: u64,
42    pub hash: String,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub journal_date: Option<String>,
45    pub metadata: JsonNoteMetadata,
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub backlinks: Option<Vec<String>>,
48}
49
50#[derive(Debug, Serialize, Deserialize)]
51pub struct JsonOutput {
52    pub metadata: JsonOutputMetadata,
53    pub notes: Vec<JsonNoteEntry>,
54}
55
56/// Format note entries with their content as JSON output.
57pub fn format_notes_with_content_as_json(
58    vault: &NoteVault,
59    entries: &[(NoteEntryData, NoteContentData)],
60    content_map: &[(VaultPath, String)],
61    workspace_name: &str,
62    workspace_path: &str,
63    query: Option<&str>,
64    is_listing: bool,
65) -> Result<String, Box<dyn std::error::Error>> {
66    let output_metadata = JsonOutputMetadata {
67        workspace: workspace_name.to_string(),
68        workspace_path: workspace_path.to_string(),
69        total_results: entries.len(),
70        query: query.map(|q| q.to_string()),
71        is_listing,
72        generated_at: Utc::now().to_rfc3339(),
73    };
74
75    let content_lookup: HashMap<String, &str> = content_map
76        .iter()
77        .map(|(p, c)| (p.to_string(), c.as_str()))
78        .collect();
79
80    let notes = entries
81        .iter()
82        .map(|(entry_data, content_data)| {
83            let path_str = entry_data.path.to_string();
84            let path_with_ext = entry_data.path.to_string_with_ext();
85
86            let content: &str = content_lookup.get(&path_str).copied().unwrap_or("");
87
88            let tags = extract_tags(content);
89            let links = extract_links(content);
90            let headers = extract_headers(content);
91
92            // Detect journal date using vault
93            let journal_date = vault
94                .journal_date(&entry_data.path)
95                .map(|d| d.format("%Y-%m-%d").to_string());
96
97            // Use modified as created (fallback until created timestamp is tracked separately)
98            let created = entry_data.modified_secs;
99
100            JsonNoteEntry {
101                path: path_with_ext,
102                title: content_data.title.clone(),
103                content: content.to_owned(),
104                size: entry_data.size,
105                modified: entry_data.modified_secs,
106                created,
107                hash: format!("{:x}", content_data.hash),
108                journal_date,
109                metadata: JsonNoteMetadata {
110                    tags,
111                    links,
112                    headers,
113                },
114                backlinks: None,
115            }
116        })
117        .collect();
118
119    let output = JsonOutput { metadata: output_metadata, notes };
120    Ok(serde_json::to_string(&output)?)
121}
122
123/// Format note entries as JSON output, fetching note content from vault
124pub async fn format_notes_as_json(
125    vault: &NoteVault,
126    entries: &[(NoteEntryData, NoteContentData)],
127    workspace_name: &str,
128    query: Option<&str>,
129    is_listing: bool,
130) -> Result<String, Box<dyn std::error::Error>> {
131    let workspace_path = vault.workspace_path.to_string_lossy().to_string();
132
133    // Fetch actual content for each note concurrently to enable metadata extraction
134    let content_futures: Vec<_> = entries
135        .iter()
136        .map(|(entry_data, _)| async {
137            let path = entry_data.path.clone();
138            match vault.get_note_text(&path).await {
139                Ok(content) => Some((path, content)),
140                Err(_) => None,
141            }
142        })
143        .collect();
144
145    let content_results = futures::future::join_all(content_futures).await;
146    let content_map: Vec<_> = content_results.into_iter().flatten().collect();
147
148    format_notes_with_content_as_json(
149        vault,
150        entries,
151        &content_map,
152        workspace_name,
153        &workspace_path,
154        query,
155        is_listing,
156    )
157}