如果我使用Glob Collections,我该如何获取高高的SRC的完整路径?

发布于 2025-01-19 09:58:13 字数 1592 浏览 5 评论 0原文

问题:

我正在使用短码/technical-seo/using-science-philosophy-organize-semantic-web.md Isle.jpg“,” Google Photos - Aisle 5,Faversham Sainsbury的“”,“ 320,640,1024”,“ IMG响应”%}

`TemplateContentRenderError` was thrown
[11ty] > (./technical-seo/using-science-philosophy-organize-semantic-web.md)
  EleventyShortcodeError: Error with Nunjucks shortcode `image`

`Template render error` was thrown
[11ty] > ENOENT: no such file or directory, open '/home/denverpr/repositories/yada-11ty-themesgrocery-isle.jpg'

是每个Glob Collection中的一个图像文件夹。我需要包括收集路径。真正的路径是/home/denverpr/repositories/yada-11ty-themes/technical-seo/images/grocery-isle.jpg

我的当前结构:我的当前结构:

├── technical-seo
    ├── create-jump-to-links-serp.md
    ├── ecommerce-site-structure-for-semantic-search.md
    ├── images
    │   ├── grocery-isle.jpg
    │   ├── optimize-local-seo.jpg
    │   ├── science-semantic-web.png
    │   ├── serp-jump-to-links-1.png
    │   ├── serp-jump-to-links-2.png
    │   ├── serp-jump-to-links-3.png
    │   └── serp-jump-to-links.png
    ├── index.njk
    ├── optimizing-local-search.md
    ├── technical-seo.11data.js
    └── using-science-philosophy-organize-semantic-web.md

我的ever> ever> ever> ever> ever>代码:

module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = path.dirname(__dirname) + src;

Problem:

I'm using the shortcode in /technical-seo/using-science-philosophy-organize-semantic-web.md {% image "grocery-isle.jpg", "Google Photos – Aisle 5, Faversham Sainsbury’s", "320, 640, 1024", "img-responsive" %}

`TemplateContentRenderError` was thrown
[11ty] > (./technical-seo/using-science-philosophy-organize-semantic-web.md)
  EleventyShortcodeError: Error with Nunjucks shortcode `image`

`Template render error` was thrown
[11ty] > ENOENT: no such file or directory, open '/home/denverpr/repositories/yada-11ty-themesgrocery-isle.jpg'

It's an image folder in each glob collection. I need the collection path to be included. The true path is /home/denverpr/repositories/yada-11ty-themes/technical-seo/images/grocery-isle.jpg

My current structure:

├── technical-seo
    ├── create-jump-to-links-serp.md
    ├── ecommerce-site-structure-for-semantic-search.md
    ├── images
    │   ├── grocery-isle.jpg
    │   ├── optimize-local-seo.jpg
    │   ├── science-semantic-web.png
    │   ├── serp-jump-to-links-1.png
    │   ├── serp-jump-to-links-2.png
    │   ├── serp-jump-to-links-3.png
    │   └── serp-jump-to-links.png
    ├── index.njk
    ├── optimizing-local-search.md
    ├── technical-seo.11data.js
    └── using-science-philosophy-organize-semantic-web.md

My eleventy-img code:

module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = path.dirname(__dirname) + src;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2025-01-26 09:58:13

在错误消息中,它表明它正在 /home/denverpr/repositories/yada-11ty-themesgrocery-isle.jpg 中查找图像,但没有找到任何内容,考虑到该图像是位于/home/denverpr/repositories/yada-11ty-themes/technical-seo/images/grocery-isle.jpg

您还可以看到为什么生成不正确的路径。 src 将是 /home/denverpr/repositories/yada-11ty-themes (目录名)+ grocery-isle.jpg

要解决此问题,您可以使用图像的完整路径更新图像短代码,或者将正确的路径添加到图像代码中。最佳方法将取决于您的具体情况。

方法 1 - 更新短代码(并修复生成的路径):

{% image "technical-seo/images/grocery-isle.jpg", ... %}
module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = path.dirname(__dirname) + "/" + src;

方法 2 - 修复图像代码中的路径:

module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = path.dirname(__dirname) + "/technical-seo/images/" + src;

In the error message, it shows that it's looking for the image at /home/denverpr/repositories/yada-11ty-themesgrocery-isle.jpg but not finding anything, which makes sense given that the image is located at /home/denverpr/repositories/yada-11ty-themes/technical-seo/images/grocery-isle.jpg.

You can also see why that incorrect path is generated. src will be /home/denverpr/repositories/yada-11ty-themes (the dirname thing) + grocery-isle.jpg.

To fix this, you can either update the image shortcode with the full path to the image, or add in the correct path to your image code. The best approach will depend on your specific situation.

Approach 1 - update shortcode (and also fix the generated path):

{% image "technical-seo/images/grocery-isle.jpg", ... %}
module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = path.dirname(__dirname) + "/" + src;

Approach 2 - fix path in image code:

module.exports = function(src, alt, widths, sizes, classattr) {
  // src input same as 'normal' site-relative path for convenience, so add base path:
  src = path.dirname(__dirname) + "/technical-seo/images/" + src;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文