Hi,
I am working on a new R
package that has some web resources in the inst/
folder. One of the HTML files, let's call it index.html
, refers to various CSS and JS resources. Is there a straightforward way (from R
) to have those resources "injected" into the HTML file either "as-is" or as base64 encoded (or something similar).
I thought that using rmarkdown::pandoc_convert()
would work with the --self-contained
flag, but pandoc is a little too ambitious for what I need and does not preserve styling. I JUST want the CSS and JS resources to be embedded into the HTML so that it is self-contained.
I tried base64 encoding the JS file, for example, using the base64enc
package and then manually inserting this into the HTML file as a data URL but I couldn't get this to work, maybe I did something wrong.
Any help or guidance is much appreciated.
Here is some example files, if it helps!
# (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script src="main.js"></script>
</head>
<body>
<h1>Test</h1>
</body>
</html>
# (main.js)
console.log("Can you hear me?")
I would be happy with:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script src="data:text/plain;base64,..."></script>
</head>
<body>
<h1>Test</h1>
</body>
</html>
or
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script>console.log("Can you hear me?")</script>
</head>
<body>
<h1>Test</h1>
</body>
</html>