codesynapse_core/ts_extract/
astro.rs1use super::make_file_node;
2use crate::error::Result;
3use crate::extract::{make_id, ImportNode, LanguageExtractor};
4use crate::types::{Edge, ExtractionFragment, Node};
5use std::collections::{HashMap, HashSet};
6use std::path::Path;
7
8pub struct AstroExtractor;
9
10impl AstroExtractor {
11 pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
12 let (file_id, _, file_node) = make_file_node(path);
13 let mut fragment = ExtractionFragment {
14 nodes: vec![file_node],
15 edges: vec![],
16 };
17
18 let content = std::str::from_utf8(source).unwrap_or("");
19 let str_path = path.to_string_lossy().to_string();
20
21 let mut regions: Vec<String> = Vec::new();
22
23 let trimmed = content.trim_start_matches(['\r', '\n']);
25 if let Some(rest) = trimmed.strip_prefix("---") {
26 if let Some(end) = rest.find("\n---") {
27 regions.push(rest[..end].to_string());
28 }
29 }
30
31 let content_lower = content.to_ascii_lowercase();
33 let mut search_start = 0usize;
34 while let Some(rel_open) = content_lower[search_start..].find("<script") {
35 let abs_open = search_start + rel_open;
36 if let Some(rel_tag_end) = content_lower[abs_open..].find('>') {
37 let content_start = abs_open + rel_tag_end + 1;
38 if let Some(rel_close) = content_lower[content_start..].find("</script") {
39 let abs_close = content_start + rel_close;
40 regions.push(content[content_start..abs_close].to_string());
41 search_start = abs_close + 8;
42 continue;
43 }
44 }
45 break;
46 }
47
48 let static_re = regex::Regex::new(r#"import\s+(?:[^'"`;\n]+?\s+from\s+)?['"]([^'"]+)['"]"#)
50 .expect("static import regex");
51 let dynamic_re = regex::Regex::new(r#"import\(\s*['"]([^'"]+)['"]\s*\)?"#)
52 .expect("dynamic import regex");
53
54 let mut seen: HashSet<String> = HashSet::new();
55
56 let add_import = |fragment: &mut ExtractionFragment,
57 seen: &mut HashSet<String>,
58 raw: &str,
59 relation: &str| {
60 if raw.is_empty() {
61 return;
62 }
63 let module = raw.rsplit('/').next().unwrap_or(raw);
64 if module.is_empty() {
65 return;
66 }
67 let node_id = make_id(&[module]);
68 let is_new = seen.insert(node_id.clone());
69 if is_new {
70 fragment.nodes.push(Node {
71 id: node_id.clone(),
72 label: raw.to_string(),
73 file_type: "module".to_string(),
74 source_file: str_path.clone(),
75 source_location: None,
76 community: None,
77 rationale: None,
78 docstring: None,
79 metadata: HashMap::new(),
80 });
81 }
82 fragment.edges.push(Edge {
83 source: file_id.clone(),
84 target: node_id,
85 relation: relation.to_string(),
86 confidence: "EXTRACTED".to_string(),
87 source_file: Some(str_path.clone()),
88 weight: 1.0,
89 context: None,
90 });
91 };
92
93 for region in ®ions {
94 for cap in static_re.captures_iter(region) {
95 if let Some(m) = cap.get(1) {
96 add_import(&mut fragment, &mut seen, m.as_str(), "imports_from");
97 }
98 }
99 for cap in dynamic_re.captures_iter(region) {
100 if let Some(m) = cap.get(1) {
101 add_import(&mut fragment, &mut seen, m.as_str(), "dynamic_import");
102 }
103 }
104 }
105
106 Ok(fragment)
107 }
108}
109
110impl LanguageExtractor for AstroExtractor {
111 fn file_extensions(&self) -> Vec<&'static str> {
112 vec!["astro"]
113 }
114 fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
115 Self::extract(source, path)
116 }
117 fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
118 vec![]
119 }
120 fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
121}