如何避免在 master/gh-pages 之间更改 site.baseurl -> site.github.url?

How to avoid having to change site.baseurl -> site.github.url between master/gh-pages?

我正在使用 github 来托管我用 jekyll 构建的 blog

我在 another post and the documentation 中读到我需要将 site.baseurl 更改为 site.github.url 以使我的静态资源得到服务。这就是我所做的,现在一切正常。请参阅下面 mastergh-pages 之间的差异:

做起来并不太痛苦,我只是在整个项目范围内使用 atom 进行了替换;但是,我想知道,是否有更好的解决方法?理想情况下,我希望我的工作流程如此,这样我就可以使用 normal branching model 在我的博客上工作,然后与 gh-pages 合并,就好像它是一个发布分支一样,而不必担心进行搜索和替换每一次。

感谢您的帮助:)

编辑:很奇怪,我刚刚将 mastergh-pages 合并,似乎 git 自动为我处理了整个事情。如此不同的问题。当我进行合并时,git 真的知道不要将 site.github.url 更改为 site.baseurl 吗?它是如何工作的?

您可以使用 jekyll.environment:

{% if jekyll.environment == "production" %}
  <a class="post-link" href="{{ post.url | prepend: site.github.url }}">{{ post.title }}</a>
{% else %}
  <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
{% endif %}

在您的构建命令中,您必须设置 JEKYLL_ENV=production 。这是由 GitHub 自动完成的。对于其他平台,您可能必须手动执行此操作(例如在 Rakefile 中):

JEKYLL_ENV=production jekyll build

默认情况下,JEKYLL_ENV 等于 development

  1. 您可以在 _config.yml 中将 baseurl 留空 (baseurl: "")。
  2. 在这种情况下,您不需要将 baseurl 添加到 post_url

围绕 baseurl 和 url 存在一些混淆,您可能需要阅读 Jekyll 维护者的澄清: https://byparker.com/blog/2014/clearing-up-confusion-around-baseurl/

site.url: https://domain.com # your domain
site.baseurl: "/blog" # the path to your website (optionnal)
site.github.url: https://username.github.io # your GitHub URL

As of Jekyll 3.3, if you're using the latest version of the github-pages gem,您现在可以在任何地方使用 site.url。在开发中,它将自动设置为本地主机,在生产中,它将被设置为您合适的 Github URL.