specfold_core/assets/
asset.rs1use crate::config::types::AssetKind;
4use crate::corpus::graph::{Graph, Resolution};
5use crate::model::markdown::frontmatter;
6use serde_json::{Map, Value};
7use std::collections::BTreeSet;
8
9#[derive(Debug, Clone)]
10pub struct Asset {
11 pub kind: AssetKind,
12 pub path: String,
14 pub stem: String,
16 pub fields: Option<Map<String, Value>>,
18 pub invalid: Option<String>,
20 pub body_line: usize,
22 pub text: String,
23}
24
25impl Asset {
26 pub fn field(&self, key: &str) -> Option<&str> {
28 self.fields.as_ref()?.get(key)?.as_str()
29 }
30
31 pub fn name(&self) -> &str {
33 self.field("name").unwrap_or(&self.stem)
34 }
35
36 pub fn body(&self) -> &str {
37 frontmatter::split(&self.text).body
38 }
39
40 pub fn docs(&self, g: &Graph) -> Vec<usize> {
44 let dir = self.path.rsplit_once('/').map(|(d, _)| d).unwrap_or("");
45 let mut out = BTreeSet::new();
46 let ok = |c: char| c.is_ascii_alphanumeric() || ":/._#-".contains(c);
47 for raw in self.text.split(|c: char| !ok(c)) {
48 let t = raw.trim_end_matches(['.', ':', '#']);
49 if !(t.contains(':') || t.contains('/') || t.ends_with(".md")) || t.contains("://") {
50 continue;
51 }
52 for cand in [t.to_string(), join(dir, t)] {
53 if let Resolution::Doc { idx, via_alias: false, .. } = g.resolve(&cand) {
54 out.insert(idx);
55 break;
56 }
57 }
58 }
59 out.into_iter().collect()
60 }
61}
62
63pub(super) fn join(dir: &str, rel: &str) -> String {
65 let mut parts: Vec<&str> = if dir.is_empty() { vec![] } else { dir.split('/').collect() };
66 for c in rel.split('/') {
67 match c {
68 "" | "." => {}
69 ".." => {
70 if parts.pop().is_none() {
71 return rel.to_string();
72 }
73 }
74 c => parts.push(c),
75 }
76 }
77 parts.join("/")
78}