From 14ed6483f947c5bf90f060b19bac7af99f11bb64 Mon Sep 17 00:00:00 2001 From: cinereal Date: Sun, 4 Oct 2026 11:28:07 +0200 Subject: [PATCH] ndg-html: link custom scripts where they are copied `copy_script_files` copies each script in `script_paths` to `assets/`, but the page linked the configured source path. A script from outside the output directory, such as a path in the Nix store, did not load. Link `assets/` instead. Assisted-by: Claude:claude-opus-5-5 Claude-Session: https://claude.ai/code/session_01RJvbghuj5GtCBiCjmBepNJ --- CHANGELOG.md | 5 ++++ crates/ndg-html/src/template.rs | 8 +++-- crates/ndg-html/tests/custom_scripts.rs | 40 +++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 crates/ndg-html/tests/custom_scripts.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f643cec3..e7c312263 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,11 @@ changes. ### Fixed +- Pages now link each script in `script_paths` at `assets/`, where + NDG copies it. Before, the link used the configured source path, so a script + from outside the output directory, such as a path in the Nix store, did not + load. + - An anchor link that holds only an element, such as ``, no longer gets humanized text added after the element. Before, the processor treated it as an empty diff --git a/crates/ndg-html/src/template.rs b/crates/ndg-html/src/template.rs index 2348665e9..556a76b9d 100644 --- a/crates/ndg-html/src/template.rs +++ b/crates/ndg-html/src/template.rs @@ -1627,9 +1627,13 @@ fn generate_custom_scripts( // replace default content, the user should specify `--template-dir` or // `--template` instead. for script_path in &config.script_paths { - // Relative path to script + // `copy_assets` copies each script to `assets/`, so link there, + // not to the configured source path. + let Some(file_name) = script_path.file_name() else { + continue; + }; let script_relative_path = - format!("{}{}", root_prefix, script_path.to_string_lossy()); + format!("{}assets/{}", root_prefix, file_name.to_string_lossy()); write!( custom_scripts, "" diff --git a/crates/ndg-html/tests/custom_scripts.rs b/crates/ndg-html/tests/custom_scripts.rs new file mode 100644 index 000000000..d923cc9bf --- /dev/null +++ b/crates/ndg-html/tests/custom_scripts.rs @@ -0,0 +1,40 @@ +#![allow(clippy::expect_used, reason = "Fine in tests")] +use std::path::PathBuf; + +mod common; + +use common::test_config; +use ndg_html::template::render; +use tempfile::TempDir; + +#[test] +fn test_custom_script_links_to_copied_asset() { + let temp_dir = TempDir::new().expect("Failed to create temp dir"); + let output_dir = temp_dir.path(); + + // A script outside the output directory, as with a path in the Nix store. + let config = ndg_config::Config { + script_paths: vec![PathBuf::from("/nix/store/abc-scripts/custom.js")], + ..test_config(output_dir) + }; + + let html = render( + &config, + "

Test content

", + "Test Page", + &[], + &PathBuf::from("docs/test.html"), + None, + ) + .expect("Failed to render page"); + + // The script is copied to `assets/`, so the page must link there. + assert!( + html.contains(""), + "Script should link to the copied asset" + ); + assert!( + !html.contains("/nix/store/abc-scripts/custom.js"), + "Script should not link to its source path" + ); +}