-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Gatsby copy linked files, video tag support, a tag support and option to ignore default ignored file types #1571
Merged
KyleAMathews
merged 13 commits into
gatsbyjs:master
from
chiedo:gatsby-copy-linked-files/more-features
Oct 12, 2017
+224
−32
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1ffe64c
More features
2421ddc
Merge branch 'master' into gatsby-copy-linked-files/more-features
sebastienfi c765ff5
Merge branch 'master' into gatsby-copy-linked-files/more-features
KyleAMathews 480d947
Format
KyleAMathews 481acf4
Fix link errors
KyleAMathews f4ba5e0
Update README
KyleAMathews 19a8e50
Update gitignore
KyleAMathews dfdb57c
Copy tweak
KyleAMathews 557ae7a
Check the url is set
KyleAMathews afcfedf
Comment tweak
KyleAMathews ff5a47b
Move plugin to dependencies to package will build on netlify
KyleAMathews 71c8bed
Hopefully this fixes build
KyleAMathews adee534
Oooops, move back to devDependencies
KyleAMathews File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/index.js | ||
/*.js | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,19 @@ const isRelativeUrl = require(`is-relative-url`) | |
const fsExtra = require(`fs-extra`) | ||
const path = require(`path`) | ||
const _ = require(`lodash`) | ||
const $ = require(`cheerio`) | ||
const cheerio = require(`cheerio`) | ||
const sizeOf = require(`image-size`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new dependency |
||
|
||
module.exports = ( | ||
{ files, markdownNode, markdownAST, getNode }, | ||
pluginOptions | ||
) => { | ||
const defaults = { | ||
ignoreFileExtensions: [`png`, `jpg`, `jpeg`, `bmp`, `tiff`], | ||
} | ||
|
||
const options = _.defaults(pluginOptions, defaults) | ||
|
||
module.exports = ({ files, markdownNode, markdownAST, getNode }) => { | ||
const filesToCopy = new Map() | ||
// Copy linked files to the public directory and modify the AST to point to | ||
// new location of the files. | ||
|
@@ -27,6 +37,12 @@ module.exports = ({ files, markdownNode, markdownAST, getNode }) => { | |
`public`, | ||
`${linkNode.internal.contentDigest}.${linkNode.extension}` | ||
) | ||
|
||
// Prevent uneeded copying | ||
if (linkPath === newPath) { | ||
return | ||
} | ||
|
||
const relativePath = path.join( | ||
`/${linkNode.internal.contentDigest}.${linkNode.extension}` | ||
) | ||
|
@@ -37,51 +53,181 @@ module.exports = ({ files, markdownNode, markdownAST, getNode }) => { | |
} | ||
} | ||
|
||
// Takes a node and generates the needed images and then returns | ||
// the needed HTML replacement for the image | ||
const generateImagesAndUpdateNode = async function(image) { | ||
const imagePath = path.posix.join( | ||
getNode(markdownNode.parent).dir, | ||
image.attr(`src`) | ||
) | ||
const imageNode = _.find(files, file => { | ||
if (file && file.absolutePath) { | ||
return file.absolutePath === imagePath | ||
} | ||
return null | ||
}) | ||
if (!imageNode || !imageNode.absolutePath) { | ||
return | ||
} | ||
|
||
const initialImageSrc = image.attr(`src`) | ||
// The link object will be modified to the new location so we'll | ||
// use that data to update our ref | ||
const link = { url: image.attr(`src`) } | ||
await visitor(link) | ||
image.attr(`src`, link.url) | ||
|
||
let dimensions | ||
|
||
if (!image.attr(`width`) || !image.attr(`height`)) { | ||
dimensions = sizeOf(imageNode.absolutePath) | ||
} | ||
|
||
// Generate default alt tag | ||
const srcSplit = initialImageSrc.split(`/`) | ||
const fileName = srcSplit[srcSplit.length - 1] | ||
const fileNameNoExt = fileName.replace(/\.[^/.]+$/, ``) | ||
const defaultAlt = fileNameNoExt.replace(/[^A-Z0-9]/gi, ` `) | ||
|
||
image.attr(`alt`, image.attr(`alt`) ? image.attr(`alt`) : defaultAlt) | ||
image.attr( | ||
`width`, | ||
image.attr(`width`) ? image.attr(`width`) : dimensions.width | ||
) | ||
image.attr( | ||
`height`, | ||
image.attr(`height`) ? image.attr(`height`) : dimensions.height | ||
) | ||
} | ||
|
||
visit(markdownAST, `link`, link => { | ||
const ext = link.url.split(`.`).pop() | ||
if (options.ignoreFileExtensions.includes(ext)) { | ||
return | ||
} | ||
|
||
visitor(link) | ||
}) | ||
|
||
// Also copy gifs since Sharp can't process them as well as svgs since we | ||
// exclude them from the image processing pipeline in | ||
// gatsby-remark-images. This will only work for markdown img tags | ||
// This will only work for markdown img tags | ||
visit(markdownAST, `image`, image => { | ||
const ext = image.url.split(`.`).pop() | ||
if (options.ignoreFileExtensions.includes(ext)) { | ||
return | ||
} | ||
|
||
const imagePath = path.join(getNode(markdownNode.parent).dir, image.url) | ||
const imageNode = _.find(files, file => { | ||
if (file && file.absolutePath) { | ||
return file.absolutePath === imagePath | ||
} | ||
return false | ||
}) | ||
if ( | ||
imageNode && | ||
(imageNode.extension === `gif` || imageNode.extension === `svg`) | ||
) { | ||
|
||
if (imageNode) { | ||
visitor(image) | ||
} | ||
}) | ||
|
||
// Same as the above except it only works for html img tags | ||
visit(markdownAST, `html`, node => { | ||
if (node.value.startsWith(`<img`)) { | ||
let image = Object.assign(node, $.parseHTML(node.value)[0].attribs) | ||
image.url = image.src | ||
image.type = `image` | ||
image.position = node.position | ||
// For each HTML Node | ||
visit(markdownAST, `html`, async node => { | ||
const $ = cheerio.load(node.value) | ||
// Handle Images | ||
const imageRefs = [] | ||
$(`img`).each(function() { | ||
try { | ||
if (isRelativeUrl($(this).attr(`src`))) { | ||
imageRefs.push($(this)) | ||
} | ||
} catch (err) { | ||
// Ignore | ||
} | ||
}) | ||
|
||
const imagePath = path.join(getNode(markdownNode.parent).dir, image.url) | ||
const imageNode = _.find(files, file => { | ||
if (file && file.absolutePath) { | ||
return file.absolutePath === imagePath | ||
for (let thisImg of imageRefs) { | ||
try { | ||
const ext = thisImg | ||
.attr(`src`) | ||
.split(`.`) | ||
.pop() | ||
if (options.ignoreFileExtensions.includes(ext)) { | ||
return | ||
} | ||
return false | ||
}) | ||
if ( | ||
imageNode && | ||
(imageNode.extension === `gif` || imageNode.extension === `svg`) | ||
) { | ||
visitor(image) | ||
|
||
await generateImagesAndUpdateNode(thisImg) | ||
} catch (err) { | ||
// Ignore | ||
} | ||
} | ||
|
||
const videoRefs = [] | ||
// Handle video tags. | ||
$(`video source`).each(function() { | ||
try { | ||
if (isRelativeUrl($(this).attr(`src`))) { | ||
videoRefs.push($(this)) | ||
} | ||
} catch (err) { | ||
// Ignore | ||
} | ||
}) | ||
|
||
for (let thisVideo of videoRefs) { | ||
try { | ||
const ext = thisVideo | ||
.attr(`src`) | ||
.split(`.`) | ||
.pop() | ||
if (options.ignoreFileExtensions.includes(ext)) { | ||
return | ||
} | ||
|
||
// The link object will be modified to the new location so we'll | ||
// use that data to update our ref | ||
const link = { url: thisVideo.attr(`src`) } | ||
await visitor(link) | ||
thisVideo.attr(`src`, link.url) | ||
} catch (err) { | ||
// Ignore | ||
} | ||
} | ||
|
||
// Handle a tags. | ||
const aRefs = [] | ||
$(`a`).each(function() { | ||
try { | ||
if (isRelativeUrl($(this).attr(`href`))) { | ||
aRefs.push($(this)) | ||
} | ||
} catch (err) { | ||
// Ignore | ||
} | ||
}) | ||
|
||
for (let thisATag of aRefs) { | ||
try { | ||
const ext = thisATag | ||
.attr(`href`) | ||
.split(`.`) | ||
.pop() | ||
if (options.ignoreFileExtensions.includes(ext)) { | ||
return | ||
} | ||
|
||
// The link object will be modified to the new location so we'll | ||
// use that data to update our ref | ||
const link = { url: thisATag.attr(`href`) } | ||
await visitor(link) | ||
thisATag.attr(`href`, link.url) | ||
} catch (err) { | ||
// Ignore | ||
} | ||
} | ||
|
||
// Replace the image node with an inline HTML node. | ||
node.type = `html` | ||
node.value = $.html() | ||
return | ||
}) | ||
|
||
return Promise.all( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are not wrapped in code tags (
<img/>
) and are attempting to render on https://www.gatsbyjs.org/packages/gatsby-remark-copy-linked-files/