Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ changes.

### Fixed

- Pages now link each script in `script_paths` at `assets/<file name>`, 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
`<a href="#fig-example"><img src="example.png"></a>`, no longer gets humanized
text added after the element. Before, the processor treated it as an empty
Expand Down
8 changes: 6 additions & 2 deletions crates/ndg-html/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/<file name>`, 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,
"<script defer src=\"{script_relative_path}\"></script>"
Expand Down
40 changes: 40 additions & 0 deletions crates/ndg-html/tests/custom_scripts.rs
Original file line number Diff line number Diff line change
@@ -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,
"<p>Test content</p>",
"Test Page",
&[],
&PathBuf::from("docs/test.html"),
None,
)
.expect("Failed to render page");

// The script is copied to `assets/<file name>`, so the page must link there.
assert!(
html.contains("<script defer src=\"../assets/custom.js\"></script>"),
"Script should link to the copied asset"
);
assert!(
!html.contains("/nix/store/abc-scripts/custom.js"),
"Script should not link to its source path"
);
}
Loading