Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for embedding media files from <audio src="..."> and <video src="..."> in the self-contained mode #355

Merged
merged 11 commits into from
Jun 16, 2022
Merged
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Authors@R: c(
person("Patrick", "Schratz", role = "ctb"),
person("Paul", "Klemm", role = "ctb", comment = c(ORCID = "0000-0002-5985-1737")),
person("Paul", "Lemmens", role = "ctb"),
person("Robert", "Fromont", role = "ctb", email = "robert.fromont@canterbury.ac.nz", comment = c(ORCID = "0000-0001-5271-5487")),
person("Sean", "Lopp", role = "ctb"),
person("Silvia", "Canelon", role = "ctb", comment = c(ORCID = "0000-0003-1709-1394")),
person("Susan", "VanderPlas", role = "ctb", comment = c(ORCID = "0000-0002-3803-0972")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# CHANGES IN xaringan VERSION 0.26

- Added support for embedding media files from `<audio src="...">` and `<video src="...">` in the self-contained mode (thanks, @robertfromont, #355).

# CHANGES IN xaringan VERSION 0.25

Expand Down
2 changes: 2 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ encode_images = function(x) {
rs = matrix(c(
'!\\[.*?\\]\\(', '\\)',
'<img .*?src\\s*=\\s*"', '".*?/>',
'<audio .*?src\\s*=\\s*"', '".*?>',
'<video .*?src\\s*=\\s*"', '".*?>',
'<source .*?src\\s*=\\s*"', '".*?>',
robertfromont marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason that you wanted to revert?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the changes broke the CI tests (and my own local knit), and I don't understand the surrounding code well enough to debug it in a timely manner. I figured having it working sooner rather than later was better.

Copy link
Owner

@yihui yihui Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I know what's going on now. I meant something like

sprintf('<%s .*?src\\s*=\\s*"', c('audio', 'video', 'source')), rep('".*?>', 3)

I'll tweak this part after merging this PR. Thanks!

'^background-image: url\\("?', '"?\\)'
), 2)
Expand Down
4 changes: 2 additions & 2 deletions inst/rmarkdown/templates/xaringan/resources/js/data-uri.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function(data, token) {
// data is an object of the form { path: base64 data }; we need to move base64
// data back to HTML nodes (e.g., into the 'src' attribute of <img>)
// data back to HTML nodes (e.g., into the 'src' attribute of <img>, <audio>, <video>, <source>)
var i, s, d, el, els;
els = document.querySelectorAll('img[src^="' + token + '"], source[src^="' + token + '"]');
els = document.querySelectorAll('img[src^="' + token + '"], audio[src^="' + token + '"], video[src^="' + token + '"], source[src^="' + token + '"]');
robertfromont marked this conversation as resolved.
Show resolved Hide resolved
for (i = 0; i < els.length; i++) {
el = els[i]; s = el.src.replace(token, ''); d = data[s];
if (d) el.src = d;
Expand Down