【问题标题】:How to avoid having to change site.baseurl -> site.github.url between master/gh-pages?如何避免在 master/gh-pages 之间更改 site.baseurl -> site.github.url?
【发布时间】:2017-01-17 23:16:29
【问题描述】:

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

我在another postdocumentation 中看到我需要将site.baseurl 更改为site.github.url 以获取我的静态资源。这就是我所做的,现在一切正常。请参阅下面 mastergh-pages 之间的差异:

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

感谢您的帮助:)

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

【问题讨论】:

  • Git 合并更改。因此,如果您更改了 gh-pages 分支上的 url,但从未在 master 分支上触摸它,那么您从 master 合并到 gh-pages 的更改将不包括 url 的更改,因此使用的 url gh-pages 将保持原样。

标签: git github jekyll


【解决方案1】:

As of Jekyll 3.3,如果您使用的是最新版本的github-pages gem,您现在可以在任何地方使用site.url。在开发中,它将自动设置为 localhost,在生产中,它将设置为您适当的 Github URL。

【讨论】:

  • 这是一个非常不幸的决定,因为许多开发环境不会将 url 设置为 localhost(例如那些在 VM 中工作的环境)
【解决方案2】:

您可以使用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

【讨论】:

  • 你认为 github 会自动为我处理吗?请看我的编辑
【解决方案3】:
  1. 您可以将 baseurl 留空 (baseurl: "") 在您的 _config.yml 中。
  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

【讨论】:

  • 我不明白 “你不需要在 post_url 前面添加任何东西” 你总是需要 baseurl 来获得任何帖子或资产的链接。
  • 我的意思是,在这种情况下,当baseurl 为空时。
猜你喜欢
  • 2021-08-01
  • 2011-08-14
  • 1970-01-01
  • 2015-12-22
  • 2013-09-01
  • 2017-02-09
  • 2013-12-23
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多