MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / render_entry_to_markdown

Function render_entry_to_markdown

atomic-repository/src/repository/vault.rs:966–1025  ·  view source on GitHub ↗

Render a vault entry to a markdown string with YAML frontmatter. The frontmatter is constructed from: 1. The entry's `frontmatter_json` fields (user-provided metadata) 2. System metadata (entry_type, content_hash, created_at, updated_at) The system fields are always included to enable round-trip (deflation can reconstruct the VaultEntry from the file).

(entry: &VaultEntry)

Source from the content-addressed store, hash-verified

964/// The system fields are always included to enable round-trip (deflation
965/// can reconstruct the VaultEntry from the file).
966fn render_entry_to_markdown(entry: &VaultEntry) -> String {
967 let mut output = String::new();
968 output.push_str("---\n");
969
970 // Parse the frontmatter JSON into a map so we can merge system fields
971 let mut fm: serde_json::Map<String, serde_json::Value> =
972 serde_json::from_str(&entry.frontmatter_json).unwrap_or_default();
973
974 // Add system fields (these take precedence)
975 fm.insert(
976 "entry_type".to_string(),
977 serde_json::Value::String(entry.entry_type.to_string()),
978 );
979 fm.insert(
980 "content_hash".to_string(),
981 serde_json::Value::String(Hash::from_bytes(entry.content_hash).to_base32()),
982 );
983 fm.insert(
984 "created_at".to_string(),
985 serde_json::Value::String(entry.created_at.clone()),
986 );
987 fm.insert(
988 "updated_at".to_string(),
989 serde_json::Value::String(entry.updated_at.clone()),
990 );
991
992 // Write frontmatter in a stable order: user fields first (sorted), then system fields
993 let system_keys = ["entry_type", "content_hash", "created_at", "updated_at"];
994 let mut user_keys: Vec<&String> = fm
995 .keys()
996 .filter(|k| !system_keys.contains(&k.as_str()))
997 .collect();
998 user_keys.sort();
999
1000 // Write user fields
1001 for key in &user_keys {
1002 write_frontmatter_field(&mut output, key, &fm[key.as_str()]);
1003 }
1004
1005 // Write system fields
1006 for key in &system_keys {
1007 if let Some(value) = fm.get(*key) {
1008 write_frontmatter_field(&mut output, key, value);
1009 }
1010 }
1011
1012 output.push_str("---\n");
1013
1014 // Content body
1015 let content = String::from_utf8_lossy(&entry.content_bytes);
1016 if !content.is_empty() {
1017 output.push_str(&content);
1018 // Ensure file ends with a newline
1019 if !content.ends_with('\n') {
1020 output.push('\n');
1021 }
1022 }
1023

Callers 2

vault_materializeMethod · 0.85

Calls 10

write_frontmatter_fieldFunction · 0.85
sortMethod · 0.80
getMethod · 0.65
insertMethod · 0.45
to_base32Method · 0.45
cloneMethod · 0.45
containsMethod · 0.45
as_strMethod · 0.45
is_emptyMethod · 0.45
pushMethod · 0.45

Tested by 1