max-heller/mdbook-pandoc

Replace unresolvable remote images with their descriptions

max-heller opened this issue · 0 comments

Mirror Pandoc's behavior of replacing remote images that can't be loaded with their description. We only resolve images that Pandoc can't handle, so if we fail to resolve it, we want to make sure the URL doesn't get to Pandoc, which would try and fail to process it.

This should be implemented here:

/// Pandoc usually downloads remote images and embeds them in documents, but it
/// doesn't handle some cases--we special case those here.
const PANDOC_UNSUPPORTED_IMAGE_EXTENSIONS: &[&str] = &[
// e.g. https://img.shields.io/github/actions/workflow/status/rust-lang/mdBook/main.yml?style=flat-square
".yml",
];
let path = &link[link_path_range()];
if PANDOC_UNSUPPORTED_IMAGE_EXTENSIONS
.iter()
.any(|extension| path.ends_with(extension))
{
self.download_remote_image(&link)
.and_then(|path| os_to_utf8(path.into_os_string()).map(CowStr::from))
.map_err(|err| (err, link))

Sample Pandoc log for inspiration:

[WARNING] Could not fetch resource https://img.shields.io/github/actions/workflow/status/google/comprehensive-rust/build.yml?style=flat-square: HttpExceptionRequest Request {
    host                 = "img.shields.io"
    port                 = 443
    secure               = True
    requestHeaders       = []
    path                 = "/github/actions/workflow/status/google/comprehensive-rust/build.yml"
    queryString          = "?style=flat-square"
    method               = "GET"
    proxy                = Nothing
    rawBody              = False
    redirectCount        = 10
    responseTimeout      = ResponseTimeoutDefault
    requestVersion       = HTTP/1.1
    proxySecureMode      = ProxySecureWithConnect
  }
   (ConnectionFailure user error (Network.Socket.gai_strerror not supported: 11004))
Replacing image with description.

Identified in google/comprehensive-rust#1911 (comment)