plissken-core::render::ssg::starlight
Rust
Astro Starlight adapter implementation
Pages go into a folder of the Starlight content collection
(src/content/docs/...). Starlight builds a route for each page from its
file path, so this module also holds the rules that turn a page path into
a route, and a route into a relative link that works under any base.
Structs
Section titled “Structs”struct StarlightAdapter
Section titled “struct StarlightAdapter”pub
Astro Starlight adapter.
Generates _sidebar.mjs: an ES module whose default export is a
Starlight sidebar group of the Python and Rust modules. The site’s
astro.config.mjs imports it:
import apiSidebar from './src/content/docs/reference/api/_sidebar.mjs';// ...sidebar: [ /* other groups */ apiSidebar ],Each link is a slug entry, so Starlight adds the site base to it.
Methods
Section titled “Methods”pub
fn new(route_base: &str) -> SelfCreate an adapter for pages under the given route of the site (for example reference/api).
Source
pub fn new(route_base: &str) -> Self { Self { route_base: route_base.trim_matches('/').to_string(), } }for_output_path
Section titled “for_output_path”pub
fn for_output_path(output_path: &Path) -> Option<Self>Create an adapter for an output folder in a Starlight site.
Returns None if the path is not in src/content/docs/.
Source
pub fn for_output_path(output_path: &Path) -> Option<Self> { content_route_base(output_path).map(|base| Self::new(&base)) }route_base
Section titled “route_base”pub
fn route_base(&self) -> &strThe route of the output folder in the site.
Source
pub fn route_base(&self) -> &str { &self.route_base }Functions
Section titled “Functions”fn github_slug
Section titled “fn github_slug”pub
fn github_slug(value: &str) -> StringSlug a string as Astro does (github-slugger).
Astro slugs each segment of a content file path with it, and Starlight
slugs heading text with it. Lowercase; keep letters, numbers, _, -
and spaces; spaces become -.
Source
pub fn github_slug(value: &str) -> String { value .to_lowercase() .chars() .filter(|c| c.is_alphanumeric() || *c == '_' || *c == '-' || *c == ' ') .map(|c| if c == ' ' { '-' } else { c }) .collect()}fn page_route
Section titled “fn page_route”pub
fn page_route(file_path: &str) -> StringThe route of a page, relative to the output folder.
rust/my_crate/sub.md -> rust/my_crate/sub; pkg/_core/index.md ->
pkg/_core. Each segment is slugged as Astro does.
Source
pub fn page_route(file_path: &str) -> String { let without_ext = file_path.strip_suffix(".md").unwrap_or(file_path); let route = without_ext .split('/') .filter(|s| !s.is_empty()) .map(github_slug) .collect::<Vec<_>>() .join("/"); match route.strip_suffix("/index") { Some(r) => r.to_string(), None if route == "index" => String::new(), None => route, }}fn starlight_file_path
Section titled “fn starlight_file_path”pub
fn starlight_file_path(path: &Path) -> PathBufThe file path of a page in a Starlight content folder.
Starlight skips a file whose name starts with _, so pkg/_core.md is
written as pkg/_core/index.md. Its route stays pkg/_core.
Source
pub fn starlight_file_path(path: &Path) -> PathBuf { let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or(""); if stem.starts_with('_') { path.with_extension("").join("index.md") } else { path.to_path_buf() }}fn route_href
Section titled “fn route_href”pub
fn route_href( from_file: &str, to_file: &str, anchor: Option<&str>,) -> StringA relative link from one page to another, as a route link.
Both paths are page paths relative to the output folder (foo/bar.md).
Starlight serves the page foo/bar.md at foo/bar/, so the link climbs
one level per segment of the source route. The result has no .md and
does not start with /, so it works under any base.
Source
pub fn route_href(from_file: &str, to_file: &str, anchor: Option<&str>) -> String { let from = page_route(from_file); let depth = if from.is_empty() { 0 } else { from.split('/').count() }; let to = page_route(to_file); let mut href = "../".repeat(depth); if !to.is_empty() { href.push_str(&to); href.push('/'); } if href.is_empty() { href.push_str("./"); } if let Some(anchor) = anchor { href.push('#'); href.push_str(anchor); } href}fn content_route_base
Section titled “fn content_route_base”pub
fn content_route_base(output_path: &Path) -> Option<String>The route of the output folder in the Starlight site.
The part of output_path after src/content/docs/, slugged:
docs/src/content/docs/reference/api -> Some("reference/api").
None if the path is not in a Starlight content folder.
Source
pub fn content_route_base(output_path: &Path) -> Option<String> { let parts: Vec<String> = output_path .components() .filter_map(|c| match c { Component::Normal(s) => Some(s.to_string_lossy().to_string()), _ => None, }) .collect(); let start = parts .windows(3) .rposition(|w| w[0] == "src" && w[1] == "content" && w[2] == "docs")?; Some( parts[start + 3..] .iter() .map(|s| github_slug(s)) .collect::<Vec<_>>() .join("/"), )}fn front_matter
Section titled “fn front_matter”pub
fn front_matter(title: &str, description: &str, order: usize) -> StringFront matter of a Starlight page.
sidebar.hidden keeps the page out of an autogenerate sidebar group:
the generated sidebar module lists the pages instead. editUrl: false:
a generated page has no source file to edit.
Source
pub fn front_matter(title: &str, description: &str, order: usize) -> String { let description = description.split_whitespace().collect::<Vec<_>>().join(" "); format!( "---\ntitle: {}\ndescription: {}\neditUrl: false\nsidebar:\n order: {}\n hidden: true\n---\n\n", yaml_string(title), yaml_string(&description), order )}fn breakable_name
Section titled “fn breakable_name”pub
fn breakable_name(text: &str) -> StringHeading text that a narrow column (the table of contents) can break after each _: a zero-width space follows it. Slugs drop the space, so anchors do not change.
Source
pub fn breakable_name(text: &str) -> String { text.replace('_', "_\u{200B}")}fn anchor_span
Section titled “fn anchor_span”pub
fn anchor_span(id: &str) -> StringAn HTML element that gives an item an explicit anchor id.
Put at the start of a Markdown heading. It has no text, so the slug and the table of contents entry of the heading do not change.
Source
pub fn anchor_span(id: &str) -> String { format!("<span id=\"{}\"></span>", id)}fn normalize_rustdoc_fences
Section titled “fn normalize_rustdoc_fences”pub
fn normalize_rustdoc_fences(content: &str) -> StringGive rustdoc code fences the language rust.
In Rust doc comments, a fence such as ```ignore or
```rust,no_run holds Rust code. Starlight’s highlighter knows no
language ignore, so it would show plain text.
Source
pub fn normalize_rustdoc_fences(content: &str) -> String { let mut out = String::with_capacity(content.len()); for line in content.split_inclusive('\n') { let trimmed = line.trim_start(); let indent = &line[..line.len() - trimmed.len()]; let fence_len = trimmed.chars().take_while(|c| *c == '`').count(); let info = trimmed[fence_len..].trim_end(); let is_rustdoc = fence_len >= 3 && !info.is_empty() && info .split(',') .map(str::trim) .all(|a| RUSTDOC_FENCE_ATTRIBUTES.contains(&a)); if is_rustdoc && info != "rust" { out.push_str(indent); out.push_str(&trimmed[..fence_len]); out.push_str("rust"); out.push_str(&line[line.trim_end().len()..]); } else { out.push_str(line); } } out}Constants
Section titled “Constants”| Name | Type | Value | Description |
|---|---|---|---|
STARLIGHT_SIDEBAR_FILE |
&str |
"_sidebar.mjs" |
File name of the generated sidebar module. The leading _ keeps it out of the content collection. |
STARLIGHT_DEFAULT_ROUTE_BASE |
&str |
"reference/api" |
The route base used when the output path is not in src/content/docs/. |
STARLIGHT_INDEX_FILE |
&str |
"index.md" |
The file of the index page of the API reference (item 15 of PLSKN-T-0057): the page at the route of the output folder. |
