【问题标题】:How to directly fetch permalink data (URL) from font-matter in 11ty?如何在 11ty 中直接从 font-matter 中获取永久链接数据(URL)?
【发布时间】:2022-07-10 02:51:15
【问题描述】:

我需要一些帮助来更改我在 11ty 中的博客文章的 URL/永久链接。目前,它正在制作带有帖子标题的slug。但是,我希望它制造出我想要的蛞蝓。例如:对于这篇名为“Alphabets start from A B C”的帖子,11ty 将 url(slug) 创建为“/alphabets-start-from-a-b-c”。我希望它是“/alphabets-start-a-b-c”。

这是我的 posts.11tydata.js 文件:

module.exports = {
  layout: 'post',
  title: 'Untitled',
  eleventyComputed: {
    permalink: "/{{ page.fileSlug }}/",
    featured_image: (data) => {
      if (data.featured_image) {
        if (data.featured_image.search(/^https?:\/\//) !== -1) {
          return data.featured_image;
        }
        return `/assets/img/${data.featured_image}`;
      } else {
        return false;
      }
    }
  }
};

请告诉我一种方法,以便我可以直接从 font-matter 获取自定义的帖子 url。

title: Alphabets start from A B C
body_class: blog
featured_image: lumberg.jpg
description: The first blog post in the new site.
permalink: alphabets-start-a-b-c

【问题讨论】:

    标签: eleventy


    【解决方案1】:

    在阅读了11ty Docs 之后,我得到了使用变量{{ page.filePathStem }} 的解决方法。

    所以现在我的 posts.11tydata.js 文件看起来像:

    module.exports = {
      layout: 'post',
      title: 'Untitled',
      eleventyComputed: {
        permalink: (data) => "/{{ page.filePathStem }}/",
        featured_image: (data) => {
          if (data.featured_image) {
            if (data.featured_image.search(/^https?:\/\//) !== -1) {
              return data.featured_image;
            }
            return `/assets/img/${data.featured_image}`;
          } else {
            return false;
          }
        }
      }
    };
    

    我的例子中的 Front Matter 看起来像:

    title: Alphabets start from A B C
    body_class: blog
    featured_image: lumberg.jpg
    description: The first blog post in the new site.
    permalink: "{{ page.filePathStem }}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多