diff --git a/src/resources.rs b/src/resources.rs index 34b1a44b3..0dcbd36bf 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -1,5 +1,5 @@ use const_format::concatcp; -use html_parser::{Dom, Node}; +use html_parser::{Dom, Node, Element}; use mdbook::book::BookItem; use mdbook::renderer::RenderContext; use mime_guess::Mime; @@ -252,6 +252,27 @@ impl Asset { } } +// Look up resources in nested HTML element +fn find_assets_in_nested_html_tags(element: &Element) -> Result, Error> { + let mut found_asset = Vec::new(); + + if element.name == "img" { + if let Some(dest) = &element.attributes["src"] { + found_asset.push(dest.clone()); + } + } + for item in &element.children { + match item { + Node::Element(ref nested_element) => { + found_asset.extend(find_assets_in_nested_html_tags(nested_element)?.into_iter()); + } + _ => {} + } + } + + Ok(found_asset) +} + // Look up resources in chapter md content fn find_assets_in_markdown(chapter_src_content: &str) -> Result, Error> { let mut found_asset = Vec::new(); @@ -269,10 +290,8 @@ fn find_assets_in_markdown(chapter_src_content: &str) -> Result, Err if let Ok(dom) = Dom::parse(&content) { for item in dom.children { match item { - Node::Element(ref element) if element.name == "img" => { - if let Some(dest) = &element.attributes["src"] { - found_asset.push(dest.clone()); - } + Node::Element(ref element) => { + found_asset.extend(find_assets_in_nested_html_tags(element)?.into_iter()); } _ => {} } diff --git a/tests/dummy/book.toml b/tests/dummy/book.toml index 9b96e751b..82d468d62 100644 --- a/tests/dummy/book.toml +++ b/tests/dummy/book.toml @@ -7,4 +7,3 @@ lang = "en" [output.epub] curly-quotes = true -additional-resources = ["assets/rust-logo.png"] \ No newline at end of file diff --git a/tests/dummy/src/chapter_1.md b/tests/dummy/src/chapter_1.md index 30f432717..22dd3c830 100644 --- a/tests/dummy/src/chapter_1.md +++ b/tests/dummy/src/chapter_1.md @@ -4,12 +4,10 @@ Here is the Rust logo: ![Rust Logo](rust-logo.png) -![Image](../third_party/wikimedia/Epub_logo_color.svg) - Listing example: {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs:here}} -Rust Logo in html +

Rust Logo in html

![Image](assets/rust-logo.png)