-
-
Notifications
You must be signed in to change notification settings - Fork 496
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
Pass through copy of asset directory #1540
Comments
Try this. module.exports = function(eleventyConfig) {
// ...
eleventyConfig.addPassthroughCopy({ "posts/first-blog.assets/image.png": "posts/first-blog/first-blog.assets/"});
} |
Yes, I have seen this in the documentation. My question is about a general rule for all files in
There must be a way without doing it manually. |
Hm, perhaps defining it as „template format” and define the permalink URL as data file? |
Same issue as #379 Another workaround is to have a custom shortcode or filter to rewrite the asset URLs, but it's still not the best solution. |
@rabbaniyon I really hope this issue can be resolved by something core or official in 11ty, but in the meantime, I found a way to get most of what I wanted for this: permalinks on posts, and images stored next to markdown in my sources. I’m using the plugin https://github.com/victornpb/eleventy-plugin-page-assets, but due to a small bug in its code, I had to fork it and fix that, then point to the fork in my Anyway, my files are now sorted like this (a small compromise was giving up the
This is okay for me. It was only a small hassle to move images, and the main things I wanted were my dated folder naming plus (non-dated) permalinks. Of course, I would love for this to be properly supported by 11ty or the 11ty base blog template, because this seems like a very obvious and common way to handle posts with images. |
I had the same need when I started using Eleventy, and finally chose to have the same folders hierarchy in the source as in the build. Adapting your example, I would have this:
I use |
The plugin mentioned has some flaws and unfortunately a passthrough will not work with permalinks. So I created a simple copy, that tries to imitate how HUGO handles assets. The only thing we will need it to render images in lists a bit differently:
Also here's the solution with sample posts I played with (not a clean post folder as required, but almost): const path = require("path");
const fs = require("fs");
const glob = require ("glob");
eleventyConfig.addTransform("local-images", function(content, outputPath) {
// HUGO logic:
// - if multiple *.md are in a folder (ignoring _index.html) - then no asset-copy over will occure
// - if single index.html (allowing extra _index.html), then no further sub-dirs will be processed, all sub-dirs and files will be copied (except *.md)
//
// Alg:
// - get all md/html/njk in the directory and sub-dirs, ignoring _index.* (_index.* - could be later used to create list-templates)
// - if only 1 found = we copy the entire sub-content
// - otherwise do no copy-over nothing
const template = this;
console.warn(`TRANSFORM - input: ${template.inputPath}, output: ${outputPath}`);
const outputDir = path.dirname(outputPath);
const templateDir = path.dirname(template.inputPath).replace(/^\.\//, "");
const templateFileName = path.basename(template.inputPath);
const extensionsRegex = template._config.templateFormats.join(",");
const mdSearchPattern = path.join(templateDir, `**\\*.{${extensionsRegex}}`);
const mdIgnorePattern = path.join(templateDir, `**\\_index.{${extensionsRegex}}`);
const entries = glob.sync(mdSearchPattern, { nodir: true, ignore: mdIgnorePattern });
// only 1 page template allowed when copying assets
if (entries.length > 1) {
console.info(`Skipping copying over files from: ${templateDir} as multiple tempaltes found in directory!`);
return;
}
// copy all hierarchically, except templates
const fileSearchPattern = path.join(templateDir, `**\\*`);
const fileIgnorePattern = path.join(templateDir, `**\\*.{${extensionsRegex}}`);
const filesToCopy = glob.sync(fileSearchPattern, { nodir: true, ignore: fileIgnorePattern });
for (let filePath of filesToCopy) {
// strip template dir
// prepend output dir
const destPath = path.join(
outputDir,
filePath.substr(templateDir.length)
);
const destDir = path.dirname(destPath);
fs.mkdirSync(destDir, { recursive: true });
fs.copyFileSync(filePath, destPath);
}
// keep original content
return content;
}); |
This is solved by the upcoming AutoCopy plugin at #3552. Let’s regroup over there. |
A blog in
posts/first-blog.md
contains an image inposts/first-blog.assets/image.png
.By using:
the directory
posts/first-blog/first-blog.assets
is copied to_site/posts/first-blog.assets
.Question: how can I set a rule so that it is copied to
_site/posts/first-blog/first-blog.assets
?The text was updated successfully, but these errors were encountered: