fix(view): make static examples self-contained

Foreign cell examples read stdin with a deterministic fallback, so static exports show real output without tracebacks. Low-level session bindings named 会话 are no longer mistaken for a view plan; static export runs all cells again.
This commit is contained in:
cflmy committed 2026-10-18 13:19:41 +08:00
1 parent 518f8e386b
commit e4387259ba
5 files changed
+33 -38

No files matched your search

+10 -2
View File
@@ -170,7 +170,9 @@ fn plan_tables(
return Ok((None, None));
};
let Value::Map(fields) = plan else {
return Err("`会话` must be a foreign.session handle".into());
// `会话` is also a common low-level session id binding (e.g. from
// foreign.session_open). It is not a view plan; leave cells stateless.
return Ok((None, None));
};
let field = |name: &str| {
fields
@@ -309,8 +311,14 @@ impl DocumentSession {
collect_cells(&entry.body, &mut cells, &mut seen)?;
let declarations = capture_bindings(path, root, &module, &source)?;
let persistent = declarations.contains_key("会话");
let (plan_kernels, plan_cells) = plan_tables(&declarations)?;
let persistent = matches!(
declarations.get("会话"),
Some(Value::Map(fields))
if fields
.iter()
.any(|(k, v)| k == "kind" && value_text(v) == "foreign-session")
);
let mut kernels: Vec<KernelDecl> = Vec::new();
let mut kernel_map: HashMap<String, KernelDecl> = HashMap::new();
if let Some(rows) = plan_kernels {
-31
View File
@@ -50,13 +50,6 @@ fn record_field(record: &Value, key: &str) -> Option<Value> {
/// Run the document's cells at export time and keep their real outputs. This
/// is what makes a static page show foreign results instead of just source.
fn cell_needs_stdin(source: &str) -> bool {
source.contains("sys.stdin")
|| source.contains("process.stdin")
|| source.contains("readFileSync(0")
|| source.contains("readFileSync(0,")
}
fn static_cell_outputs(
root: &ViewRoot,
abs: &std::path::Path,
@@ -66,23 +59,6 @@ fn static_cell_outputs(
let Ok(mut session) = DocumentSession::open(abs, &root.root) else {
return out;
};
if stdin.trim().is_empty() {
let mut run_order = Vec::new();
for cell in session.cells.iter() {
if cell_needs_stdin(&cell.source) {
out.insert(
cell.name.clone(),
StaticCellOutput {
ok: true,
text: "(static: this cell reads stdin; its input is supplied by foreign.call — see Execution)".to_string(),
},
);
} else {
run_order.push(cell.name.clone());
}
}
session.order = run_order;
}
let Ok(summary) = session.run_all_with_stdin(false, Some(stdin)) else {
session.close();
return out;
@@ -263,13 +239,6 @@ fn resolve_abs(root: &ViewRoot, rel: &str) -> Result<PathBuf> {
mod tests {
use super::*;
#[test]
fn detects_stdin_dependent_cells() {
assert!(cell_needs_stdin("import sys\njson.load(sys.stdin)"));
assert!(cell_needs_stdin("process.stdin.on('data', () => {})"));
assert!(!cell_needs_stdin("console.log('hello')"));
}
#[test]
fn injects_visible_static_cell_outputs() {
let html = r#"<div class="card" data-code-name="a"></div><pre class="code-out" hidden></pre>"#;