rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
215 stars 68 forks source link

htmlDependency() sometimes doesn't allow list entries for `script` #320

Closed dmurdoch closed 2 years ago

dmurdoch commented 2 years ago

The script component of htmlDependency can be a list containing list entries in order to set attributes on the included script. However, if all_files = FALSE is used, this fails. For example, the markdown file below fails to render. Changing all_files to TRUE makes it work, as does changing self_contained to true.

The problem appears to be here:

https://github.com/rstudio/htmltools/blob/e47232123e8916bf7c8d95f4513d0fa77c8f4d13/R/html_dependency.R#L358

because the code assumes dependencies are all simple filenames.

Here's the sample file to demonstrate:

---
title: "Untitled"
output: 
  html_document:
    self_contained: false
---

```{r}
javascript <- 'console.log("this is Javascript to write to the console");'
f1 <- tempfile(fileext = ".js")
writeLines(javascript, f1)

text <- 'this is some text to go in a script'
f2 <- tempfile(fileext = ".txt")
writeLines(text, f2)
library(htmltools)
deps <- htmlDependency("test", "1.0",
                       src = tempdir(),
                       script = list(basename(f1),
                                     list(src = basename(f2),
                                          type = "text/plain",
                                          id = "shader")),
                       all_files = FALSE
                       )
tag <- tags$p("this is visible text", deps)
tag
dmurdoch commented 2 years ago

The context for this question was trying to load a GLSL shader from an external file, but as it turns out, this isn't a viable way to do that. So while I think this bug is worth fixing, it's not going to help me to fix it.

cpsievert commented 2 years ago

Addressed by https://github.com/rstudio/htmltools/commit/5fe6cd67b25153302351fd026a070d1a7660d7a6