forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarlight.rs
More file actions
673 lines (588 loc) · 22.1 KB
/
Copy pathstarlight.rs
File metadata and controls
673 lines (588 loc) · 22.1 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Starlight-compatible markdown page generator.
//!
//! Generates a directory of `.md` files from a [`DocIndex`] that Astro Starlight
//! can render as native documentation pages with sidebar, search, and badges.
use std::collections::HashMap;
use std::fmt::Write as _;
use std::io;
use std::path::Path;
use crate::escape::escape_html;
use crate::model::*;
/// Generate Starlight-compatible markdown pages from a [`DocIndex`].
///
/// Each module becomes a directory with an `index.md`, and each item becomes
/// an individual `.md` file with Starlight frontmatter (title, description,
/// sidebar badge, etc.).
pub fn generate(index: &DocIndex, output_dir: &Path, base_url: &str) -> io::Result<()> {
std::fs::create_dir_all(output_dir)?;
let collisions = detect_collisions(index);
// Root index page
write_root_index(index, output_dir, base_url)?;
// Module pages
for module in &index.modules {
write_module_tree(module, index, output_dir, base_url, &collisions)?;
}
// Item pages (skip Module/Impl/ImplTrait — they're rendered elsewhere)
for item in &index.items {
if matches!(
item.kind,
DocItemKind::Module | DocItemKind::Impl | DocItemKind::ImplTrait
) {
continue;
}
write_item_page(item, index, output_dir, base_url, &collisions)?;
}
// Write web component assets for Starlight to import
write_component_assets(output_dir)?;
Ok(())
}
// ---------------------------------------------------------------------------
// Path helpers
// ---------------------------------------------------------------------------
/// Convert a doc path like `mylib::Greeter` to a filesystem path like `mylib/greeter.md`.
fn item_fs_path(
path: &str,
kind: DocItemKind,
collisions: &HashMap<String, Vec<DocItemKind>>,
) -> std::path::PathBuf {
let segments: Vec<&str> = path.split("::").collect();
let mut p = std::path::PathBuf::new();
if kind == DocItemKind::Module {
for seg in &segments {
p.push(slug(seg));
}
p.push("index.md");
return p;
}
// Parent module segments → directories
for seg in &segments[..segments.len().saturating_sub(1)] {
p.push(slug(seg));
}
// Item filename, with kind suffix if there's a name collision
let name = slug(segments.last().unwrap_or(&"unknown"));
if collisions.contains_key(path) {
p.push(format!("{}-{}.md", name, kind.as_str()));
} else {
p.push(format!("{name}.md"));
}
p
}
/// Convert a name to a URL-safe slug (lowercase).
fn slug(name: &str) -> String {
name.to_lowercase()
}
/// Build an absolute URL for a doc item within the Starlight site.
fn item_url(
path: &str,
kind: DocItemKind,
base_url: &str,
collisions: &HashMap<String, Vec<DocItemKind>>,
) -> String {
let fs = item_fs_path(path, kind, collisions);
let route = fs.with_extension("").to_string_lossy().replace('\\', "/");
format!("{base_url}/{route}/")
}
/// Detect doc paths that appear with multiple different kinds (rare, but possible).
fn detect_collisions(index: &DocIndex) -> HashMap<String, Vec<DocItemKind>> {
let mut path_kinds: HashMap<String, Vec<DocItemKind>> = HashMap::new();
for item in &index.items {
path_kinds
.entry(item.path.clone())
.or_default()
.push(item.kind);
}
path_kinds.retain(|_, kinds| kinds.len() > 1);
path_kinds
}
// ---------------------------------------------------------------------------
// Starlight badge mapping
// ---------------------------------------------------------------------------
fn kind_badge(kind: DocItemKind) -> (&'static str, &'static str) {
match kind {
DocItemKind::Struct => ("struct", "note"),
DocItemKind::Enum => ("enum", "caution"),
DocItemKind::Trait => ("trait", "tip"),
DocItemKind::Contract => ("contract", "danger"),
DocItemKind::Function => ("fn", "note"),
DocItemKind::TypeAlias => ("type", "note"),
DocItemKind::Const => ("const", "note"),
DocItemKind::Module => ("mod", "note"),
DocItemKind::Impl | DocItemKind::ImplTrait => ("impl", "note"),
DocItemKind::Msg => ("msg", "note"),
DocItemKind::MsgVariant => ("msg variant", "note"),
}
}
// ---------------------------------------------------------------------------
// Page writers
// ---------------------------------------------------------------------------
/// Render a signature as an HTML web component.
///
/// If `rich_signature` is non-empty, emits `<fe-signature data='...'>` with the
/// rich signature serialized as JSON. Otherwise falls back to `<fe-code-block>`.
fn render_signature_html(signature: &str, rich_signature: &[SignaturePart]) -> String {
if !rich_signature.is_empty() {
let json = serde_json::to_string(rich_signature).unwrap_or_default();
format!(
"<fe-signature data='{}'>{}</fe-signature>",
escape_html(&json),
escape_html(signature),
)
} else {
format!(
"<fe-code-block lang=\"fe\">{}</fe-code-block>",
escape_html(signature),
)
}
}
/// Write web component JS/CSS assets alongside the generated markdown.
fn write_component_assets(output_dir: &Path) -> io::Result<()> {
use crate::assets::{FE_CODE_BLOCK_JS, FE_HIGHLIGHT_CSS, FE_SIGNATURE_JS};
let dir = output_dir.join("_components");
std::fs::create_dir_all(&dir)?;
std::fs::write(dir.join("fe-code-block.js"), FE_CODE_BLOCK_JS)?;
std::fs::write(dir.join("fe-signature.js"), FE_SIGNATURE_JS)?;
std::fs::write(dir.join("fe-highlight.css"), FE_HIGHLIGHT_CSS)?;
Ok(())
}
fn write_root_index(index: &DocIndex, output_dir: &Path, base_url: &str) -> io::Result<()> {
let mut md = String::new();
writeln!(md, "---").unwrap();
writeln!(md, "title: API Reference").unwrap();
writeln!(md, "description: Fe API documentation").unwrap();
writeln!(md, "---").unwrap();
writeln!(md).unwrap();
if !index.modules.is_empty() {
writeln!(md, "## Modules\n").unwrap();
writeln!(md, "| Module | Description |").unwrap();
writeln!(md, "|--------|-------------|").unwrap();
for module in &index.modules {
let url = format!("{base_url}/{}/", slug(&module.name));
let summary = module
.items
.first()
.and_then(|i| i.summary.as_deref())
.unwrap_or("");
writeln!(md, "| [{}]({url}) | {summary} |", module.name).unwrap();
}
writeln!(md).unwrap();
}
std::fs::write(output_dir.join("index.md"), md)
}
fn write_module_tree(
module: &DocModuleTree,
index: &DocIndex,
output_dir: &Path,
base_url: &str,
collisions: &HashMap<String, Vec<DocItemKind>>,
) -> io::Result<()> {
let dir = output_dir.join(slug(&module.name));
std::fs::create_dir_all(&dir)?;
let mut md = String::new();
writeln!(md, "---").unwrap();
writeln!(md, "title: \"{}\"", module.name).unwrap();
writeln!(md, "description: \"Module {}\"", module.path).unwrap();
writeln!(md, "sidebar:").unwrap();
writeln!(md, " label: {}", module.name).unwrap();
writeln!(md, "---").unwrap();
writeln!(md).unwrap();
// Module docs (if the module has a DocItem)
if let Some(item) = index
.items
.iter()
.find(|i| i.path == module.path && i.kind == DocItemKind::Module)
&& let Some(docs) = &item.docs
{
writeln!(md, "{}\n", docs.body).unwrap();
}
// Group items by kind, sorted by display order
let mut by_kind: HashMap<DocItemKind, Vec<&DocModuleItem>> = HashMap::new();
for item in &module.items {
by_kind.entry(item.kind).or_default().push(item);
}
let mut kinds: Vec<_> = by_kind.keys().copied().collect();
kinds.sort_by_key(|k| k.display_order());
for kind in kinds {
let items = &by_kind[&kind];
writeln!(md, "## {}\n", kind.plural_name()).unwrap();
writeln!(md, "| Name | Description |").unwrap();
writeln!(md, "|------|-------------|").unwrap();
for item in items {
let url = item_url(&item.path, item.kind, base_url, collisions);
let summary = item.summary.as_deref().unwrap_or("");
writeln!(md, "| [{}]({url}) | {summary} |", item.name).unwrap();
}
writeln!(md).unwrap();
}
std::fs::write(dir.join("index.md"), md)?;
// Recurse into child modules
for child in &module.children {
write_module_tree(child, index, &dir, base_url, collisions)?;
}
Ok(())
}
fn write_item_page(
item: &DocItem,
_index: &DocIndex,
output_dir: &Path,
base_url: &str,
collisions: &HashMap<String, Vec<DocItemKind>>,
) -> io::Result<()> {
let fs_path = item_fs_path(&item.path, item.kind, collisions);
let full_path = output_dir.join(&fs_path);
if let Some(parent) = full_path.parent() {
std::fs::create_dir_all(parent)?;
}
let (badge_text, badge_variant) = kind_badge(item.kind);
let mut md = String::new();
// Frontmatter
writeln!(md, "---").unwrap();
writeln!(md, "title: \"{}\"", item.name).unwrap();
writeln!(
md,
"description: \"{} {} in {}\"",
item.kind.display_name(),
item.name,
parent_module(&item.path)
)
.unwrap();
writeln!(md, "sidebar:").unwrap();
writeln!(md, " label: {}", item.name).unwrap();
writeln!(md, " badge:").unwrap();
writeln!(md, " text: {badge_text}").unwrap();
writeln!(md, " variant: {badge_variant}").unwrap();
writeln!(md, "---").unwrap();
writeln!(md).unwrap();
// Signature
if !item.signature.is_empty() {
writeln!(
md,
"{}\n",
render_signature_html(&item.signature, &item.rich_signature)
)
.unwrap();
}
// Documentation body
if let Some(docs) = &item.docs {
writeln!(md, "{}", docs.body).unwrap();
writeln!(md).unwrap();
}
// Children grouped by kind
render_children(&mut md, &item.children);
// Trait implementations
render_trait_impls(&mut md, &item.trait_impls, base_url, collisions);
// Implementors (for trait pages)
render_implementors(&mut md, &item.implementors, base_url, collisions);
std::fs::write(full_path, md)
}
// ---------------------------------------------------------------------------
// Section renderers
// ---------------------------------------------------------------------------
fn render_children(md: &mut String, children: &[DocChild]) {
if children.is_empty() {
return;
}
let mut by_kind: HashMap<DocChildKind, Vec<&DocChild>> = HashMap::new();
for child in children {
by_kind.entry(child.kind).or_default().push(child);
}
let mut kinds: Vec<_> = by_kind.keys().copied().collect();
kinds.sort_by_key(|k| k.display_order());
for kind in kinds {
let items = &by_kind[&kind];
writeln!(md, "## {}\n", kind.plural_name()).unwrap();
for child in items {
writeln!(md, "### `{}`\n", child.name).unwrap();
if !child.signature.is_empty() {
writeln!(
md,
"{}\n",
render_signature_html(&child.signature, &child.rich_signature)
)
.unwrap();
}
if let Some(docs) = &child.docs {
writeln!(md, "{}\n", docs.body).unwrap();
}
}
}
}
fn render_trait_impls(
md: &mut String,
trait_impls: &[DocTraitImpl],
_base_url: &str,
_collisions: &HashMap<String, Vec<DocItemKind>>,
) {
if trait_impls.is_empty() {
return;
}
writeln!(md, "## Trait Implementations\n").unwrap();
for ti in trait_impls {
let heading = if ti.trait_name.is_empty() {
"Methods".to_string()
} else {
format!("impl {}", ti.trait_name)
};
writeln!(md, "### {heading}\n").unwrap();
if !ti.signature.is_empty() {
writeln!(
md,
"{}\n",
render_signature_html(&ti.signature, &ti.rich_signature)
)
.unwrap();
}
for method in &ti.methods {
writeln!(md, "#### `{}`\n", method.name).unwrap();
if !method.signature.is_empty() {
writeln!(
md,
"{}\n",
render_signature_html(&method.signature, &method.rich_signature)
)
.unwrap();
}
if let Some(docs) = &method.docs {
writeln!(md, "{}\n", docs.body).unwrap();
}
}
}
}
fn render_implementors(
md: &mut String,
implementors: &[DocImplementor],
base_url: &str,
collisions: &HashMap<String, Vec<DocItemKind>>,
) {
if implementors.is_empty() {
return;
}
writeln!(md, "## Implementors\n").unwrap();
writeln!(md, "| Type | Signature |").unwrap();
writeln!(md, "|------|-----------|").unwrap();
for imp in implementors {
let url = item_url(&imp.type_url, DocItemKind::Struct, base_url, collisions);
let sig_cell = if !imp.rich_signature.is_empty() {
let json = serde_json::to_string(&imp.rich_signature).unwrap_or_default();
format!(
"<fe-signature data='{}'>{}</fe-signature>",
escape_html(&json),
escape_html(&imp.signature),
)
} else {
format!("<code>{}</code>", escape_html(&imp.signature))
};
writeln!(md, "| [{}]({url}) | {sig_cell} |", imp.type_name).unwrap();
}
writeln!(md).unwrap();
}
/// Extract the parent module path from a fully qualified path.
fn parent_module(path: &str) -> &str {
path.rsplit_once("::").map_or(path, |(parent, _)| parent)
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
fn sample_index() -> DocIndex {
let mut index = DocIndex::new();
index.add_item(DocItem {
path: "mylib::Greeter".into(),
name: "Greeter".into(),
kind: DocItemKind::Struct,
visibility: DocVisibility::Public,
docs: Some(DocContent::from_raw("A **friendly** greeter.")),
signature: "pub struct Greeter".into(),
rich_signature: vec![],
signature_span: None,
sig_scope: None,
generics: vec![],
where_bounds: vec![],
children: vec![
DocChild {
kind: DocChildKind::Field,
name: "name".into(),
docs: Some(DocContent::from_raw("The greeter's name.")),
signature: "name: String".into(),
rich_signature: vec![],
signature_span: None,
sig_scope: None,
visibility: DocVisibility::Public,
},
DocChild {
kind: DocChildKind::Method,
name: "greet".into(),
docs: Some(DocContent::from_raw("Say hello.")),
signature: "pub fn greet(self)".into(),
rich_signature: vec![],
signature_span: None,
sig_scope: None,
visibility: DocVisibility::Public,
},
],
source: None,
source_text: None,
trait_impls: vec![],
implementors: vec![],
});
index.add_item(DocItem {
path: "mylib::hello".into(),
name: "hello".into(),
kind: DocItemKind::Function,
visibility: DocVisibility::Public,
docs: Some(DocContent::from_raw("A hello function.")),
signature: "pub fn hello()".into(),
rich_signature: vec![],
signature_span: None,
sig_scope: None,
generics: vec![],
where_bounds: vec![],
children: vec![],
source: None,
source_text: None,
trait_impls: vec![],
implementors: vec![],
});
index.modules = vec![DocModuleTree {
name: "mylib".into(),
path: "mylib".into(),
children: vec![],
items: vec![
DocModuleItem {
name: "Greeter".into(),
path: "mylib::Greeter".into(),
kind: DocItemKind::Struct,
summary: Some("A friendly greeter.".into()),
},
DocModuleItem {
name: "hello".into(),
path: "mylib::hello".into(),
kind: DocItemKind::Function,
summary: Some("A hello function.".into()),
},
],
}];
index
}
#[test]
fn generates_directory_structure() {
let index = sample_index();
let dir = std::env::temp_dir().join("fe_starlight_test");
let _ = std::fs::remove_dir_all(&dir);
generate(&index, &dir, "/api").unwrap();
assert!(dir.join("index.md").exists(), "root index");
assert!(dir.join("mylib/index.md").exists(), "module index");
assert!(dir.join("mylib/greeter.md").exists(), "struct page");
assert!(dir.join("mylib/hello.md").exists(), "function page");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn root_index_has_frontmatter() {
let index = sample_index();
let dir = std::env::temp_dir().join("fe_starlight_root");
let _ = std::fs::remove_dir_all(&dir);
generate(&index, &dir, "/api").unwrap();
let content = std::fs::read_to_string(dir.join("index.md")).unwrap();
assert!(content.starts_with("---\n"));
assert!(content.contains("title: API Reference"));
assert!(content.contains("[mylib]"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn item_page_has_badge_and_signature() {
let index = sample_index();
let dir = std::env::temp_dir().join("fe_starlight_item");
let _ = std::fs::remove_dir_all(&dir);
generate(&index, &dir, "/api").unwrap();
let content = std::fs::read_to_string(dir.join("mylib/greeter.md")).unwrap();
assert!(content.contains("text: struct"), "should have struct badge");
assert!(
content.contains("<fe-code-block") || content.contains("<fe-signature"),
"should use web component for signature, got:\n{content}"
);
assert!(
content.contains("pub struct Greeter"),
"should have signature text"
);
assert!(
!content.contains("```fe"),
"should not have fenced code blocks"
);
assert!(content.contains("## Fields"), "should group fields");
assert!(content.contains("### `name`"), "should have field heading");
assert!(content.contains("## Methods"), "should group methods");
assert!(
content.contains("### `greet`"),
"should have method heading"
);
assert!(content.contains("**friendly**"), "should have docs body");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn module_page_groups_by_kind() {
let index = sample_index();
let dir = std::env::temp_dir().join("fe_starlight_mod");
let _ = std::fs::remove_dir_all(&dir);
generate(&index, &dir, "/api").unwrap();
let content = std::fs::read_to_string(dir.join("mylib/index.md")).unwrap();
assert!(
content.contains("## Structs"),
"should have Structs section"
);
assert!(
content.contains("## Functions"),
"should have Functions section"
);
assert!(content.contains("[Greeter]"), "should link to Greeter");
assert!(content.contains("[hello]"), "should link to hello");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn writes_component_assets() {
let index = sample_index();
let dir = std::env::temp_dir().join("fe_starlight_assets");
let _ = std::fs::remove_dir_all(&dir);
generate(&index, &dir, "/api").unwrap();
let comp = dir.join("_components");
assert!(comp.join("fe-code-block.js").exists(), "fe-code-block.js");
assert!(comp.join("fe-signature.js").exists(), "fe-signature.js");
assert!(comp.join("fe-highlight.css").exists(), "fe-highlight.css");
// Verify content is non-empty
let cb = std::fs::read_to_string(comp.join("fe-code-block.js")).unwrap();
assert!(cb.contains("fe-code-block"), "should contain element name");
let sig = std::fs::read_to_string(comp.join("fe-signature.js")).unwrap();
assert!(sig.contains("fe-signature"), "should contain element name");
let css = std::fs::read_to_string(comp.join("fe-highlight.css")).unwrap();
assert!(!css.is_empty(), "CSS should be non-empty");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn item_fs_path_basic() {
let no_collisions = HashMap::new();
assert_eq!(
item_fs_path("mylib::Foo", DocItemKind::Struct, &no_collisions),
std::path::PathBuf::from("mylib/foo.md")
);
assert_eq!(
item_fs_path("mylib", DocItemKind::Module, &no_collisions),
std::path::PathBuf::from("mylib/index.md")
);
}
#[test]
fn item_fs_path_collision() {
let mut collisions = HashMap::new();
collisions.insert(
"mylib::Foo".to_string(),
vec![DocItemKind::Struct, DocItemKind::Function],
);
assert_eq!(
item_fs_path("mylib::Foo", DocItemKind::Struct, &collisions),
std::path::PathBuf::from("mylib/foo-struct.md")
);
assert_eq!(
item_fs_path("mylib::Foo", DocItemKind::Function, &collisions),
std::path::PathBuf::from("mylib/foo-fn.md")
);
}
}