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

feat(gatsby-remark-copy-linked-files): add support for video elements with src attribute #10395

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/gatsby-remark-copy-linked-files/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ describe(`gatsby-remark-copy-linked-files`, () => {
expect(fsExtra.copy).toHaveBeenCalled()
})

it(`can copy HTML videos from video elements with the src attribute`, async () => {
const path = `videos/sample-video.mp4`

const markdownAST = remark.parse(
`<video controls="controls" autoplay="true" src="${path}">\n<p>Your browser does not support the video element.</p>\n</video>`
)

await plugin({ files: getFiles(path), markdownAST, markdownNode, getNode })

expect(fsExtra.copy).toHaveBeenCalled()
})

it(`can copy HTML videos when some siblings are in ignore extensions`, async () => {
const path = `videos/sample-video.mp4`
const path1 = `images/sample-image.jpg`
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-remark-copy-linked-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ module.exports = (

// Handle video tags.
const videoRefs = []
$(`video source`).each(function() {
$(`video source, video`).each(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

Suggested change
$(`video source, video`).each(function() {
$(`video source[src], video[src]`).each(function() {

to target elements that actually have src attribute?

With video source, video selector we would push a lot of undefined to videoRefs array:

<video>
  <source type="video/mp4" src="video.mp4"></source>
</video>

outer <video> would be targeted by selector, but it doesn't have src so it doesn't make sense to push it to array, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, that's a great idea. I looked at the code again and saw that there is quite some duplication in that file. Would you be open if I refactor that file a bit?

Copy link
Contributor

Choose a reason for hiding this comment

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

Absolutely open to that! After you mentioned it I just did quick browse of entire file (instead of just zooming in on area your changes) and see that the handling of video, audio and anchor tags is essentially copy'n'pasted and could be DRYed a bit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed a lot of duplicated code in 6efd3c3 and then added the attributes to the selector as you proposed in 067d453.

try {
if (isRelativeUrl($(this).attr(`src`))) {
videoRefs.push($(this))
Expand Down