无法同时使用limit_posts与post_url的Jekyll问题求助
问题
使用--limit_posts参数加速Jekyll构建流程时,执行以下命令报错:
$ bundle exec jekyll serve --livereload -o --incremental --limit_posts 1 Doing `require 'backports'` is deprecated and will not load any backport in the next major release. Require just the needed backports instead, or 'backports/latest'. Configuration file: C:/Users/JanMeppe/Documents/rainymood.github.io/_config.yml Source: C:/Users/JanMeppe/Documents/rainymood.github.io Destination: C:/Users/JanMeppe/Documents/rainymood.github.io/_site Incremental build: enabled Generating... Remote Theme: Using theme mmistakes/minimal-mistakes Jekyll Feed: Generating feed for posts Liquid Exception: Could not find post "2019-08-04-regex-for-noobs" in tag 'post_url'. Make sure the post exists and the name is correct. in _pages/about.md ERROR: YOUR SITE COULD NOT BE BUILT: ------------------------------------ Could not find post "2019-08-04-regex-for-noobs" in tag 'post_url'. Make sure the post exists and the name is correct.
原因是limit_posts限制了加载的文章数量,导致post_url标签无法找到未被加载的目标文章。现需解决如何同时使用limit_posts与post_url的问题。
解决方案
可以同时使用limit_posts和post_url,以下是几种实用解决方法:
临时注释
post_url引用:如果只是为了快速预览站点核心功能,可临时注释掉页面(如_pages/about.md)中的post_url标签,开发完成后再恢复。适合临时测试场景,操作简单。添加条件判断跳过未加载文章的链接:在Liquid模板中通过条件判断检查目标文章是否已加载,再决定是否渲染链接:
{% assign target_post = site.posts | where: "slug", "regex-for-noobs" | first %} {% if target_post %} <a href="{{ post_url target_post.id }}">正则入门指南</a> {% else %} <span>正则入门指南(开发预览中)</span> {% endif %}这种方式既保留了构建速度,又不会触发报错。
调整
limit_posts值覆盖所有被链接文章:如果被post_url引用的文章数量不多,将limit_posts的值设为至少包含所有被引用文章的数量。比如有3篇文章被其他页面链接,就使用--limit_posts 3,兼顾构建效率与引用需求。自定义插件扩展
post_url查找逻辑:编写Jekyll插件让post_url直接从文件系统读取文章元数据,不依赖已加载的文章列表。注意:该方法需要Ruby开发基础,且GitHub Pages可能不支持自定义插件,需确认部署环境兼容性。
内容的提问来源于stack exchange,提问作者Rainymood
相关产品推荐
相关产品推荐

