docs-core 0.3.0

Rust docs pipeline core: CommonMark+GFM to static HTML with astro-island-compatible island emission, so a Rust shell hydrates the estate's existing SolidJS bundles. Consolidates Tachyon + docs-pipeline + the starlight-* stack (ADR-0004 §2.5)
Documentation
//! The renderer: CommonMark+GFM to static HTML, with island fences routed
//! through [`crate::island`]. M1 scope is the element set the estate's docs
//! actually use; the rest of the spec (search indexing, versioning, i18n)
//! is the Wave-2 roadmap in the crate README, not silently absent.

use crate::error::DocsError;
use crate::island::Island;
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd};
use std::fmt::Write as _;

/// Render Markdown to HTML. Islands in ```` ```island ```` fences become
/// astro-island-compatible custom elements (`base` prefixes component URLs).
///
/// # Errors
/// [`DocsError`] for malformed island fences; Markdown itself never fails —
/// unknown constructs render their inner content (documented M1 behaviour).
pub fn render(markdown: &str, base: &str) -> Result<String, DocsError> {
    render_inner(markdown, base, true)
}

/// The renderer proper. `allow_islands = false` drops island fences
/// entirely (untrusted content, Wave 2 sanitization: hydration is a
/// trusted-author surface).
pub(crate) fn render_inner(
    markdown: &str,
    base: &str,
    allow_islands: bool,
) -> Result<String, DocsError> {
    let mut options = Options::empty();
    options.insert(Options::ENABLE_TABLES);
    options.insert(Options::ENABLE_STRIKETHROUGH);
    options.insert(Options::ENABLE_TASKLISTS);
    options.insert(Options::ENABLE_FOOTNOTES);
    // Wave 2 math: `$...$` and `$$...$$` arrive as InlineMath/DisplayMath
    // events and leave as build-time MathML (no client JS).
    options.insert(Options::ENABLE_MATH);

    let mut out = String::new();
    let mut headings: Vec<u8> = Vec::new();
    // Open lists, innermost last, with their ordered flags — the closing tag
    // must match the element actually opened, and paragraphs are suppressed
    // only while *some* list is open.
    let mut list_stack: Vec<bool> = Vec::new();
    let mut in_code_block = false;
    let mut parser = Parser::new_ext(markdown, options);

    // Heading anchor ids (Wave 2c.6): every heading gains a slugified id so
    // TOCs and deep links work. The opening tag is rewritten in place at
    // End(Heading), once the plain text (and therefore the slug) is known.
    // Duplicates get -2/-3 suffixes.
    let mut slug_counts: std::collections::HashMap<String, usize> =
        std::collections::HashMap::new();
    let mut heading_text = String::new();
    let mut heading_open: Option<(usize, u8)> = None; // (offset of "<hN>", level)

    while let Some(event) = parser.next() {
        match event {
            Event::Start(Tag::Heading { level, .. }) => {
                // pulldown's `level` Display already renders "h1".."h6".
                headings.push(level as u8);
                heading_text.clear();
                heading_open = Some((out.len(), level as u8));
                let _ = write!(out, "<{level}>");
            }
            Event::End(TagEnd::Heading(_)) => {
                let level = headings.pop().unwrap_or(1);
                let slug = slugify(&heading_text);
                let count = slug_counts.entry(slug.clone()).or_insert(0);
                *count += 1;
                let id = if *count == 1 {
                    slug
                } else {
                    format!("{slug}-{count}")
                };
                if let Some((offset, _)) = heading_open.take() {
                    let tag = format!("<h{level}>");
                    let with_id = format!("<h{level} id=\"{id}\">");
                    out.replace_range(offset..offset + tag.len(), &with_id);
                }
                let _ = write!(out, "</h{level}>");
            }
            Event::Start(Tag::Paragraph) => {
                if list_stack.is_empty() {
                    out.push_str("<p>");
                }
            }
            Event::End(TagEnd::Paragraph) => {
                if list_stack.is_empty() {
                    out.push_str("</p>");
                }
            }
            Event::Start(Tag::Emphasis) => out.push_str("<em>"),
            Event::End(TagEnd::Emphasis) => out.push_str("</em>"),
            Event::Start(Tag::Strong) => out.push_str("<strong>"),
            Event::End(TagEnd::Strong) => out.push_str("</strong>"),
            Event::Start(Tag::Strikethrough) => out.push_str("<del>"),
            Event::End(TagEnd::Strikethrough) => out.push_str("</del>"),
            Event::Code(code) => {
                let _ = write!(out, "<code>{}</code>", escape_html(&code));
            }
            Event::Start(Tag::Link { dest_url, .. }) => {
                let _ = write!(out, "<a href=\"{}\">", escape_attr(&dest_url));
            }
            Event::End(TagEnd::Link) => out.push_str("</a>"),
            Event::Start(Tag::Image {
                dest_url, title, ..
            }) => {
                // The alt text arrives as a Text event between Start and End;
                // capture it into the attribute instead of leaking it into
                // the output as visible body text.
                let mut alt = String::new();
                for inner in parser.by_ref() {
                    match inner {
                        Event::Text(t) => alt.push_str(&t),
                        Event::End(TagEnd::Image) => break,
                        _ => {}
                    }
                }
                let _ = write!(
                    out,
                    "<img src=\"{}\" alt=\"{}\"{}>",
                    escape_attr(&dest_url),
                    escape_attr(&alt),
                    if title.is_empty() {
                        String::new()
                    } else {
                        format!(" title=\"{}\"", escape_attr(&title))
                    }
                );
            }
            Event::End(TagEnd::Image) => {}
            Event::Start(Tag::BlockQuote(_)) => out.push_str("<blockquote>"),
            Event::End(TagEnd::BlockQuote(_)) => out.push_str("</blockquote>"),
            Event::Start(Tag::CodeBlock(kind)) => match kind {
                CodeBlockKind::Fenced(info) if info.starts_with("island") => {
                    // Island props are raw JSON, not prose: consume the fence
                    // body verbatim, emit the custom element, and swallow the
                    // matching End event.
                    let mut body = String::new();
                    for inner in parser.by_ref() {
                        match inner {
                            Event::Text(t) => {
                                body.push_str(&t);
                                break;
                            }
                            Event::End(TagEnd::CodeBlock) => break,
                            _ => {}
                        }
                    }
                    if allow_islands {
                        let island = Island::parse(&info, &body)?;
                        out.push_str(&island.to_html(base));
                    }
                    // Untrusted content: the fence (and its props) simply
                    // disappears - documented `render_untrusted` behaviour.
                    let _ = parser.next(); // the End(CodeBlock) we did not consume
                }
                _ => {
                    in_code_block = true;
                    out.push_str("<pre><code>");
                }
            },
            Event::End(TagEnd::CodeBlock) => {
                if in_code_block {
                    in_code_block = false;
                    out.push_str("</code></pre>");
                }
            }
            Event::Start(Tag::List(start)) => {
                let ordered = start.is_some();
                let _ = write!(out, "<{}>", if ordered { "ol" } else { "ul" });
                list_stack.push(ordered);
            }
            Event::End(TagEnd::List(_)) => {
                // Close the element that was actually opened — the event does
                // not carry the ordered flag, so the stack is the source of
                // truth. Empty pop = unbalanced end; emit nothing.
                match list_stack.pop() {
                    Some(true) => out.push_str("</ol>"),
                    Some(false) => out.push_str("</ul>"),
                    None => {}
                }
            }
            Event::Start(Tag::Item) => out.push_str("<li>"),
            Event::End(TagEnd::Item) => out.push_str("</li>"),
            Event::TaskListMarker(checked) => {
                let _ = write!(
                    out,
                    "<input type=\"checkbox\" disabled{}>",
                    if checked { " checked" } else { "" }
                );
            }
            Event::Rule => out.push_str("<hr>"),
            // Standard Markdown policy: raw HTML (block *and* inline) passes
            // through — docs content is first-party repository content, the
            // same contract mdBook/Zola/Starlight ship. Text nodes are
            // escaped. If content ever becomes untrusted, sanitize the
            // *output* with `ammonia` (Wave 2, §Scope in lib.rs) — do not
            // silently narrow here.
            Event::Text(text) => {
                // Inside a heading the raw text also feeds the anchor slug.
                if !headings.is_empty() {
                    heading_text.push_str(&text);
                }
                out.push_str(&escape_html(&text));
            }
            Event::Html(text) | Event::InlineHtml(text) => out.push_str(&text),
            Event::SoftBreak => out.push('\n'),
            Event::HardBreak => out.push_str("<br>"),
            // Wave 2 math: build-time LaTeX -> MathML. A malformed span
            // fails the render (BadMath names the offending input) - the
            // same fail-the-build contract as a malformed island fence.
            Event::InlineMath(tex) => {
                let mathml = mathml(&tex, latex2mathml::DisplayStyle::Inline)?;
                out.push_str(&mathml);
            }
            Event::DisplayMath(tex) => {
                let mathml = mathml(&tex, latex2mathml::DisplayStyle::Block)?;
                out.push_str(&mathml);
            }
            _ => {}
        }
    }
    Ok(out)
}

fn escape_html(src: &str) -> String {
    src.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

/// Slugify heading text for anchor ids: lowercase, alphanumerics and
/// hyphens only, runs collapsed, trimmed. Empty text yields `heading`.
fn slugify(text: &str) -> String {
    let mut slug = String::with_capacity(text.len());
    let mut last_hyphen = true;
    for c in text.chars() {
        if c.is_alphanumeric() {
            for lower in c.to_lowercase() {
                slug.push(lower);
            }
            last_hyphen = false;
        } else if !last_hyphen {
            slug.push('-');
            last_hyphen = true;
        }
    }
    let slug = slug.trim_end_matches('-').to_string();
    if slug.is_empty() {
        "heading".to_string()
    } else {
        slug
    }
}

/// Convert one math span to MathML. The output is generated, trusted
/// markup; only the *input* can fail, and it fails loudly.
fn mathml(latex: &str, style: latex2mathml::DisplayStyle) -> Result<String, DocsError> {
    latex2mathml::latex_to_mathml(latex, style).map_err(|_| DocsError::BadMath(latex.to_owned()))
}

fn escape_attr(src: &str) -> String {
    escape_html(src).replace('"', "&quot;")
}