1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use anyhow::{Context, Result};
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
/// Information extracted from a README file
#[derive(Clone, Default)]
pub struct ReadmeInfo {
/// Optional title extracted from the first markdown heading
pub title: Option<String>,
/// Owner of the feature
pub owner: String,
/// Description content (everything after the first heading)
pub description: String,
/// Additional metadata from YAML frontmatter
pub meta: BTreeMap<String, serde_json::Value>,
/// The `feature` boolean from YAML frontmatter, if present
pub feature_flag: Option<bool>,
}
fn extract_first_title(content: &str) -> Option<String> {
for line in content.lines() {
let trimmed = line.trim();
// Check if this line is a title (starting with #)
if trimmed.starts_with('#') {
// Remove all leading # characters and whitespace
let title = trimmed.trim_start_matches('#').trim();
if !title.is_empty() {
return Some(title.to_string());
}
}
}
None
}
fn read_readme_content(content: &str) -> String {
let mut found_first_title = false;
let mut lines_after_title: Vec<&str> = Vec::new();
for line in content.lines() {
let trimmed = line.trim();
// Check if we found the first title (starting with #)
if !found_first_title && trimmed.starts_with('#') {
found_first_title = true;
continue;
}
// Collect all lines after the first title
if found_first_title {
lines_after_title.push(line);
}
}
// Join all lines after the first title
lines_after_title.join("\n").trim().to_string()
}
/// Reads README information from README.md or README.mdx files
/// Returns ReadmeInfo containing title, owner, description, and metadata
/// The title is extracted from the first markdown heading (# Title)
pub fn read_readme_info(readme_path: &Path) -> Result<ReadmeInfo> {
if !readme_path.exists() {
return Ok(ReadmeInfo::default());
}
let content = fs::read_to_string(readme_path)
.with_context(|| format!("could not read README file at `{}`", readme_path.display()))?;
Ok(parse_readme_info(&content))
}
/// Parses README information from the content of a README file
pub fn parse_readme_info(content: &str) -> ReadmeInfo {
let mut title: Option<String> = None;
let mut owner = "".to_string();
let mut description = "".to_string();
let mut meta: BTreeMap<String, serde_json::Value> = BTreeMap::new();
let mut feature_flag = None;
// Check if content starts with YAML front matter (---)
if let Some(stripped) = content.strip_prefix("---\n") {
if let Some(end_pos) = stripped.find("\n---\n") {
let yaml_content = &stripped[..end_pos];
let markdown_content = stripped[end_pos + 5..].to_string();
// Parse YAML front matter
if let Ok(yaml_value) = serde_yaml::from_str::<serde_yaml::Value>(yaml_content)
&& let Some(mapping) = yaml_value.as_mapping()
{
for (key, value) in mapping {
if let Some(key_str) = key.as_str() {
if key_str == "owner" {
if let Some(owner_value) = value.as_str() {
owner = owner_value.to_string();
}
} else {
if key_str == "feature" {
feature_flag = value.as_bool();
}
// Convert YAML value to JSON value for meta
if let Ok(json_value) = serde_json::to_value(value) {
meta.insert(key_str.to_string(), json_value);
}
}
}
}
}
// Extract title from markdown content (after frontmatter)
title = extract_first_title(&markdown_content);
description = read_readme_content(&markdown_content)
}
} else {
// No frontmatter, extract title and description from full content
title = extract_first_title(content);
description = read_readme_content(content)
}
ReadmeInfo {
title,
owner,
description,
meta,
feature_flag,
}
}