Skip to main content

drft/parsers/
frontmatter.rs

1use super::{ParseResult, Parser};
2
3/// Check whether a frontmatter value looks like a link target (file path or URI).
4fn is_link_candidate(value: &str) -> bool {
5    // URIs are always candidates — graph builder classifies them as External(Remote)
6    if crate::util::is_uri(value) {
7        return true;
8    }
9    // Explicit path prefixes are always candidates.
10    // The graph builder gates all filesystem access for out-of-root targets.
11    if value.starts_with("./") || value.starts_with("../") || value.starts_with('/') {
12        return true;
13    }
14    // Prose contains spaces — file paths don't
15    if value.contains(' ') {
16        return false;
17    }
18    // Must have a plausible file extension: dot followed by 1-6 alphanumeric
19    // chars that aren't all digits (rejects v2.0, e.g., Dr.)
20    let basename = value.rsplit('/').next().unwrap_or(value);
21    if let Some(dot_pos) = basename.rfind('.') {
22        let ext = &basename[dot_pos + 1..];
23        !ext.is_empty()
24            && ext.len() <= 6
25            && ext.chars().all(|c| c.is_ascii_alphanumeric())
26            && !ext.chars().all(|c| c.is_ascii_digit())
27    } else {
28        false
29    }
30}
31
32/// Strip all code content (fenced blocks and inline backtick spans),
33/// replacing with spaces to preserve offsets.
34fn strip_code(content: &str) -> String {
35    // First strip fenced code blocks (``` and ~~~)
36    let mut result = String::with_capacity(content.len());
37    let mut in_code_block = false;
38    let mut fence_marker = "";
39
40    for line in content.lines() {
41        let trimmed = line.trim_start();
42        if !in_code_block {
43            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
44                in_code_block = true;
45                fence_marker = if trimmed.starts_with("```") {
46                    "```"
47                } else {
48                    "~~~"
49                };
50                result.push_str(&" ".repeat(line.len()));
51            } else {
52                result.push_str(line);
53            }
54        } else if trimmed.starts_with(fence_marker) && trimmed.trim() == fence_marker {
55            in_code_block = false;
56            result.push_str(&" ".repeat(line.len()));
57        } else {
58            result.push_str(&" ".repeat(line.len()));
59        }
60        result.push('\n');
61    }
62
63    // Then strip inline code spans (single and double backticks)
64    let mut cleaned = String::with_capacity(result.len());
65    let chars: Vec<char> = result.chars().collect();
66    let mut i = 0;
67    while i < chars.len() {
68        if chars[i] == '`' {
69            // Count opening backticks
70            let mut ticks = 0;
71            while i + ticks < chars.len() && chars[i + ticks] == '`' {
72                ticks += 1;
73            }
74            // Find matching closing backticks in the char array
75            let after = i + ticks;
76            let mut found = None;
77            let mut j = after;
78            while j + ticks <= chars.len() {
79                if chars[j..j + ticks].iter().all(|c| *c == '`') {
80                    found = Some(j);
81                    break;
82                }
83                j += 1;
84            }
85            if let Some(close_start) = found {
86                // Replace entire span (backticks + content + backticks) with spaces
87                let total = close_start + ticks - i;
88                for _ in 0..total {
89                    cleaned.push(' ');
90                }
91                i += total;
92            } else {
93                // No closing — keep the backtick as-is
94                cleaned.push(chars[i]);
95                i += 1;
96            }
97        } else {
98            cleaned.push(chars[i]);
99            i += 1;
100        }
101    }
102
103    cleaned
104}
105
106/// Built-in frontmatter parser. Extracts YAML frontmatter as links and metadata.
107pub struct FrontmatterParser {
108    /// File routing filter. None = receives all File nodes.
109    pub file_filter: Option<globset::GlobSet>,
110}
111
112impl Parser for FrontmatterParser {
113    fn matches(&self, path: &str) -> bool {
114        match &self.file_filter {
115            Some(set) => set.is_match(path),
116            None => true,
117        }
118    }
119
120    fn parse(&self, _path: &str, content: &str) -> ParseResult {
121        let Some(yaml) = parse_frontmatter_yaml(content) else {
122            return ParseResult::default();
123        };
124
125        let mut links = Vec::new();
126        collect_string_leaves(&yaml, &mut links);
127        links.retain(|v| is_link_candidate(v));
128
129        ParseResult {
130            links,
131            metadata: Some(yaml_to_json(yaml)),
132        }
133    }
134}
135
136/// Parse a file's YAML frontmatter block into a value, or `None` when there is
137/// no well-formed block. Operates on code-block-stripped content so frontmatter
138/// inside fenced code examples is ignored.
139fn parse_frontmatter_yaml(content: &str) -> Option<serde_yml::Value> {
140    let content = strip_code(content);
141
142    let rest = content.strip_prefix("---")?;
143    let end = rest.find("\n---")?;
144    let yaml_str = &rest[..end];
145    if yaml_str.trim().is_empty() {
146        return None;
147    }
148
149    match serde_yml::from_str(yaml_str) {
150        Ok(value) => Some(value),
151        Err(e) => {
152            eprintln!("warn: frontmatter parser: invalid YAML: {e}");
153            None
154        }
155    }
156}
157
158/// Recursively collect all string leaf values from a YAML structure.
159/// Skips keys (only visits values) and non-string types (numbers, bools, null).
160fn collect_string_leaves(value: &serde_yml::Value, out: &mut Vec<String>) {
161    match value {
162        serde_yml::Value::String(s) => out.push(s.clone()),
163        serde_yml::Value::Sequence(seq) => {
164            for item in seq {
165                collect_string_leaves(item, out);
166            }
167        }
168        serde_yml::Value::Mapping(map) => {
169            for (_key, val) in map {
170                collect_string_leaves(val, out);
171            }
172        }
173        serde_yml::Value::Tagged(tagged) => collect_string_leaves(&tagged.value, out),
174        _ => {}
175    }
176}
177
178/// Convert serde_yml::Value to serde_json::Value.
179fn yaml_to_json(yaml: serde_yml::Value) -> serde_json::Value {
180    match yaml {
181        serde_yml::Value::Null => serde_json::Value::Null,
182        serde_yml::Value::Bool(b) => serde_json::Value::Bool(b),
183        serde_yml::Value::Number(n) => {
184            if let Some(i) = n.as_i64() {
185                serde_json::Value::Number(i.into())
186            } else if let Some(f) = n.as_f64() {
187                serde_json::Number::from_f64(f)
188                    .map(serde_json::Value::Number)
189                    .unwrap_or(serde_json::Value::Null)
190            } else {
191                serde_json::Value::Null
192            }
193        }
194        serde_yml::Value::String(s) => serde_json::Value::String(s),
195        serde_yml::Value::Sequence(seq) => {
196            serde_json::Value::Array(seq.into_iter().map(yaml_to_json).collect())
197        }
198        serde_yml::Value::Mapping(map) => {
199            let obj: serde_json::Map<String, serde_json::Value> = map
200                .into_iter()
201                .filter_map(|(k, v)| {
202                    let key = match k {
203                        serde_yml::Value::String(s) => s,
204                        other => serde_json::to_string(&yaml_to_json(other)).ok()?,
205                    };
206                    Some((key, yaml_to_json(v)))
207                })
208                .collect();
209            serde_json::Value::Object(obj)
210        }
211        serde_yml::Value::Tagged(tagged) => yaml_to_json(tagged.value),
212    }
213}
214
215#[cfg(test)]
216mod tests {
217    use super::*;
218
219    fn parse(content: &str) -> ParseResult {
220        let parser = FrontmatterParser { file_filter: None };
221        parser.parse("test.md", content)
222    }
223
224    #[test]
225    fn extracts_frontmatter_links() {
226        let content =
227            "---\nsources:\n  - ../shared/glossary.md\n  - ./prior-art.md\n---\n\n# Hello\n";
228        let result = parse(content);
229        assert_eq!(result.links.len(), 2);
230        assert_eq!(result.links[0], "../shared/glossary.md");
231        assert_eq!(result.links[1], "./prior-art.md");
232    }
233
234    #[test]
235    fn extracts_same_directory_links() {
236        let content = "---\nsources:\n  - setup.md\n  - config.rs\n---\n";
237        let result = parse(content);
238        assert_eq!(result.links.len(), 2);
239        assert_eq!(result.links[0], "setup.md");
240        assert_eq!(result.links[1], "config.rs");
241    }
242
243    #[test]
244    fn frontmatter_skips_non_paths() {
245        let content = "---\ntitle: My Document\nversion: 1.0\ntags:\n  - rust\n  - cli\n---\n";
246        let result = parse(content);
247        assert!(result.links.is_empty());
248    }
249
250    #[test]
251    fn frontmatter_skips_code_block_examples() {
252        let content = "# Doc\n\n```markdown\n---\nsources:\n  - ./fake.md\n---\n```\n";
253        let result = parse(content);
254        assert!(
255            result.links.is_empty(),
256            "frontmatter inside code block should be ignored"
257        );
258        assert!(result.metadata.is_none());
259    }
260
261    #[test]
262    fn extracts_metadata() {
263        let content =
264            "---\ntitle: My Doc\nstatus: draft\ntags:\n  - rust\n  - cli\n---\n\n# Hello\n";
265        let result = parse(content);
266        let meta = result.metadata.unwrap();
267        assert_eq!(meta["title"], "My Doc");
268        assert_eq!(meta["status"], "draft");
269        assert_eq!(meta["tags"], serde_json::json!(["rust", "cli"]));
270    }
271
272    #[test]
273    fn no_metadata_without_frontmatter() {
274        let result = parse("# Just a heading\n");
275        assert!(result.metadata.is_none());
276    }
277
278    #[test]
279    fn metadata_handles_nested_yaml() {
280        let content = "---\ntitle: Test\nauthor:\n  name: Alice\n  role: dev\n---\n";
281        let result = parse(content);
282        let meta = result.metadata.unwrap();
283        assert_eq!(meta["author"]["name"], "Alice");
284        assert_eq!(meta["author"]["role"], "dev");
285    }
286
287    #[test]
288    fn no_filter_matches_everything() {
289        let parser = FrontmatterParser { file_filter: None };
290        assert!(parser.matches("index.md"));
291        assert!(parser.matches("main.rs"));
292    }
293
294    #[test]
295    fn file_filter_restricts_matching() {
296        let mut builder = globset::GlobSetBuilder::new();
297        builder.add(globset::Glob::new("*.md").unwrap());
298        let parser = FrontmatterParser {
299            file_filter: Some(builder.build().unwrap()),
300        };
301        assert!(parser.matches("index.md"));
302        assert!(!parser.matches("main.rs"));
303    }
304
305    #[test]
306    fn extracts_uris() {
307        let content = "---\nsources:\n  - https://example.com\n  - ./local.md\n---\n";
308        let result = parse(content);
309        assert_eq!(result.links.len(), 2);
310        assert_eq!(result.links[0], "https://example.com");
311        assert_eq!(result.links[1], "./local.md");
312    }
313
314    #[test]
315    fn skips_prose_with_spaces() {
316        let content = "---\npurpose: configuration reference\nstatus: needs review\n---\n";
317        let result = parse(content);
318        assert!(result.links.is_empty());
319    }
320
321    #[test]
322    fn skips_abbreviations_and_versions() {
323        let content = "---\nnote: e.g.\nversion: v2.0\nauthor: Dr.\n---\n";
324        let result = parse(content);
325        assert!(result.links.is_empty());
326    }
327
328    #[test]
329    fn accepts_paths_without_prefix() {
330        let content = "---\nsources:\n  - config.rs\n  - docs/setup.md\n---\n";
331        let result = parse(content);
332        assert_eq!(result.links.len(), 2);
333        assert_eq!(result.links[0], "config.rs");
334        assert_eq!(result.links[1], "docs/setup.md");
335    }
336
337    #[test]
338    fn emits_absolute_paths() {
339        let content = "---\nsource: /usr/local/config.toml\n---\n";
340        let result = parse(content);
341        assert_eq!(result.links.len(), 1);
342        assert_eq!(result.links[0], "/usr/local/config.toml");
343    }
344
345    #[test]
346    fn yaml_list_values_not_parsed_as_uris() {
347        // Regression: `- name: foo bar bazz` was split on `- ` to get
348        // `name: foo bar bazz`, which the old is_uri matched as scheme `name:`
349        let content = "---\ntags:\n  - name: foo bar bazz\n  - status: draft\n---\n";
350        let result = parse(content);
351        assert!(result.links.is_empty());
352    }
353}