-
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
feat(gatsby-remark-copy-linked-files): Add absolutePath
to dir function
#36213
Conversation
I tried out things locally a bit and I'm not sure how Can you give an example of a setup (e.g. with gatsby-config and some paths to files) and what the expected outcome is? For me, |
Below is my original motivating example config from #27097 . It worked back then, but not sure if it still works now:
This was so that the output files would stay in the same folder structure that the original file is in, instead of losing the path in the built version. Basically, a way to make https://github.com/akabekobeko/npm-gatsby-remark-copy-relative-linked-files obsolete. |
Yeah, but what was your MD files setup? I tried it like this:
pathToFile and pluginOptions.path are already both in the same dir |
Right, they were in the same directory - seem to remember it working. Maybe something changed in |
I mean, if I'd point I'm assuming you want a In theory you could already achieve this on your own at the moment by the way, couldn't you? You get |
Oh ok, so this PR is more complicated than I thought. Not sure why I thought it was working before, maybe I didn't test it after all...? If so, then would you recommend that I write a new function to calculate this relative path in the Maybe
|
Hey @LekoArts, any thoughts on the above? |
I was on vacation :) Ok, so how about doing this:
So in your case you'd use |
Hope your vacation was great! I'd be up for giving that a shot, sure! Is 95a8fcf what you mean? If this is the way forward, I can give this a go in our application to see if I can use |
@karlhorky yeah, I'd say copy the things into your |
Cool, trying this now. Here's my diff --git a/node_modules/gatsby-remark-copy-linked-files/index.js b/node_modules/gatsby-remark-copy-linked-files/index.js
index d37ee93..385ff4f 100644
--- a/node_modules/gatsby-remark-copy-linked-files/index.js
+++ b/node_modules/gatsby-remark-copy-linked-files/index.js
@@ -27,13 +27,10 @@ const validateDestinationDir = dir => {
return true;
} else if (typeof dir === `string`) {
// need to pass dummy data for validation to work
- return destinationIsValid(`${dir}/h/n`);
+ return destinationIsValid(`${dir}/n/h/a`);
} else if (_.isFunction(dir)) {
// need to pass dummy data for validation to work
- return destinationIsValid(`${dir({
- name: `n`,
- hash: `h`
- })}`);
+ return destinationIsValid(`${dir({ name: `n`, hash: `h`, absolutePath: `a` })}`);
} else {
return false;
}
@@ -43,15 +40,11 @@ const defaultDestination = linkNode => `${linkNode.internal.contentDigest}/${lin
const getDestination = (linkNode, dir) => {
if (_.isFunction(dir)) {
- // need to pass dummy data for validation to work
- const isValidFunction = `${dir({
- name: `n`,
- hash: `h`
- })}` !== `${dir({})}`;
- return isValidFunction ? `${dir({
+ return `${dir({
name: linkNode.name,
- hash: linkNode.internal.contentDigest
- })}.${linkNode.extension}` : `${dir()}/${defaultDestination(linkNode)}`;
+ hash: linkNode.internal.contentDigest,
+ absolutePath: linkNode.absolutePath,
+ })}.${linkNode.extension}`;
} else if (_.isString(dir)) {
return `${dir}/${defaultDestination(linkNode)}`;
} else {
@@ -73,7 +66,10 @@ const newLinkURL = (linkNode, options, pathPrefix) => {
destinationDir
} = options;
const destination = getDestination(linkNode, destinationDir);
- return `${pathPrefix ? pathPrefix : ``}/${destination}`;
+ const startsWithSlash = destination.startsWith(`/`)
+ return `${pathPrefix ? pathPrefix : ``}${
+ startsWithSlash ? `` : `/`
+ }${destination}`
};
function toArray(buf) { |
So this seems to be working / at parity with - {
- resolve: 'gatsby-remark-copy-relative-linked-files',
- options: {
- filename: ({
- name,
- hash,
- extension,
- }: {
- name: string;
- hash: string;
- extension: string;
- }) => `${name}-${hash}.${extension}`,
- },
- },
+ {
+ resolve: 'gatsby-remark-copy-linked-files',
+ options: {
+ destinationDir: ({
+ name,
+ hash,
+ absolutePath,
+ }: {
+ name: string;
+ hash: string;
+ absolutePath: string;
+ }) => {
+ return `${path
+ .dirname(absolutePath)
+ .replace(`${__dirname}/src/pages/`, '')}/${name}-${hash}`;
+ },
+ },
+ }, |
So from my perspective, this could be merged! 🎉 Maybe there could be an example added to the examples section in the readme too, with the |
absolutePath
to dir function
Yes, that's what I meant :) I'll want to add an example to the README for sure, can you try this instead of what you have? path.dirname(path.relative(__dirname, absolutePath)) And path.dirname(path.relative(path.join(__dirname, `src`, `pages`), absolutePath)) This should give back (for first example) the |
Oh, is this true? Given the following example, my assumption was that the path returned from the
|
@karlhorky README should be good now? |
expect(imageURL(markdownAST)).toEqual(`/${expectedDestination}`) | ||
}) | ||
}) | ||
|
||
it(`copies file to the root dir when destinationDir is not supplied`, async () => { |
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.
could also rework all the messages in the tests to remove "root" and refer to "public" instead...
but we could also just leave it! 🤷♂️
Looking pretty good! Just some small suggestions. |
Co-authored-by: Karl Horky <[email protected]>
Looking good, ready to merge? |
You can use it with |
Nice, thanks for helping out so much here @LekoArts ! I'll upgrade once it's in a non-beta release. |
Oh @LekoArts I just realized that there is still the issue of generated asset paths defined by Now that there is a I've opened an issue for this in 2020, but it was converted to a discussion (and then forgotten, like happens normally with discussions):
I'd be happy to take a shot at the implementation for this as well. |
@LekoArts Ok, finally got around to testing // save `src/pages/custom-folder/my-awesome-pdf.pdf` to `public/custom-folder/my-awesome-pdf.pdf`
// Note: Import `path` to use this example https://nodejs.org/api/path.html
destinationDir: f => `${path.dirname(path.relative(path.join(__dirname, `src`, `pages`), f.absolutePath))}/${f.name}` ...and got some weird output:
When I logged the
{
resolve: 'gatsby-remark-copy-linked-files',
options: {
destinationDir: (f: {
name: string;
hash: string;
absolutePath: string;
}) =>
console.log(f.name, f.hash, f.absolutePath) ||
console.log(
`${path.dirname(
path.relative(
path.join(__dirname, 'src', 'pages'),
f.absolutePath,
),
)}/${f.name}-${f.hash}`,
) yielded:
|
I'm working around it now in a different way, but I guess this is not desirable for users to have to figure out: {
resolve: 'gatsby-remark-copy-linked-files',
options: {
destinationDir: (f: {
name: string;
hash: string;
absolutePath: string;
}) =>
// Avoid errors when Gatsby calls function using this data:
// { name: n, hash: h, absolutePath: a }
!f.absolutePath.includes('/')
? `${f.hash}/${f.name}`
: `${path.dirname(
path.relative(
path.join(__dirname, 'src', 'pages'),
f.absolutePath,
),
)}/${f.name}-${f.hash}`,
},
}, |
Supersedes #27126 (see #27126 (comment))
Ref: #27097
Ref: #32735
Description
Draft of a fix for #27097, to pass the
linkNode.relativeDirectory
to thedestinationDir
callback.Documentation
If this is an ok solution, then I can add documentation!
Related Issues
Closes #27097